This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
swing:buttons [2015/05/11 04:28] gthanos created |
swing:buttons [2018/03/27 20:38] gthanos |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Buttons, Check Boxes & Radio Buttons ====== | ====== Buttons, Check Boxes & Radio Buttons ====== | ||
| - | Για την δημιουργία ενός button μπορείτε να χρησιμοποιήσετε μία κλάση από τις απογόνους της κλάσης [[https:// | + | Για την δημιουργία ενός button μπορείτε να χρησιμοποιήσετε μία κλάση από τις απογόνους της κλάσης [[https:// |
| - | ή [[https:// | + | ή [[https:// |
| - | ===== Η κλάση JButton ===== | + | * [[swing: |
| + | * [[swing: | ||
| + | * [[swing: | ||
| - | Δείτε, μεταγλωττίστε και τρέξτε το παρακάτω παράδειγμα από το 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 = 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(); | ||
| - | } | ||
| - | }); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | Το παραπάνω παράδειγμα απαιτεί την χρήση των παρακάτω εικόνων {{: | ||
| + | | Προηγούμενο: | ||