This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
swing:labels [2016/05/25 07:36] gthanos |
swing:labels [2018/03/27 19:37] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Labels ====== | ||
| - | |||
| - | Μέχρι τώρα είδαμε πως μπορείτε να προσθέσετε ένα κουμπί σε ένα αντικείμενο της κλάσης [[http:// | ||
| - | |||
| - | <code java LabelGreeting.java> | ||
| - | import javax.swing.*; | ||
| - | import java.awt.event.ActionListener; | ||
| - | import java.awt.event.ActionEvent; | ||
| - | |||
| - | public class LabelGreeting extends JFrame { | ||
| - | public LabelGreeting () { | ||
| - | super(); | ||
| - | setSize(200, | ||
| - | setTitle(" | ||
| - | JLabel greeting = new JLabel(" | ||
| - | add(greeting); | ||
| - | } | ||
| - | | ||
| - | public static void main(String[] args) { | ||
| - | LabelGreeting lg = new LabelGreeting(); | ||
| - | lg.setVisible(true); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Ένα αντικείμενο της κλάσης 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(); | ||
| - | } | ||
| - | }); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||