This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
swing:labels [2015/05/02 10:46] gthanos |
swing:labels [2017/04/04 15:55] gthanos |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Labels ====== | ====== Labels ====== | ||
| - | Μέχρι τώρα είδαμε πως μπορείτε να προσθέσετε ένα κουμπί σε ένα JFrame. Εάν θέλετε να προσθέσετε | + | Μέχρι τώρα είδαμε πως μπορείτε να προσθέσετε ένα κουμπί σε ένα |
| <code java LabelGreeting.java> | <code java LabelGreeting.java> | ||
| Line 18: | Line 18: | ||
| | | ||
| public static void main(String[] args) { | public static void main(String[] args) { | ||
| - | LabelGreeting lg = new LabelGreeting(); | + | |
| - | lg.setVisible(true); | + | public void run() { |
| + | LabelGreeting lg = new LabelGreeting(); | ||
| + | | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Ένα αντικείμενο της κλάσης JLabel μπορεί να περιέχει κείμενο ή εικόνα ή και τα δύο. Επίσης σε ένα τέτοιο αντικείμενο της κλάσης, | ||
| + | <code java> | ||
| + | void setHorizontalAlignment(int alignment) | ||
| + | //Sets the alignment of the label' | ||
| + | void setVerticalAlignment(int alignment) | ||
| + | Sets the alignment of the label' | ||
| + | </ | ||
| + | |||
| + | Εάν ένα Label περιέχει κείμενο και εικόνα μπορείτε να προσδιορίσετε την θέση του κειμένου σε σχέση με την εικόνα μέσα από τις μεθόδους | ||
| + | <code java> | ||
| + | void setHorizontalTextPosition(int textPosition) | ||
| + | //Sets the horizontal position of the label' | ||
| + | void setVerticalTextPosition(int textPosition) | ||
| + | //Sets the vertical position of the label' | ||
| + | </ | ||
| + | |||
| + | Δείτε το παρακάτω παράδειγμα από το site της Oracle και μεταβάλλετε τις παραμέτρους. Παρατηρήστε πως μπορείτε να προσθέσετε HTML κείμενο σε ένα αντικείμενο τύπου Label. | ||
| + | <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(); | ||
| + | } | ||
| + | }); | ||
| } | } | ||
| } | } | ||