We must strive to survive in the competition. --Sun Yat-sen
What's in this lecture: event handling
To import a package:
Import java.awt.event.*;
The events fall into two main categories: low-level events and advanced events.
1, low-level events include the following several
Event Category |
Interface Name |
Describe |
ComponentEvent Component Events |
Componentlistener |
Occurs when the component size changes, moves, displays, or hides |
ContainerEvent Container Events |
ContainerListener |
occurs When a component is added or removed |
WindowEvent Window Events |
WindowListener |
window is activated, masked, minimized, or closed |
MouseEvent Mouse Events |
Mouseeventlistener |
mouse button is pressed, released, mouse moved or dragged |
KeyEvent keyboard Events |
KeyListener |
A key on the keyboard is pressed or released |
FocusEvent Focus Event |
FocusEvent |
Occurs when the component receives focus or loses focus |
2, advanced events include the following several
Event Category
|
Interface Name
|
Describe
|
ActionEvent Action Events |
ActionListener |
Occurs when a button click, menu selection, list box selection, press Enter in a text field, and so on |
AdjustmentEvent Adjustment Events |
Adjustmentlistener |
Occurs when the user moves the slider on the scroll bar to adjust the value |
ItemEvent Project Events |
ItemListener |
Occurs when the user selects from a set of selection boxes or list boxes |
TextEvent Text Events |
TextListener |
Occurs when the contents of a Text field or text box change |
Example one: Two buttons to implement the corresponding event
public class Text extends JFrame implements Actionlistener{jpanel Mb=null; JButton an1,an2;public static void Main (string[] args) {text t=new text ();} Public Text () {mb=new JPanel (); Mb.setbackground (Color.yellow); An1=new JButton ("Red"); An1.addactionlistener (this);// Register the Monitor An1.setactioncommand ("rea");//Specify the action command an2=new JButton ("Blue"); An2.addactionlistener (this); An2.setactioncommand ("Blue"); This.add (An1,borderlayout.north); This.add (An2,borderlayout.south); This.add (MB);// Set the form Properties This.settitle ("Technology Daniel-Xiao Jin"); This.setlocation (+); this.setsize (400,400); this.setvisible (true); This.setdefaultcloseoperation (jframe.exit_on_close);} public void actionperformed (ActionEvent e) {if (E.getactioncommand (). Equals ("Rea")) {Mb.setbackground (color.red);} else if (E.getactioncommand (). Equals ("Blue")) {Mb.setbackground (Color.Blue);}}
Method Two (you can also get listener events)
public class Text extends JFrame {JPanel mb=null; JButton an1,an2;public static void Main (string[] args) {text t=new text ();} Public Text () {mb=new JPanel (); Mb.setbackground (Color.yellow); An1=new JButton ("Red"); An2=new JButton ("Blue"); MyListener listener=new MyListener (); An1.addactionlistener (listener);//Register for monitoring An1.setactioncommand ("red");// Specify the action command An2.addactionlistener (listener); An2.setactioncommand ("Blue"); This.add (An1,borderlayout.north); This.add (An2,borderlayout.south); This.add (MB);//Set form Properties This.settitle ("Technology Daniel-Xiao Jin"); This.setlocation (300, 300); This.setsize (400,400); this.setvisible (true); This.setdefaultcloseoperation (Jframe.exit_on_close);}} Class MyListener implements Actionlistener{public void actionperformed (ActionEvent e) {if (E.getactioncommand (). equals ("Red")) {System.out.println ("red");} else if (E.getactioncommand (). Equals ("Blue")) {System.out.println ("Blue");}}}
Example two: A rectangle will move up and down around the keyboard
public class Text extends JFrame {mypanel mb=null;public static void Main (string[] args) {text t=new text ();//each definition of a t will produce A corresponding this}public Text () {mb=new mypanel (); This.add (MB); This.addkeylistener (MB);//Add a listening keyboard to listen for objects defined by this class are monitored with MP objects Set the form Properties This.settitle ("Technology Daniel-Xiao Jin"); This.setlocation (+); this.setsize (400,400); this.setvisible (true); This.setdefaultcloseoperation (Jframe.exit_on_close);}} Class Mypanel extends JPanel implements keylistener{int x=100,y=100;public void Paint (Graphics g) {super.paint (g); G.setcolor (color.red); G.fillrect (x, Y, 10, 10);} The key is pressed public void keypressed (KeyEvent e) {//system.out.println ("1"), if (E.getkeycode () ==keyevent.vk_up) {y--;} else if (E.getkeycode () ==keyevent.vk_down) {y++;} else if (E.getkeycode () ==keyevent.vk_left) {x--;} else if (E.getkeycode () ==keyevent.vk_right) {x + +;} Call the repaint () function to redraw the interface This.repaint ();} Key is released public void keyreleased (KeyEvent e) {System.out.println ("key is Freed");} <span style= "color: #ff0000;" A value of the >//key is printed out (the next key and so on will not be executed because it is not printed) </span>public void keytyped (KeyEvent e) {System.out.println ("Other key will be executed, the next key or so will not execute, because no print out");}}
This is where we go, take your time and enjoy it
Study Note 25: Event handling (i)