The Java event-handling mechanism is what I should be building on swing, because in the SE phase, the Java event processing is to listen to the graphical interface, I said the general, of course, there are special, for events, for I should not be unfamiliar with the significance of this event and B/ s development inside the event is the same, the same also has the event source, event monitoring and event response, this mechanism is to better complete human-computer interaction, this thing is also in the drawing of the focus of anti-technology, the principle is not difficult to understand, difficult to understand is the underlying factors of event processing, for this, the same, You should finish the function first and then do some understanding. Here are some of the code that I learned in the process:
Package Test;import Java.awt.*;import javax.swing.*;//import java.awt.event.*;import java.awt.event.ActionEvent; Import java.awt.event.actionlistener;//time processing mechanism to use the package import java.awt.event.*;
public class Shijian extends JFrame implements Actionlistener{jpanel MP = NULL; JButton Jb1=null; JButton jb2=null;public static void Main (string[] args) {Shijian p1=new Shijian (); Public Shijian () {mp=new JPanel (); Jb1=new JButton ("Black"); Jb2=new JButton ("Red"); This.add (Jb1,borderlayout.north); Mp.setbackground (Color.Black); This.add (MP) This.add (Jb2,borderlayout.south);//Register a listener Jb1.addactionlistener (this ); Jb2.addactionlistener (this); Jb1.setactioncommand ("AA"); Jb2.setactioncommand ("BB"); This.setsize (200,150); This.setdefaultcloseoperation (Jframe.exit_on_close); this.setvisible (true);} @Overridepublic void actionperformed (ActionEvent e) {if (E.getactioncommand (). Equals ("AA")) {System.out.println (" You clicked on the Black ");} else if (E.getactioncommand () Equals ("BB")) {System.out.println ("you clicked Red");} else {System.out.print ("you clicked Other");}}}
The basics of Java's event handling mechanism