swing:buttons
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| swing:buttons [2015/05/11 05:59] – [Η κλάση JCheckBox] gthanos | swing:buttons [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| ή [[https:// | ή [[https:// | ||
| - | ===== Η κλάση JButton ===== | + | * [[swing:jbutton|Buttons]] | 
| - | + |  | |
| - | Δείτε, μεταγλωττίστε και τρέξτε το παρακάτω παράδειγμα από το site της Oracle, το οποίο και θα συζητήσουμε στη συνέχεια. | + | * [[swing:jradiobutton|RadioButtons]] | 
| - | <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: | + | |
| - |  | + | |
| - | *   images/ | + | |
| - |  | + | |
| - | */ | + | |
| - | 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(" | + | |
| - | //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(); | + | |
| - | } | + | |
| - | }); | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | Το παραπάνω παράδειγμα απαιτεί την χρήση των παρακάτω εικόνων {{:swing: | + | |
| - |  | + | |
| - | - Για κάθε Button οφείλουμε να ορίσουμε τουλάχιστον ένα αντικείμενο τύπου ActionListener. | + | |
| - | - Κάθε Button μπορεί να συνδέεται με ένα //keyboard shortcut//. Για να ορίσετε ένα keyboard shortcut χρησιμοποιήστε τη μέθοδο '' | + | |
| - | + | ||
| - | ===== Η κλάση JCheckBox ===== | + | |
| - | + | ||
| - | Ένα //check box// έχει δύο δυνατές καταστάσεις //α)// επιλεγμένο και //β)// απο-επιλεγμένο. Κάθε //check box// υλοποιείται από την κλάση | + | |
| - | + | ||
| - | <code java CheckBoxDemo.java> | + | |
| - | package components; | + | |
| - | + | ||
| - | import java.awt.*; | + | |
| - | import java.awt.event.*; | + | |
| - | import javax.swing.*; | + | |
| - | + | ||
| - | /* | + | |
| - | * CheckBoxDemo.java requires 16 image files in the images/ | + | |
| - | * directory: | + | |
| - | * geek-----.gif, | + | |
| - | * geek-cg--.gif, | + | |
| - | */ | + | |
| - | public class CheckBoxDemo extends JPanel | + | |
| - | implements ItemListener { | + | |
| - | JCheckBox chinButton; | + | |
| - | JCheckBox glassesButton; | + | |
| - | JCheckBox hairButton; | + | |
| - | JCheckBox teethButton; | + | |
| - | + | ||
| - |  | + | |
| - | * 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. | + | |
| - | + | ||
| - |  | + | |
| - | + | ||
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - | + | ||
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - | + | ||
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - |  | + | |
| - | + | ||
| - |  | + | |
| - | */ | + | |
| - | + | ||
| - | StringBuffer choices; | + | |
| - | JLabel pictureLabel; | + | |
| - | + | ||
| - | public CheckBoxDemo() { | + | |
| - | super(new BorderLayout()); | + | |
| - | + | ||
| - | //Create the check boxes. | + | |
| - | chinButton = new JCheckBox(" | + | |
| - | chinButton.setMnemonic(KeyEvent.VK_C); | + | |
| - | chinButton.setSelected(true); | + | |
| - | + | ||
| - | glassesButton = new JCheckBox(" | + | |
| - | glassesButton.setMnemonic(KeyEvent.VK_G); | + | |
| - | glassesButton.setSelected(true); | + | |
| - | + | ||
| - | hairButton = new JCheckBox(" | + | |
| - | hairButton.setMnemonic(KeyEvent.VK_H); | + | |
| - | hairButton.setSelected(true); | + | |
| - | + | ||
| - | teethButton = new JCheckBox(" | + | |
| - | teethButton.setMnemonic(KeyEvent.VK_T); | + | |
| - | teethButton.setSelected(true); | + | |
| - | + | ||
| - | //Register a listener for the check boxes. | + | |
| - | chinButton.addItemListener(this); | + | |
| - | glassesButton.addItemListener(this); | + | |
| - | hairButton.addItemListener(this); | + | |
| - | teethButton.addItemListener(this); | + | |
| - | + | ||
| - | //Indicates what's on the geek. | + | |
| - | choices = new StringBuffer(" | + | |
| - | + | ||
| - | //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, | + | |
| - | add(pictureLabel, | + | |
| - | setBorder(BorderFactory.createEmptyBorder(20, | + | |
| - | } | + | |
| - | + | ||
| - | /** Listens to the check boxes. */ | + | |
| - | public void itemStateChanged(ItemEvent e) { | + | |
| - | int index = 0; | + | |
| - | char c = ' | + | |
| - | 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. */ | + | |
| - | protected static ImageIcon createImageIcon(String path) { | + | |
| - | java.net.URL imgURL = CheckBoxDemo.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. | + | |
| - | JComponent newContentPane = new CheckBoxDemo(); | + | |
| - | 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(); | + | |
| - | } | + | |
| - | }); | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | Θα χρειαστείτε επιπλέον τις φωτογραφίες που περιέχονται στο αρχείο zip {{:swing: | + | |
| - | + | ||
| - | ===== Οι κλάσεις ButtonGroup και JRadioButton ===== | + | |
| + | | Προηγούμενο: | ||
swing/buttons.1431323962.txt.gz · Last modified: 2015/05/11 04:59 (external edit)
                
                