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:buttons [2015/05/11 04:44] gthanos [Η κλάση JButton] |
swing:buttons [2018/03/23 11:30] gthanos [Η κλάση JCheckBox] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Buttons, Check Boxes & Radio Buttons ====== | ====== Buttons, Check Boxes & Radio Buttons ====== | ||
| - | Για την δημιουργία ενός button μπορείτε να χρησιμοποιήσετε μία κλάση από τις απογόνους της κλάσης [[https:// | + | Για την δημιουργία ενός button μπορείτε να χρησιμοποιήσετε μία κλάση από τις απογόνους της κλάσης [[https:// |
| - | ή [[https:// | + | ή [[https:// |
| - | ===== Η κλάση JButton ===== | ||
| - | Δείτε, μεταγλωττίστε και τρέξτε το παρακάτω παράδειγμα από το site της Oracle, το οποίο και θα συζητήσουμε στη συνέχεια. | ||
| - | <code java> | ||
| - | package components; | ||
| - | import javax.swing.AbstractButton; | + | ===== Οι κλάσεις ButtonGroup και JRadioButton ===== |
| - | import javax.swing.JButton; | + | |
| - | import javax.swing.JPanel; | + | |
| - | import javax.swing.JFrame; | + | |
| - | import javax.swing.ImageIcon; | + | |
| - | import java.awt.event.ActionEvent; | + | Τα //radio buttons// είναι ανάλογα με τα //check boxes// με την ιδιαιτερότητα ότι μόνο ένα button μπορεί να είναι επιλεγμένο κάθε φορά. Τα radio buttons οργανώνονται σε button groups. Από τα κουμπιά ενός button group μόνο ένα είναι επιλέξιμο. |
| - | import java.awt.event.ActionListener; | + | |
| - | import java.awt.event.KeyEvent; | + | |
| - | /* | + | Κάθε φορά που ο χρήστης επιλέγει ένα radio button τότε δημιουργείται ένα αντικείμενο τύπου [[http:// |
| - | | + | |
| - | | + | <code java RadioButtonDemo.java> |
| - | | + | |
| - | | + | import java.awt.*; |
| + | import java.awt.event.*; | ||
| + | import javax.swing.*; | ||
| + | |||
| + | /* | ||
| + | * RadioButtonDemo.java requires | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| */ | */ | ||
| - | public class ButtonDemo | + | public class RadioButtonDemo |
| - | implements ActionListener { | + | |
| - | | + | |
| + | static String catString = " | ||
| + | static String dogString = " | ||
| + | static String rabbitString = " | ||
| + | static String pigString = " | ||
| - | | + | |
| - | ImageIcon leftButtonIcon = createImageIcon(" | + | |
| - | ImageIcon middleButtonIcon = createImageIcon(" | + | |
| - | ImageIcon rightButtonIcon = createImageIcon(" | + | |
| - | b1 = new JButton(" | + | public RadioButtonDemo() { |
| - | | + | |
| - | b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales | + | |
| - | b1.setMnemonic(KeyEvent.VK_D); | + | |
| - | b1.setActionCommand(" | + | |
| - | | + | |
| - | | + | JRadioButton birdButton |
| - | | + | |
| - | | + | |
| + | | ||
| - | | + | |
| - | | + | |
| - | b3.setMnemonic(KeyEvent.VK_E); | + | |
| - | | + | |
| - | b3.setEnabled(false); | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | //Add Components | + | |
| - | add(b1); | + | pigButton.setMnemonic(KeyEvent.VK_P); |
| - | add(b2); | + | pigButton.setActionCommand(pigString); |
| - | add(b3); | + | |
| + | //Group the radio buttons. | ||
| + | ButtonGroup group = new ButtonGroup(); | ||
| + | group.add(birdButton); | ||
| + | group.add(catButton); | ||
| + | group.add(dogButton); | ||
| + | group.add(rabbitButton); | ||
| + | group.add(pigButton); | ||
| + | |||
| + | //Register a listener for the radio buttons. | ||
| + | birdButton.addActionListener(this); | ||
| + | catButton.addActionListener(this); | ||
| + | dogButton.addActionListener(this); | ||
| + | rabbitButton.addActionListener(this); | ||
| + | pigButton.addActionListener(this); | ||
| + | |||
| + | //Set up the picture label. | ||
| + | picture = new JLabel(createImageIcon(" | ||
| + | + birdString | ||
| + | + " | ||
| + | |||
| + | | ||
| + | //widest image and the height of the tallest image. | ||
| + | //A real program would compute | ||
| + | picture.setPreferredSize(new Dimension(177, 122)); | ||
| + | |||
| + | |||
| + | //Put the radio buttons in a column in a panel. | ||
| + | | ||
| + | radioPanel.add(birdButton); | ||
| + | | ||
| + | | ||
| + | radioPanel.add(rabbitButton); | ||
| + | radioPanel.add(pigButton); | ||
| + | |||
| + | add(radioPanel, | ||
| + | add(picture, | ||
| + | setBorder(BorderFactory.createEmptyBorder(20, | ||
| } | } | ||
| + | /** Listens to the radio buttons. */ | ||
| public void actionPerformed(ActionEvent e) { | public void actionPerformed(ActionEvent e) { | ||
| - | | + | |
| - | b2.setEnabled(false); | + | + e.getActionCommand() |
| - | b1.setEnabled(false); | + | + ".gif")); |
| - | b3.setEnabled(true); | + | |
| - | } else { | + | |
| - | b2.setEnabled(true); | + | |
| - | b1.setEnabled(true); | + | |
| - | b3.setEnabled(false); | + | |
| - | } | + | |
| } | } | ||
| /** Returns an ImageIcon, or null if the path was invalid. */ | /** Returns an ImageIcon, or null if the path was invalid. */ | ||
| protected static ImageIcon createImageIcon(String path) { | protected static ImageIcon createImageIcon(String path) { | ||
| - | java.net.URL imgURL = ButtonDemo.class.getResource(path); | + | java.net.URL imgURL = RadioButtonDemo.class.getResource(path); |
| if (imgURL != null) { | if (imgURL != null) { | ||
| return new ImageIcon(imgURL); | return new ImageIcon(imgURL); | ||
| Line 90: | Line 119: | ||
| /** | /** | ||
| - | * Create the GUI and show it. For thread safety, | + | * Create the GUI and show it. For thread safety, |
| - | * this method should be invoked from the | + | * this method should be invoked from the |
| * event-dispatching thread. | * event-dispatching thread. | ||
| */ | */ | ||
| private static void createAndShowGUI() { | private static void createAndShowGUI() { | ||
| - | |||
| //Create and set up the window. | //Create and set up the window. | ||
| - | JFrame frame = new JFrame(" | + | JFrame frame = new JFrame(" |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| //Create and set up the content pane. | //Create and set up the content pane. | ||
| - | | + | |
| newContentPane.setOpaque(true); | newContentPane.setOpaque(true); | ||
| frame.setContentPane(newContentPane); | frame.setContentPane(newContentPane); | ||
| Line 115: | Line 143: | ||
| javax.swing.SwingUtilities.invokeLater(new Runnable() { | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
| public void run() { | public void run() { | ||
| - | createAndShowGUI(); | + | createAndShowGUI(); |
| } | } | ||
| }); | }); | ||
| Line 121: | Line 149: | ||
| } | } | ||
| </ | </ | ||
| - | Το παραπάνω παράδειγμα απαιτεί την χρήση | + | |
| - | - Κάθε button μπορεί να περιέχει κείμενο και ένα εικονίδιο σε αναλογία με τα αντικείμενα τύπου JLabel. Επίσης μπορεί να περιέχει και HTML όπως και τα JLabel (δεν εμφανίζεται στο παραπάνω παράδειγμα). | + | Για το παραπάνω παράδειγμα |
| - | - Για κάθε Button οφείλουμε να ορίσουμε τουλάχιστον ένα ActionListener αντικείμενο. | + | |
| - | - Κάθε Button μπορεί να συνδέεται με ένα //keyboard shortcut//. Για να ορίσετε ένα keyboard shortcut χρησιμοποιήστε τη μέθοδο '' | + | |