GUI-AWT event handling
The introduction of event handling mechanism is to respond to the user's behavior.
Previously, after creating the Frame window, click X, but it cannot be closed.
Because in AWT, all events must be handled by a specific object (event listener), the Frame and component itself are just a framework and cannot respond.
Loading and warehousing
import java.awt.event.*;
During event processing, there are three objects.
Event - an event that encapsulates a specific event (usually an operation of the user) that occurs on a GUI component
Event Source - Event Source, usually each component, such as window and button
Event Listener -- is responsible for listening to the events of the event occurrence source and giving a response
The processing flow of these three
External action - trigger event - identify event source - generate event object - pass this event in - event listener - call event handler to respond.
Event listener -- registers the event listener with the event source
For example: how to close
package test1; import java.awt.*; import java.awt.event.*; public class Class1{ public static void main(String[] args){ //Create Frame object window Frame f = new Frame("Frame window"); f.setSize(300,200); f.setVisible(true); //new a listener close LC ListenerClose lc = new ListenerClose(); //Add this lc to the window f.addWindowListener(lc); }} //Connect WindowListener with listener close class ListenerClose implements WindowListener{ //Implement the interface built-in method (rewrite): windowClosing() to close the window. public void windowClosing(WindowEvent e){ //Get this window e Window w = e.getWindow(); //Set window invisible and release = close window w.setVisible(false); w.dispose(); } //Because it is an inherited interface, the rest must be implemented public void windowOpened(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowActivated(WindowEvent e){} public void windowDeactivated(WindowEvent e){} }
malpractice
Because the interface is used, seven methods need to be implemented
Event Adapter
I only wanted to do one thing, but I had to write seven more methods. I'm certainly not happy.
After all, sojia only wants to rewrite the windowClosing () method and just want to close the window.
Principle:
The event adapter is an empty implementation of the listener interface, that is, the event adapter implements the listener interface and provides an implementation for each method in the interface, but there is no code in the method body, which is an empty implementation. Now, you can write. I've arranged other methods for you. Although it's empty, it can be realized after writing.
All listener interfaces that contain multiple methods have a corresponding adapter.
following.
Listener ----> Adapter
Listener interface:
ContainerListener FocusListener ComponentListener KeyListener MouseListener MouseMotionListener WindowListener
Replace the adapter with the listener - > adapter
package test1; import java.awt.*; import java.awt.event.*; public class Class1{ public static void main(String[] args){ //Create a Frame object and new a window Frame f = new Frame("Frame window"); f.setSize(300,200); f.setVisible(true); ListenerClose lc = new ListenerClose(); f.addWindowListener(lc); } } //Inherit WindowAdapter with ListenerClose and override windowClosing() class ListenerClose extends WindowAdapter{ public void windowClosing(WindowEvent e){ Window w = e.getWindow(); w.setVisible(false); w.dispose(); } }
Anonymous inner classes implement event handling
Now, the event adapter has been used, which is relatively concise.
However, in addition to the main method, you still need to create more than one class. Can you be more concise?
package test1; import java.awt.*; import java.awt.event.*; public class Class1{ public static void main(String[] args){ //Create a Frame object and new a window Frame f = new Frame("Frame window"); f.setSize(300,200); f.setLocation(800,400);//Sets the position relative to the screen f.setVisible(true);, //Write anonymous classes directly in the parameters f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ Window w = e.getWindow(); w.setVisible(false); w.dispose(); } }); }}
Common event classification
For example:
Window event, mouse, keyboard, action
Method of window event WindowListener interface
All changes about a window, such as opening and closing
void windowIconified(WindowEvent e) //Triggered when the window is minimized void windowDeactivated(WindowEvent e) //Triggered when the window is deselected void windowDeiconified(WindowEvent e) //Triggered when a window is restored from minimization void windowActivated(WindowEvent e) //Triggered when the window is selected void windowOpened(WindowEvent e) //Open window trigger void windowClosed(WindowEvent e) //Close window trigger void windowClosing(WindowEvent e) //Triggered when the window is closing
example
package test1; import java.awt.*; import java.awt.event.*; public class Class1{ public static void main(String[] args){ //Create Frame object Frame f = new Frame("Frame window"); f.setSize(300,200); f.setLocation(800,400);//Sets the position relative to the screen f.setVisible(true); //new WindowAdapter() is passed in as an anonymous class f.addWindowListener(new WindowAdapter(){ public void windowOpened(WindowEvent e){ System.out.println("windowOpened -->The window is opened"); } public void windowIconified(WindowEvent e){ System.out.println("windowIconified -->window minimizing"); } public void windowDeiconified(WindowEvent e){ System.out.println("windowDeiconified --> Window recovery from minimization"); } public void windowDeactivated(WindowEvent e){ System.out.println("windowDeactivated --> Uncheck window "); } public void windowClosing(WindowEvent e){ System.out.println("windowClosing -->The window is closing"); } public void windowClosed(WindowEvent e){ System.out.println("windowClosed -->window closing"); } public void windowActivated(WindowEvent e){ System.out.println("windowActivated -->The window is selected"); } }); }}
windowIconified -->window minimizing windowDeactivated --> Uncheck window windowDeiconified --> Window recovery from minimization windowActivated -->The window is selected windowIconified -->window minimizing windowDeactivated --> Uncheck window windowDeiconified --> Window recovery from minimization windowActivated -->The window is selected windowClosing -->The window is closing windowClosing -->The window is closing windowClosing -->The window is closing windowDeactivated --> Uncheck window
Mouse event MouseEvent
void mouseClicked(MouseEvent e) //Click (press and release) void mousepressed(MouseEvent e) //Press void mouseReleased(MouseEvent e) //release void mouseEnteren(MouseEvent e) //get into void mouseExited(MouseEvent e) //sign out
Mouse and button
package test1; import java.awt.*; import java.awt.event.*; class DrawFrame extends Panel{ public void paint(Graphics g){ g.drawArc(10,10,30,30,300,180); g.drawLine(50,10,70,40); g.drawRect(10,50,30,30); g.drawRoundRect(50,50,30,30,20,20); } } public class Class1 { public static void main(String[] args){ Frame f = new Frame("Frame window"); Panel p = new Panel(); Button b = new Button("Button"); p.add(b); f.add(p); f.setSize(300,200); f.setLocation(500,200); f.setVisible(true); b.addMouseListener(new MouseListener(){ public void mouseReleased(MouseEvent e){ System.out.println("mouseReleased --> Mouse release"); } public void mousePressed(MouseEvent e){ System.out.println("mousePressed --> Mouse down"); } public void mouseExited(MouseEvent e){ System.out.println("mouseExited --> Mouse away"); } public void mouseEntered(MouseEvent e){ System.out.println("mouseEntered --> Mouse entry"); } public void mouseClicked(MouseEvent e){ int i = e.getButton(); if( i== MouseEvent.BUTTON1){ System.out.println("mouseClicked --> Left click" + e.getClickCount() + "second"); }else if( i== MouseEvent.BUTTON3){ System.out.println("mouseClicked --> Right click" + e.getClickCount() + "second"); }else{ System.out.println("mouseClicked --> Press roller" + e.getClickCount() + "second"); } } }); }}
Keyboard events
KeyListener void KeyTyped(KeyEvent e) Called when a key is struck void KeyPressed(KeyEvent e) Called when pressed void KeyReleased(KeyEvent e) Call upon sending
char getKeyChar() Returns the entered character, only for KeyTyped()Responsive int getKeyCode() Returns the key code of the input character static String getKeyText(int KeyCode) Returns information about this key, for example F1,H
example
package test1; import java.awt.*; import java.awt.event.*; public class Class1 { public static void main(String[] args){ Frame f = new Frame("Frame window"); Panel p = new Panel(); TextField tf = new TextField(10); //Create text box p.add(tf); f.add(p); f.setSize(300,200); f.setLocation(500,200); f.setVisible(true); tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ System.out.println("keyPressed --> keyboard" + KeyEvent.getKeyText(e.getKeyCode()) + "Key press"); } public void keyReleased(KeyEvent e){ System.out.println("keyReleased --> keyboard" + KeyEvent.getKeyText(e.getKeyCode()) + "Key release"); } public void keyTyped(KeyEvent e){ System.out.println("keyTyped --> The keyboard input is:" + e.getKeyChar()); } }); }}
keyReleased --> keyboard A Key release keyTyped --> The keyboard input is: a keyReleased --> keyboard Shift Key release keyPressed --> keyboard Shift Key press keyPressed --> keyboard A Key press keyTyped --> The keyboard input is: A keyReleased --> keyboard A Key release keyReleased --> keyboard Shift Key release
Action event
If you want to make a button meaningful, you need to use action events.
In the event processing of AWT, the action event is different from the first three events. It does not represent a specific action, but only represents that an action has occurred,.
For example, if you want to assign a paragraph, you can assign it through the right mouse button and Ctrl+C on the keyboard, but we don't need to know how to assign it. As long as the copy operation is carried out, this action event is triggered.
The ActionListener of Java is an event listening interface for handling actions.