swing:jradiobutton
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| swing:jradiobutton [2018/03/23 12:05] – gthanos | swing:jradiobutton [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| Παράλληλα, | Παράλληλα, | ||
| - | Παρακάτω δίνονται δύο παραλλαγές του ιδίου προγράμματος **α)** με χρήση [[http:// | + | Παρακάτω δίνονται δύο παραλλαγές του ιδίου προγράμματος **α)** με χρήση [[http:// |
| ==== Παράδειγμα χρήσης ActionListener ==== | ==== Παράδειγμα χρήσης ActionListener ==== | ||
| Line 24: | Line 24: | ||
| | | ||
| */ | */ | ||
| - | public class RadioButtonDemo extends JPanel | + | public class RadioButtonDemo extends JPanel { |
| - | | + | |
| static String birdString = " | static String birdString = " | ||
| static String catString = " | static String catString = " | ||
| Line 68: | Line 67: | ||
| //Register a listener for the radio buttons. | //Register a listener for the radio buttons. | ||
| - | birdButton.addActionListener(this); | + | birdButton.addActionListener(listener); |
| - | catButton.addActionListener(this); | + | catButton.addActionListener(listener); |
| - | dogButton.addActionListener(this); | + | dogButton.addActionListener(listener); |
| - | rabbitButton.addActionListener(this); | + | rabbitButton.addActionListener(listener); |
| - | pigButton.addActionListener(this); | + | pigButton.addActionListener(listener); |
| //Set up the picture label. | //Set up the picture label. | ||
| Line 93: | Line 92: | ||
| radioPanel.add(pigButton); | radioPanel.add(pigButton); | ||
| - | add(radioPanel, | + | add(radioPanel, |
| add(picture, | add(picture, | ||
| setBorder(BorderFactory.createEmptyBorder(20, | setBorder(BorderFactory.createEmptyBorder(20, | ||
| } | } | ||
| - | /** Listens to the radio buttons. */ | + | |
| - | public void actionPerformed(ActionEvent e) { | + | |
| - | picture.setIcon(new ImageIcon(e.getActionCommand() + " | + | public void actionPerformed(ActionEvent e) { |
| - | } | + | picture.setIcon(new ImageIcon(e.getActionCommand() + " |
| + | } | ||
| + | }; | ||
| /** | /** | ||
| Line 137: | Line 138: | ||
| ==== Παράδειγμα χρήσης ItemListener ==== | ==== Παράδειγμα χρήσης ItemListener ==== | ||
| - | <code java .java> | + | <code java RadioButtonDemo2.java> |
| import java.awt.*; | import java.awt.*; | ||
| import java.awt.event.*; | import java.awt.event.*; | ||
| Line 150: | Line 151: | ||
| | | ||
| */ | */ | ||
| - | public class RadioButtonDemo2 extends JPanel | + | public class RadioButtonDemo2 extends JPanel { |
| - | | + | |
| static String birdString = " | static String birdString = " | ||
| static String catString = " | static String catString = " | ||
| Line 166: | Line 167: | ||
| JRadioButton birdButton = new JRadioButton(birdString); | JRadioButton birdButton = new JRadioButton(birdString); | ||
| birdButton.setMnemonic(KeyEvent.VK_B); | birdButton.setMnemonic(KeyEvent.VK_B); | ||
| - | birdButton.setActionCommand(birdString); | ||
| birdButton.setSelected(true); | birdButton.setSelected(true); | ||
| JRadioButton catButton = new JRadioButton(catString); | JRadioButton catButton = new JRadioButton(catString); | ||
| catButton.setMnemonic(KeyEvent.VK_C); | catButton.setMnemonic(KeyEvent.VK_C); | ||
| - | catButton.setActionCommand(catString); | ||
| JRadioButton dogButton = new JRadioButton(dogString); | JRadioButton dogButton = new JRadioButton(dogString); | ||
| dogButton.setMnemonic(KeyEvent.VK_D); | dogButton.setMnemonic(KeyEvent.VK_D); | ||
| - | dogButton.setActionCommand(dogString); | ||
| JRadioButton rabbitButton = new JRadioButton(rabbitString); | JRadioButton rabbitButton = new JRadioButton(rabbitString); | ||
| rabbitButton.setMnemonic(KeyEvent.VK_R); | rabbitButton.setMnemonic(KeyEvent.VK_R); | ||
| - | rabbitButton.setActionCommand(rabbitString); | ||
| JRadioButton pigButton = new JRadioButton(pigString); | JRadioButton pigButton = new JRadioButton(pigString); | ||
| pigButton.setMnemonic(KeyEvent.VK_P); | pigButton.setMnemonic(KeyEvent.VK_P); | ||
| - | pigButton.setActionCommand(pigString); | ||
| //Group the radio buttons. | //Group the radio buttons. | ||
| Line 194: | Line 190: | ||
| //Register a listener for the radio buttons. | //Register a listener for the radio buttons. | ||
| - | birdButton.addItemListener(this); | + | birdButton.addItemListener(itemListener); |
| - | catButton.addItemListener(this); | + | catButton.addItemListener(itemListener); |
| - | dogButton.addItemListener(this); | + | dogButton.addItemListener(itemListener); |
| - | rabbitButton.addItemListener(this); | + | rabbitButton.addItemListener(itemListener); |
| - | pigButton.addItemListener(this); | + | pigButton.addItemListener(itemListener); |
| //Set up the picture label. | //Set up the picture label. | ||
| - | picture = new JLabel(new ImageIcon( | + | picture = new JLabel(new ImageIcon(birdString + " |
| - | birdString | + | |
| - | + " | + | |
| //The preferred size is hard-coded to be the width of the | //The preferred size is hard-coded to be the width of the | ||
| Line 219: | Line 213: | ||
| radioPanel.add(pigButton); | radioPanel.add(pigButton); | ||
| - | add(radioPanel, | + | add(radioPanel, |
| add(picture, | add(picture, | ||
| setBorder(BorderFactory.createEmptyBorder(20, | setBorder(BorderFactory.createEmptyBorder(20, | ||
| } | } | ||
| - | | + | |
| - | public void itemStateChanged(ItemEvent e) { | + | public void itemStateChanged(ItemEvent e) { |
| - | if(e.getStateChange() == ItemEvent.SELECTED) { | + | if(e.getStateChange() == ItemEvent.SELECTED) { |
| - | JRadioButton btn = (JRadioButton)e.getItemSelectable(); | + | JRadioButton btn = (JRadioButton)e.getItemSelectable(); |
| - | picture.setIcon(new ImageIcon(btn.getText() + " | + | picture.setIcon(new ImageIcon(btn.getText() + " |
| + | } | ||
| } | } | ||
| - | } | + | }; |
| /** | /** | ||
swing/jradiobutton.1521806725.txt.gz · Last modified: 2018/03/23 12:05 (external edit)
