swing:labels
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| swing:labels [2016/05/25 07:36] – gthanos | swing:labels [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Labels ====== | + | ====== Labels |
| Μέχρι τώρα είδαμε πως μπορείτε να προσθέσετε ένα κουμπί σε ένα αντικείμενο της κλάσης [[http:// | Μέχρι τώρα είδαμε πως μπορείτε να προσθέσετε ένα κουμπί σε ένα αντικείμενο της κλάσης [[http:// | ||
| Line 11: | Line 11: | ||
| public LabelGreeting () { | public LabelGreeting () { | ||
| super(); | super(); | ||
| - | | + | |
| - | | + | |
| - | JLabel greeting = new JLabel(" | + | |
| + | //JLabel greeting = new JLabel(new ImageIcon(" | ||
| + | | ||
| add(greeting); | add(greeting); | ||
| + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | pack(); | ||
| + | setVisible(true); | ||
| } | } | ||
| | | ||
| public static void main(String[] args) { | public static void main(String[] args) { | ||
| - | LabelGreeting lg = new LabelGreeting(); | + | |
| - | | + | public void run() { |
| + | LabelGreeting lg = new LabelGreeting(); | ||
| + | } | ||
| + | | ||
| } | } | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| - | Ένα αντικείμενο της κλάσης JLabel μπορεί να περιέχει κείμενο ή εικόνα ή και τα δύο. Επίσης σε ένα τέτοιο αντικείμενο της κλάσης, | + | Ένα αντικείμενο της κλάσης |
| <code java> | <code java> | ||
| void setHorizontalAlignment(int alignment) | void setHorizontalAlignment(int alignment) | ||
| //Sets the alignment of the label' | //Sets the alignment of the label' | ||
| void setVerticalAlignment(int alignment) | void setVerticalAlignment(int alignment) | ||
| - | Sets the alignment of the label' | + | //Sets the alignment of the label' |
| </ | </ | ||
| - | Εάν ένα | + | Εάν ένα |
| <code java> | <code java> | ||
| void setHorizontalTextPosition(int textPosition) | void setHorizontalTextPosition(int textPosition) | ||
| Line 40: | Line 49: | ||
| </ | </ | ||
| - | Δείτε | + | Στο παραπάνω πρόγραμμα βάλτε σε σχόλια τη γραμμή 9 και βγάλτε |
| - | <code java LabelDemo.java> | + | |
| - | + | ||
| - | import java.awt.*; | + | |
| - | import java.awt.event.*; | + | |
| - | import javax.swing.*; | + | |
| - | + | ||
| - | /* | + | |
| - | * LabelDemo.java needs one other file: | + | |
| - | | + | |
| - | */ | + | |
| - | public class LabelDemo extends JPanel | + | |
| - | public LabelDemo() | + | |
| - | super(new GridLayout(3, | + | |
| - | + | ||
| - | JLabel label1, label2, label3, label4; | + | |
| - | + | ||
| - | ImageIcon icon = createImageIcon(" | + | |
| - | " | + | |
| - | + | ||
| - | // | + | |
| - | + | ||
| - | //Create the first label. | + | |
| - | label1 = new JLabel(" | + | |
| - | icon, | + | |
| - | JLabel.LEFT); | + | |
| - | //Set the position of its text, relative to its icon: | + | |
| - | label1.setVerticalTextPosition(JLabel.BOTTOM); | + | |
| - | label1.setHorizontalTextPosition(JLabel.CENTER); | + | |
| - | label1.setBorder(BorderFactory.createLineBorder(Color.BLACK)); | + | |
| - | + | ||
| - | //Create the other labels. | + | |
| - | label2 = new JLabel(" | + | |
| - | // | + | |
| - | label2.setBorder(BorderFactory.createLineBorder(Color.BLACK)); | + | |
| - | + | ||
| - | label3 = new JLabel(icon); | + | |
| - | label3.setBorder(BorderFactory.createLineBorder(Color.BLACK)); | + | |
| - | + | ||
| - | label4 = new JLabel("< | + | |
| - | label4.setBorder(BorderFactory.createLineBorder(Color.BLACK)); | + | |
| - | + | ||
| - | //Create tool tips, for the heck of it. | + | |
| - | label1.setToolTipText(" | + | |
| - | label2.setToolTipText(" | + | |
| - | label3.setToolTipText(" | + | |
| - | + | ||
| - | //Add the labels. | + | |
| - | add(label1); | + | |
| - | add(label2); | + | |
| - | add(label3); | + | |
| - | add(label4); | + | |
| - | } | + | |
| - | + | ||
| - | /** Returns an ImageIcon, or null if the path was invalid. */ | + | |
| - | protected static ImageIcon createImageIcon(String path, | + | |
| - | | + | |
| - | java.net.URL imgURL = LabelDemo.class.getResource(path); | + | |
| - | if (imgURL != null) { | + | |
| - | return new ImageIcon(imgURL, | + | |
| - | } else { | + | |
| - | System.err.println(" | + | |
| - | return null; | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * Create the GUI and show it. For thread safety, | + | |
| - | * this method should be invoked from the | + | |
| - | * event dispatch thread. | + | |
| - | */ | + | |
| - | private static void createAndShowGUI() { | + | |
| - | //Create and set up the window. | + | |
| - | JFrame frame = new JFrame(" | + | |
| - | frame.setSize(400, | + | |
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | |
| - | + | ||
| - | //Add content to the window. | + | |
| - | frame.add(new LabelDemo()); | + | |
| - | + | ||
| - | //Display the window. | + | |
| - | // | + | |
| - | frame.setVisible(true); | + | |
| - | } | + | |
| - | + | ||
| - | public static void main(String[] args) { | + | |
| - | //Schedule a job for the event dispatch thread: | + | |
| - | //creating and showing this application' | + | |
| - | SwingUtilities.invokeLater(new Runnable() { | + | |
| - | public void run() { | + | |
| - | //Turn off metal' | + | |
| - | UIManager.put(" | + | |
| - | + | ||
| - | createAndShowGUI(); | + | |
| - | } | + | |
| - | }); | + | |
| - | | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| + | | Προηγούμενο: | ||
swing/labels.1464161776.txt.gz · Last modified: 2016/05/25 06:36 (external edit)
