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:59] gthanos [Η κλάση JCheckBox] |
swing:buttons [2018/03/23 11:30] gthanos [Η κλάση JCheckBox] |
||
|---|---|---|---|
| Line 4: | Line 4: | ||
| ή [[https:// | ή [[https:// | ||
| - | ===== Η κλάση JButton ===== | ||
| - | Δείτε, μεταγλωττίστε και τρέξτε το παρακάτω παράδειγμα από το site της Oracle, το οποίο και θα συζητήσουμε στη συνέχεια. | ||
| - | <code java> | ||
| - | package components; | ||
| - | import javax.swing.AbstractButton; | + | ===== Οι κλάσεις |
| - | import javax.swing.JButton; | + | |
| - | import javax.swing.JPanel; | + | |
| - | import javax.swing.JFrame; | + | |
| - | import javax.swing.ImageIcon; | + | |
| - | + | ||
| - | import java.awt.event.ActionEvent; | + | |
| - | import java.awt.event.ActionListener; | + | |
| - | import java.awt.event.KeyEvent; | + | |
| - | + | ||
| - | /* | + | |
| - | * ButtonDemo.java requires the following files: | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | */ | + | |
| - | public class ButtonDemo extends JPanel | + | |
| - | implements ActionListener { | + | |
| - | protected JButton b1, b2, b3; | + | |
| - | + | ||
| - | public ButtonDemo() { | + | |
| - | ImageIcon leftButtonIcon | + | |
| - | ImageIcon middleButtonIcon | + | |
| - | ImageIcon rightButtonIcon | + | |
| - | + | ||
| - | 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(" | + | |
| - | //Use the default text position of CENTER, TRAILING (RIGHT). | + | |
| - | 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 e) { | + | |
| - | 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) { | + | |
| - | java.net.URL imgURL = ButtonDemo.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-dispatching thread. | + | |
| - | */ | + | |
| - | private static void createAndShowGUI() { | + | |
| - | + | ||
| - | //Create and set up the window. | + | |
| - | JFrame frame = new JFrame(" | + | |
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | |
| - | + | ||
| - | //Create and set up the content pane. | + | |
| - | ButtonDemo newContentPane = new ButtonDemo(); | + | |
| - | newContentPane.setOpaque(true); | + | |
| - | frame.setContentPane(newContentPane); | + | |
| - | + | ||
| - | //Display the window. | + | |
| - | frame.pack(); | + | |
| - | frame.setVisible(true); | + | |
| - | } | + | |
| - | + | ||
| - | public static void main(String[] args) { | + | |
| - | //Schedule a job for the event-dispatching thread: | + | |
| - | //creating and showing this application' | + | |
| - | javax.swing.SwingUtilities.invokeLater(new Runnable() { | + | |
| - | public void run() { | + | |
| - | createAndShowGUI(); | + | |
| - | } | + | |
| - | }); | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | Το παραπάνω παράδειγμα απαιτεί την χρήση των παρακάτω εικόνων {{: | + | |
| - | - Κάθε button μπορεί να περιέχει κείμενο και ένα εικονίδιο | + | |
| - | - Για κάθε Button οφείλουμε να ορίσουμε τουλάχιστον ένα αντικείμενο τύπου ActionListener. | + | |
| - | - Κάθε Button μπορεί να συνδέεται με ένα //keyboard shortcut//. Για να ορίσετε ένα keyboard shortcut χρησιμοποιήστε τη μέθοδο '' | + | |
| - | ===== Η κλάση JCheckBox ===== | + | Τα //radio buttons// είναι ανάλογα με τα //check boxes// με την ιδιαιτερότητα ότι μόνο ένα button μπορεί να είναι επιλεγμένο κάθε φορά. Τα radio buttons οργανώνονται |
| - | Ένα //check box// έχει δύο δυνατές καταστάσεις //α)// επιλεγμένο και //β)// απο-επιλεγμένο. Κάθε | + | Κάθε φορά που ο χρήστης |
| - | <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 |
| - | */ | + | |
| - | + | | |
| - | StringBuffer choices; | + | |
| - | JLabel pictureLabel; | + | |
| - | + | ||
| - | public CheckBoxDemo() { | + | |
| - | super(new BorderLayout()); | + | |
| - | + | ||
| - | //Create the check boxes. | + | |
| - | chinButton | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | ButtonGroup group = new ButtonGroup(); |
| - | | + | |
| + | group.add(catButton); | ||
| + | group.add(dogButton); | ||
| + | group.add(rabbitButton); | ||
| + | | ||
| - | | + | |
| - | | + | birdButton.addActionListener(this); |
| - | | + | |
| + | dogButton.addActionListener(this); | ||
| + | rabbitButton.addActionListener(this); | ||
| + | | ||
| - | //Register a listener for the check boxes. | + | //Set up the picture label. |
| - | | + | |
| - | glassesButton.addItemListener(this); | + | + birdString |
| - | | + | + ".gif")); |
| - | teethButton.addItemListener(this); | + | |
| - | //Indicates what's on the geek. | + | //The preferred size is hard-coded to be the width of the |
| - | | + | //widest image and the height of the tallest image. |
| + | | ||
| + | picture.setPreferredSize(new Dimension(177, 122)); | ||
| - | //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 | + | //Put the radio buttons |
| - | JPanel | + | JPanel |
| - | | + | |
| - | | + | radioPanel.add(catButton); |
| - | | + | |
| - | | + | |
| + | | ||
| - | add(checkPanel, BorderLayout.LINE_START); | + | add(radioPanel, BorderLayout.LINE_START); |
| - | add(pictureLabel, BorderLayout.CENTER); | + | add(picture, 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: | ||
| </ | </ | ||
| - | Θα χρειαστείτε | + | Για το παραπάνω παράδειγμα θα χρειαστείτε τις εικόνες {{:swing:animals.zip|}} |
| - | + | ||
| - | ===== Οι κλάσεις ButtonGroup και JRadioButton ===== | + | |