swing:window_events
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| swing:window_events [2015/05/16 17:40] – gthanos | swing:window_events [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| </ | </ | ||
| - | Στην πραγματικότητα αυτό που συμβαίνει κάθε φορά που πατάτε ένα από τα κουμπιά που συνδέονται με την λειτουργία του παραθύρου (στο δεξιό ή το αριστερό επάνω μέρος του παραθύρου - εξαρτάται από το λειτουργικό σύστημα) παράγεται ένα [[fhttp:// | + | Στην πραγματικότητα αυτό που συμβαίνει κάθε φορά που πατάτε ένα από τα κουμπιά που συνδέονται με την λειτουργία του παραθύρου (στο δεξιό ή το αριστερό επάνω μέρος του παραθύρου - εξαρτάται από το λειτουργικό σύστημα) |
| + | |||
| + | <code java> | ||
| + | void windowActivated(WindowEvent e) | ||
| + | //Invoked when the Window is set to be the active Window. | ||
| + | |||
| + | void windowClosed(WindowEvent e) | ||
| + | //Invoked when a window has been closed as the result of calling dispose on the window. | ||
| + | |||
| + | void windowClosing(WindowEvent e) | ||
| + | //Invoked when the user attempts to close the window from the window' | ||
| + | |||
| + | void windowDeactivated(WindowEvent e) | ||
| + | //Invoked when a Window is no longer the active Window. | ||
| + | |||
| + | void windowDeiconified(WindowEvent e) | ||
| + | //Invoked when a window is changed from a minimized to a normal state. | ||
| + | |||
| + | void windowIconified(WindowEvent e) | ||
| + | //Invoked when a window is changed from a normal to a minimized state. | ||
| + | |||
| + | void windowOpened(WindowEvent e) | ||
| + | //Invoked the first time a window is made visible. | ||
| + | </ | ||
| + | |||
| + | Όταν πατάτε το εικονίδιο σμίκρυνσης του παραθύρου ή πατάτε το συνδυασμό πλήκτρων //ALT+TAB// τότε παράθυρο φεύγει από το προσκήνιο. Αντίστοιχα, | ||
| + | <code java> | ||
| + | void windowGainedFocus(WindowEvent e) | ||
| + | //Invoked when the Window is set to be the focused Window, which means that the Window, or one of its subcomponents, | ||
| + | void windowLostFocus(WindowEvent e) | ||
| + | //Invoked when the Window is no longer the focused Window, which means that keyboard events will no longer be delivered to the Window or any of its subcomponents. | ||
| + | </ | ||
| + | |||
| + | Δείτε το παρακάτω παράδειγμα (από το site της Oracle) το οποίο συνοψίζει την χρήση των παραπάνω δύο interfaces. | ||
| + | <code java WindowEventDemo.java> | ||
| + | package events; | ||
| + | |||
| + | import java.awt.*; | ||
| + | import java.awt.event.*; | ||
| + | import javax.swing.*; | ||
| + | |||
| + | public class WindowEventDemo extends JFrame | ||
| + | implements WindowListener, | ||
| + | WindowFocusListener { | ||
| + | static final String newline = System.getProperty(" | ||
| + | static final String space = " | ||
| + | static WindowEventDemo frame = new WindowEventDemo(" | ||
| + | JTextArea display; | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | /* Use an appropriate Look and Feel */ | ||
| + | try { | ||
| + | // | ||
| + | // | ||
| + | UIManager.setLookAndFeel(" | ||
| + | } catch (UnsupportedLookAndFeelException ex) { | ||
| + | ex.printStackTrace(); | ||
| + | } catch (IllegalAccessException ex) { | ||
| + | ex.printStackTrace(); | ||
| + | } catch (InstantiationException ex) { | ||
| + | ex.printStackTrace(); | ||
| + | } catch (ClassNotFoundException ex) { | ||
| + | ex.printStackTrace(); | ||
| + | } | ||
| + | /* Turn off metal' | ||
| + | UIManager.put(" | ||
| + | |||
| + | //Schedule a job for the event dispatch thread: | ||
| + | //creating and showing this application' | ||
| + | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
| + | public void run() { | ||
| + | createAndShowGUI(); | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * 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. | ||
| + | frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | ||
| + | |||
| + | //Set up the content pane. | ||
| + | frame.addComponentsToPane(); | ||
| + | |||
| + | //Display the window. | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | } | ||
| + | |||
| + | private void addComponentsToPane() { | ||
| + | display = new JTextArea(); | ||
| + | display.setEditable(false); | ||
| + | JScrollPane scrollPane = new JScrollPane(display); | ||
| + | scrollPane.setPreferredSize(new Dimension(500, | ||
| + | getContentPane().add(scrollPane, | ||
| + | |||
| + | addWindowListener(this); | ||
| + | addWindowFocusListener(this); | ||
| + | } | ||
| + | |||
| + | public WindowEventDemo(String name) { | ||
| + | super(name); | ||
| + | } | ||
| + | |||
| + | public void windowClosing(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | //A pause so user can see the message before | ||
| + | //the window actually closes. | ||
| + | ActionListener task = new ActionListener() { | ||
| + | boolean alreadyDisposed = false; | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | if (frame.isDisplayable()) { | ||
| + | alreadyDisposed = true; | ||
| + | frame.dispose(); | ||
| + | } | ||
| + | } | ||
| + | }; | ||
| + | Timer timer = new Timer(500, task); //fire every half second | ||
| + | timer.setInitialDelay(2000); | ||
| + | timer.setRepeats(false); | ||
| + | timer.start(); | ||
| + | } | ||
| + | |||
| + | public void windowClosed(WindowEvent e) { | ||
| + | //This will only be seen on standard output. | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | public void windowOpened(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | public void windowIconified(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | public void windowDeiconified(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | public void windowActivated(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | public void windowDeactivated(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | public void windowGainedFocus(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | public void windowLostFocus(WindowEvent e) { | ||
| + | displayMessage(" | ||
| + | } | ||
| + | |||
| + | private void displayMessage(String msg) { | ||
| + | display.append(msg + newline); | ||
| + | System.out.println(msg); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
swing/window_events.1431798047.txt.gz · Last modified: 2015/05/16 16:40 (external edit)
