This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
swing:jbutton [2018/03/27 14:16] |
swing:jbutton [2018/03/27 15:16] gthanos [Action Listeners] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ===== Η κλάση JButton ===== | ||
| + | |||
| + | Ένα αντικείμενο τύπου Button δημιουργείται όπως είδαμε με χρήση της κλάση [[http:// | ||
| + | |||
| + | <code java> | ||
| + | JButton endButton = new JButton(" | ||
| + | endButton.addActionListener(new endProgramActionListener()); | ||
| + | someJFrame.add(endButton); | ||
| + | </ | ||
| + | |||
| + | ,όπου το Button που δημιουργείται γράφει //" | ||
| + | |||
| + | <WRAP tip 80% center round> | ||
| + | Η κλάση JButton συνδεέται με τα κουμπιά που εισάγει ο χρήστης και όχι με τα κουμπιά //minimize, resize, close// που βρίσκονται επάνω στο παράθυρο (για λειτουργικό σύστημα Windows επάνω δεξιά) και εισάγονται από το //window manager// του λειτουργικού συστήματος. | ||
| + | </ | ||
| + | |||
| + | ===== Action Listeners ===== | ||
| + | |||
| + | Όταν ο χρήστης πατάει το ποντίκι του πάνω σε ένα κουμπί ενός παραθύρου δημιουργείται ένα αντικείμενο που αντιστοιχεί στο event που δημιούργησε ο χρήστης. Το αντικείμενο αυτό, λαμβάνεται από ένα άλλο αντικείμενο που λειτουργεί ως ακροατής τέτοιων events και είναι τύπου [[https:// | ||
| + | |||
| + | <code java> | ||
| + | void actionPerformed(ActionEvent e); | ||
| + | </ | ||
| + | |||
| + | <WRAP tip 80% center round> | ||
| + | Η κλήση της συγκεκριμένης μεθόδου γίνεται ασύγχρονα πράγμα που σημαίνει ότι καλείστε να ορίσετε την μέθοδο, | ||
| + | </ | ||
| + | |||
| + | Δείτε, μεταγλωττίστε και τρέξτε το παρακάτω παράδειγμα από [[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 χρησιμοποιήστε τη μέθοδο '' | ||