User Tools

Site Tools


swing:buttons

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revision Both sides next revision
swing:buttons [2015/05/11 04:28]
gthanos created
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://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html|javax.swing.AbstractButton]], όπως οι [[https://docs.oracle.com/javase/8/docs/api/javax/swing/JButton.html|JButton]], [[https://docs.oracle.com/javase/8/docs/api/javax/swing/JCheckBox.html|JCheckBox]] +Για την δημιουργία ενός button μπορείτε να χρησιμοποιήσετε μία κλάση από τις απογόνους της κλάσης [[https://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html|javax.swing.AbstractButton]], όπως οι [[https://docs.oracle.com/javase/8/docs/api/javax/swing/JButton.html|JButton]], [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBox.html|JCheckBox]] 
- ή [[https://docs.oracle.com/javase/8/docs/api/javax/swing/JRadioButton.html|JRadioButton]]. Άλλες υποκλάσεις της κλάσης //AbstractButton// είναι οι [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenuItem.html|JMenuItem]], [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBoxMenuItem.html|JCheckBoxMenuItem]] και [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JRadioButtonMenuItem.html|JRadioButtonMenuItem]] τις οποίες θα δούμε στη συνέχεια.+ ή [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JRadioButton.html|JRadioButton]]. Άλλες υποκλάσεις της κλάσης //AbstractButton// είναι οι [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenuItem.html|JMenuItem]], [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBoxMenuItem.html|JCheckBoxMenuItem]] και [[https://docs.oracle.com/javase/7/docs/api/javax/swing/JRadioButtonMenuItem.html|JRadioButtonMenuItem]] τις οποίες θα δούμε στη συνέχεια.
  
-===== Η κλάση 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/ActionEvent.html|java.awt.events.ActionEvent]] το οποίο λαμβάνεται από ένα αντικείμενο τύπου [[http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html|ActionListener]] που συνδέεται με το button. Παράλληλα, δημιουργούνται και δύο αντικείμενα τύπου [[http://docs.oracle.com/javase/7/docs/api/java/awt/event/ItemEvent.html|java.awt.events.ItemEvent]] ένα αντικείμενο για το button που επιλέχθηκε και ένα αντικείμενο για το button που απο-επιλέχθηκε (εάν υπήρξε τέτοιο), τα οποία λαμβάνονται από ένα αντικείμενο του τύπου [[http://docs.oracle.com/javase/7/docs/api/java/awt/event/ItemListener.html|ItemListener]]. Συνήθως χρησιμοποιούμε ItemListeners όπως φαίνεται στο παρακάτω παράδειγμα από το site της Oracle. 
- ButtonDemo.java requires the following files: + 
-   images/right.gif +<code java RadioButtonDemo.java> 
-   images/middle.gif + 
-   images/left.gif+import java.awt.*; 
 +import java.awt.event.*
 +import javax.swing.*; 
 + 
 +/* 
 + * RadioButtonDemo.java requires these files: 
 +   images/Bird.gif 
 +   images/Cat.gif 
 +   images/Dog.gif 
 +   images/Rabbit.gif 
 +   images/Pig.gif
  */  */
-public class ButtonDemo extends JPanel +public class RadioButtonDemo extends JPanel 
-            implements ActionListener { +               implements ActionListener { 
-  protected JButton b1, b2, b3;+  static String birdString = "Bird"; 
 +  static String catString = "Cat"; 
 +  static String dogString = "Dog"; 
 +  static String rabbitString = "Rabbit"; 
 +  static String pigString = "Pig";
  
-  public ButtonDemo() { +  JLabel picture;
-    ImageIcon leftButtonIcon = createImageIcon("images/right.gif"); +
-    ImageIcon middleButtonIcon = createImageIcon("images/middle.gif"); +
-    ImageIcon rightButtonIcon = createImageIcon("images/left.gif");+
  
-    b1 = new JButton("Disable middle button", leftButtonIcon); +  public RadioButtonDemo() { 
-    b1.setVerticalTextPosition(AbstractButton.CENTER); +    super(new BorderLayout());
-    b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales +
-    b1.setMnemonic(KeyEvent.VK_D); +
-    b1.setActionCommand("disable");+
  
-    b2 = new JButton("Middle button", middleButtonIcon); +    //Create the radio buttons. 
-    b2.setVerticalTextPosition(AbstractButton.BOTTOM); +    JRadioButton birdButton = new JRadioButton(birdString); 
-    b2.setHorizontalTextPosition(AbstractButton.CENTER); +    birdButton.setMnemonic(KeyEvent.VK_B); 
-    b2.setMnemonic(KeyEvent.VK_M);+    birdButton.setActionCommand(birdString); 
 +    birdButton.setSelected(true);
  
-    b3 = new JButton("Enable middle button", rightButtonIcon); +    JRadioButton catButton = new JRadioButton(catString); 
-    //Use the default text position of CENTER, TRAILING (RIGHT). +    catButton.setMnemonic(KeyEvent.VK_C); 
-    b3.setMnemonic(KeyEvent.VK_E); +    catButton.setActionCommand(catString);
-    b3.setActionCommand("enable"); +
-    b3.setEnabled(false);+
  
-    //Listen for actions on buttons 1 and 3. +    JRadioButton dogButton = new JRadioButton(dogString); 
-    b1.addActionListener(this); +    dogButton.setMnemonic(KeyEvent.VK_D); 
-    b3.addActionListener(this);+    dogButton.setActionCommand(dogString);
  
-    b1.setToolTipText("Click this button to disable the middle button."); +    JRadioButton rabbitButton = new JRadioButton(rabbitString); 
-    b2.setToolTipText("This middle button does nothing when you click it."); +    rabbitButton.setMnemonic(KeyEvent.VK_R); 
-    b3.setToolTipText("Click this button to enable the middle button.");+    rabbitButton.setActionCommand(rabbitString);
  
-    //Add Components to this containerusing the default FlowLayout+    JRadioButton pigButton = new JRadioButton(pigString); 
-    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("images/" 
 +                       + birdString 
 +                       + ".gif")); 
 + 
 +    //The preferred size is hard-coded to be the width of the 
 +    //widest image and the height of the tallest image. 
 +    //A real program would compute this
 +    picture.setPreferredSize(new Dimension(177122)); 
 + 
 + 
 +    //Put the radio buttons in a column in a panel
 +    JPanel radioPanel = new JPanel(new GridLayout(0, 1)); 
 +    radioPanel.add(birdButton); 
 +    radioPanel.add(catButton); 
 +    radioPanel.add(dogButton); 
 +    radioPanel.add(rabbitButton); 
 +    radioPanel.add(pigButton); 
 + 
 +    add(radioPanel, BorderLayout.LINE_START); 
 +    add(picture, BorderLayout.CENTER); 
 +    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
   }   }
  
 +  /** Listens to the radio buttons. */
   public void actionPerformed(ActionEvent e) {   public void actionPerformed(ActionEvent e) {
-    if ("disable".equals(e.getActionCommand())) { +    picture.setIcon(createImageIcon("images/" 
-      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("ButtonDemo");+    JFrame frame = new JFrame("RadioButtonDemo");
     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.
-    ButtonDemo newContentPane = new ButtonDemo();+    JComponent newContentPane = new RadioButtonDemo();
     newContentPane.setOpaque(true); //content panes must be opaque     newContentPane.setOpaque(true); //content panes must be opaque
     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:
 } }
 </code> </code>
-Το παραπάνω παράδειγμα απαιτεί την χρήση των παρακάτω εικόνων {{:swing:buttondemoimages.zip|}}+ 
 +Για το παραπάνω παράδειγμα θα χρειαστείτε τις εικόνες {{:swing:animals.zip|}}
  
swing/buttons.txt · Last modified: 2018/03/27 19:38 (external edit)