This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
swing:jbutton [2018/03/23 11:29] gthanos created |
swing:jbutton [2018/03/27 14:16] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== Η κλάση JButton ===== | ||
| - | |||
| - | Δείτε, μεταγλωττίστε και τρέξτε το παρακάτω παράδειγμα από [[https:// | ||
| - | <code java ButtonDemo.java> | ||
| - | 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; | ||
| - | import java.awt.event.MouseAdapter; | ||
| - | |||
| - | |||
| - | /* | ||
| - | * ButtonDemo.java requires the following files: | ||
| - | | ||
| - | | ||
| - | | ||
| - | */ | ||
| - | public class ButtonDemo extends JPanel | ||
| - | implements ActionListener { | ||
| - | protected JButton b1, b2, b3; | ||
| - | |||
| - | public ButtonDemo() { | ||
| - | ImageIcon leftButtonIcon = new ImageIcon(" | ||
| - | ImageIcon middleButtonIcon = new ImageIcon(" | ||
| - | ImageIcon rightButtonIcon = new ImageIcon(" | ||
| - | |||
| - | b1 = new JButton(" | ||
| - | b1.setVerticalTextPosition(AbstractButton.CENTER); | ||
| - | b1.setHorizontalTextPosition(AbstractButton.LEFT); | ||
| - | b1.setMnemonic(KeyEvent.VK_D); | ||
| - | b1.setActionCommand(" | ||
| - | |||
| - | b2 = new JButton(" | ||
| - | b2.setVerticalTextPosition(AbstractButton.BOTTOM); | ||
| - | b2.setHorizontalTextPosition(AbstractButton.CENTER); | ||
| - | b2.setMnemonic(KeyEvent.VK_M); | ||
| - | b2.setActionCommand(" | ||
| - | |||
| - | b3 = new JButton(" | ||
| - | b3.setMnemonic(KeyEvent.VK_E); | ||
| - | b3.setActionCommand(" | ||
| - | b3.setEnabled(false); | ||
| - | |||
| - | //Listen for actions on buttons 1 and 3. | ||
| - | b1.addActionListener(this); | ||
| - | b2.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 if (" | ||
| - | b2.setEnabled(true); | ||
| - | b1.setEnabled(true); | ||
| - | b3.setEnabled(false); | ||
| - | } else if (" | ||
| - | b2.setEnabled(false); | ||
| - | b1.setEnabled(false); | ||
| - | b3.setEnabled(true); | ||
| - | } | ||
| - | } | ||
| - | | ||
| - | /** | ||
| - | * 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); | ||
| - | |||
| - | //Size and 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(); | ||
| - | } | ||
| - | }); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | Το παραπάνω παράδειγμα απαιτεί την χρήση των παρακάτω εικόνων {{: | ||
| - | - Κάθε αντικείμενο [[https:// | ||
| - | - Για κάθε [[https:// | ||
| - | - Το //click// ενός **JButton** μπορεί να ανατεθεί σε ένα //keyboard shortcut//. Για να ορίσετε ένα keyboard shortcut χρησιμοποιήστε τη μέθοδο '' | ||