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 05:39] gthanos [Η κλάση JCheckBoxButton] |
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://docs.oracle.com/javase/7/docs/api/java/awt/event/ |
| - | * ButtonDemo.java requires the following files: | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | */ | + | |
| - | public class ButtonDemo extends JPanel | + | |
| - | implements ActionListener { | + | |
| - | protected JButton b1, b2, b3; | + | |
| - | + | ||
| - | public ButtonDemo() { | + | |
| - | ImageIcon leftButtonIcon = createImageIcon(" | + | |
| - | ImageIcon middleButtonIcon = createImageIcon(" | + | |
| - | ImageIcon rightButtonIcon = createImageIcon(" | + | |
| - | + | ||
| - | b1 = new JButton(" | + | |
| - | b1.setVerticalTextPosition(AbstractButton.CENTER); | + | |
| - | b1.setHorizontalTextPosition(AbstractButton.LEADING); | + | |
| - | b1.setMnemonic(KeyEvent.VK_D); | + | |
| - | b1.setActionCommand(" | + | |
| - | + | ||
| - | b2 = new JButton(" | + | |
| - | b2.setVerticalTextPosition(AbstractButton.BOTTOM); | + | |
| - | b2.setHorizontalTextPosition(AbstractButton.CENTER); | + | |
| - | b2.setMnemonic(KeyEvent.VK_M); | + | |
| - | + | ||
| - | b3 = new JButton(" | + | |
| - | | + | |
| - | b3.setMnemonic(KeyEvent.VK_E); | + | |
| - | b3.setActionCommand(" | + | |
| - | b3.setEnabled(false); | + | |
| - | + | ||
| - | //Listen for actions on buttons 1 and 3. | + | |
| - | b1.addActionListener(this); | + | |
| - | b3.addActionListener(this); | + | |
| - | + | ||
| - | b1.setToolTipText(" | + | |
| - | b2.setToolTipText(" | + | |
| - | b3.setToolTipText(" | + | |
| - | + | ||
| - | //Add Components to this container, using the default FlowLayout. | + | |
| - | add(b1); | + | |
| - | add(b2); | + | |
| - | add(b3); | + | |
| - | } | + | |
| - | + | ||
| - | public void actionPerformed(ActionEvent | + | |
| - | if (" | + | |
| - | b2.setEnabled(false); | + | |
| - | b1.setEnabled(false); | + | |
| - | b3.setEnabled(true); | + | |
| - | } else { | + | |
| - | b2.setEnabled(true); | + | |
| - | b1.setEnabled(true); | + | |
| - | b3.setEnabled(false); | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | /** Returns an ImageIcon, or null if the path was invalid. */ | + | |
| - | protected static ImageIcon createImageIcon(String 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-dispatching thread. | + | |
| - | */ | + | |
| - | private static void createAndShowGUI() { | + | |
| - | + | ||
| - | | + | |
| - | JFrame frame = new JFrame(" | + | |
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | |
| - | + | ||
| - | | + | |
| - | ButtonDemo newContentPane = new ButtonDemo(); | + | |
| - | newContentPane.setOpaque(true); | + | |
| - | frame.setContentPane(newContentPane); | + | |
| - | + | ||
| - | | + | |
| - | frame.pack(); | + | |
| - | frame.setVisible(true); | + | |
| - | } | + | |
| - | + | ||
| - | public static void main(String[] args) { | + | |
| - | | + | |
| - | | + | |
| - | javax.swing.SwingUtilities.invokeLater(new Runnable() { | + | |
| - | public void run() { | + | |
| - | createAndShowGUI(); | + | |
| - | } | + | |
| - | }); | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | Το παραπάνω παράδειγμα απαιτεί την χρήση των παρακάτω εικόνων {{:swing: | + | |
| - | - Κάθε | + | |
| - | - Για κάθε Button | + | |
| - | - Κάθε Button μπορεί να συνδέεται με ένα | + | |
| - | + | ||
| - | ===== Η κλάση JCheckBoxButton ===== | + | |
| - | + | ||
| - | Δείτε το παρακάτω παράδειγμα | + | |
| - | <code java CheckBoxDemo.java> | + | <code java RadioButtonDemo.java> |
| - | package components; | + | |
| import java.awt.*; | import java.awt.*; | ||
| Line 138: | Line 19: | ||
| /* | /* | ||
| - | | + | |
| - | * directory: | + | |
| - | | + | |
| - | | + | |
| + | | ||
| + | | ||
| */ | */ | ||
| - | public class CheckBoxDemo | + | public class RadioButtonDemo |
| - | implements | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| + | static String pigString = " | ||
| - | | + | |
| - | * Four accessory choices provide for 16 different | + | |
| - | * combinations. The image for each combination is | + | |
| - | * contained in a separate image file whose name indicates | + | |
| - | * the accessories. The filenames are " | + | |
| - | * where XXXX can be one of the following 16 choices. | + | |
| - | * The " | + | |
| - | * indicates the current selection and is used to generate | + | |
| - | * the file name of the image to display. | + | |
| - | | + | public RadioButtonDemo() { |
| + | super(new BorderLayout()); | ||
| - | | + | |
| - | -g-- | + | |
| - | --h- | + | |
| - | ---t | + | |
| + | birdButton.setSelected(true); | ||
| - | | + | |
| - | | + | |
| - | c--t | + | |
| - | -gh- | + | |
| - | | + | |
| - | --ht | + | |
| - | | + | |
| - | | + | |
| - | cg-t | + | |
| - | cgh- | + | |
| - | | + | JRadioButton rabbitButton = new JRadioButton(rabbitString); |
| - | */ | + | |
| + | rabbitButton.setActionCommand(rabbitString); | ||
| - | StringBuffer choices; | + | JRadioButton pigButton = new JRadioButton(pigString); |
| - | | + | |
| + | pigButton.setActionCommand(pigString); | ||
| - | public CheckBoxDemo() { | + | //Group the radio buttons. |
| - | | + | ButtonGroup group = new ButtonGroup(); |
| + | | ||
| + | group.add(catButton); | ||
| + | group.add(dogButton); | ||
| + | group.add(rabbitButton); | ||
| + | group.add(pigButton); | ||
| - | //Create | + | //Register a listener for the radio buttons. |
| - | | + | |
| - | | + | |
| - | | + | dogButton.addActionListener(this); |
| + | rabbitButton.addActionListener(this); | ||
| + | | ||
| - | | + | |
| - | | + | picture |
| - | glassesButton.setSelected(true); | + | + birdString |
| + | + ".gif")); | ||
| - | | + | |
| - | | + | |
| - | | + | //A real program would compute this. |
| + | | ||
| - | teethButton = new JCheckBox(" | ||
| - | teethButton.setMnemonic(KeyEvent.VK_T); | ||
| - | teethButton.setSelected(true); | ||
| - | //Register a listener for the check boxes. | + | //Put the radio buttons in a column in a panel. |
| - | | + | |
| - | | + | radioPanel.add(birdButton); |
| - | | + | radioPanel.add(catButton); |
| - | | + | |
| + | | ||
| + | | ||
| - | | + | add(radioPanel, BorderLayout.LINE_START); |
| - | choices = new StringBuffer(" | + | add(picture, BorderLayout.CENTER); |
| - | + | ||
| - | //Set up the picture label | + | |
| - | pictureLabel = new JLabel(); | + | |
| - | pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC)); | + | |
| - | updatePicture(); | + | |
| - | + | ||
| - | //Put the check boxes in a column in a panel | + | |
| - | JPanel checkPanel = new JPanel(new GridLayout(0, | + | |
| - | checkPanel.add(chinButton); | + | |
| - | checkPanel.add(glassesButton); | + | |
| - | checkPanel.add(hairButton); | + | |
| - | checkPanel.add(teethButton); | + | |
| - | + | ||
| - | add(checkPanel, BorderLayout.LINE_START); | + | |
| - | add(pictureLabel, BorderLayout.CENTER); | + | |
| setBorder(BorderFactory.createEmptyBorder(20, | setBorder(BorderFactory.createEmptyBorder(20, | ||
| } | } | ||
| - | /** Listens to the check boxes. */ | + | /** Listens to the radio buttons. */ |
| - | public void itemStateChanged(ItemEvent | + | public void actionPerformed(ActionEvent |
| - | | + | |
| - | char c = ' | + | + e.getActionCommand() |
| - | Object source = e.getItemSelectable(); | + | + " |
| - | + | ||
| - | if (source == chinButton) { | + | |
| - | index = 0; | + | |
| - | c = ' | + | |
| - | } else if (source == glassesButton) { | + | |
| - | index = 1; | + | |
| - | c = ' | + | |
| - | } else if (source == hairButton) { | + | |
| - | index = 2; | + | |
| - | c = ' | + | |
| - | } else if (source == teethButton) { | + | |
| - | index = 3; | + | |
| - | c = ' | + | |
| - | } | + | |
| - | + | ||
| - | //Now that we know which button was pushed, find out | + | |
| - | //whether it was selected or deselected. | + | |
| - | if (e.getStateChange() == ItemEvent.DESELECTED) { | + | |
| - | c = ' | + | |
| - | } | + | |
| - | + | ||
| - | //Apply the change to the string. | + | |
| - | choices.setCharAt(index, | + | |
| - | + | ||
| - | updatePicture(); | + | |
| - | } | + | |
| - | + | ||
| - | protected void updatePicture() { | + | |
| - | //Get the icon corresponding to the image. | + | |
| - | ImageIcon icon = createImageIcon( | + | |
| - | | + | |
| - | + choices.toString() | + | |
| - | + " | + | |
| - | pictureLabel.setIcon(icon); | + | |
| - | pictureLabel.setToolTipText(choices.toString()); | + | |
| - | if (icon == null) { | + | |
| - | pictureLabel.setText(" | + | |
| - | } else { | + | |
| - | pictureLabel.setText(null); | + | |
| - | } | + | |
| } | } | ||
| /** 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 = CheckBoxDemo.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 296: | Line 125: | ||
| 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. | ||
| - | JComponent newContentPane = new CheckBoxDemo(); | + | JComponent newContentPane = new RadioButtonDemo(); |
| newContentPane.setOpaque(true); | newContentPane.setOpaque(true); | ||
| frame.setContentPane(newContentPane); | frame.setContentPane(newContentPane); | ||
| Line 321: | Line 150: | ||
| </ | </ | ||
| - | Θα χρειαστείτε της φωτογραφίες που περιέχονται στο zip αρχείο {{:swing:geek-images.zip|}}. | + | Για το παραπάνω παράδειγμα θα χρειαστείτε τις εικόνες {{:swing:animals.zip|}} |