This article summarizes common event response methods in Java, including container class monitoring, listener class, AbstractAction, reflection, and so on. In order to facilitate everyone's reference. The specific methods are as follows:
First, in the Java graphical user interface, the steps that are necessary to handle an event are :
1. Create the component that accepts the response (control)
2, the implementation of related event monitoring interface
3, register the event source action listener
4, event-triggered event handling
The corresponding response can be made by the following centralized approach.
First, the Container class monitoring
Effects: Click on the three buttons in the form to implement the appropriate time.
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
When declaring a class, add "implements ActionListener" to implement the listening interface, and if you want to listen to a variety of listening methods, then use a comma interval open//such as "Implements Actionlistener,keylistener" Class Buttonlistener extends JFrame implements actionlistener{JButton OK, cancel,exit;//Create a response-only build, which is three buttons public butt
Onlistener (String title) {Super (title);
This.setlayout (New FlowLayout ());
OK = new JButton ("OK");
Cancel = new JButton ("return");
Exit = new JButton ("Exit");
The following three statements register the Listener Ok.addactionlistener (this) separately for the button;
Cancel.addactionlistener (this);
Exit.addactionlistener (this);
Getcontentpane (). Add (OK);
Getcontentpane (). Add (cancel);
Getcontentpane (). Add (exit); The event handling when the event is triggered is handled public void actionperformed (ActionEvent e) {if (E.getsource () ==ok) System.out.println ("OK")
;
if (E.getsource () ==cancel) System.out.println ("Back");
if (E.getsource () ==exit) system.exit (0);; public static void Main (String args[]) {Buttonlistener pd=new ButtoNlistener ("ActionEvent Demo");
Pd.setsize (250,100);
Pd.setvisible (TRUE);
}
}
second, the listening class implements the &NBSP
Effect: Click the three buttons on the form to implement the appropriate time. &NBSP
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Class ButtonListener1 extends JFrame {//here does not realize monitoring JButton OK, cancel,exit;
Public ButtonListener1 (String title) {Super (title);
This.setlayout (New FlowLayout ());
OK = new JButton ("OK");
Cancel = new JButton ("return");
Exit = new JButton ("Exit");
Ok.addactionlistener (New MyListener ());
Cancel.addactionlistener (New MyListener ());
Exit.addactionlistener (New MyListener ());
Getcontentpane (). Add (OK);
Getcontentpane (). Add (cancel);
Getcontentpane (). Add (exit);
public static void Main (String args[]) {Buttonlistener pd=new buttonlistener ("ActionEvent Demo");
Pd.setsize (250,100);
Pd.setvisible (TRUE); }//Listener Action event class MyListener implements actionlistener{public void actionperformed (ActionEvent e) {if (E.geta
Ctioncommand () = = "OK") System.out.println ("OK");
if (e.getactioncommand () = = "Return") System.out.println ("return");
if (e.getactioncommand () = = "Exit") System.exit (0);;
}
}
Third, the use of abstractaction class to achieve monitoring
Effects: Click the menu to respond
Import Java.awt.BorderLayout;
Import java.awt.event.ActionEvent;
Import javax.swing.AbstractAction;
Import javax.swing.Action;
Import Javax.swing.JFrame;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JOptionPane;
This class inherits abstractaction and must implement the Actionperformed () method.
Class Abstractevent extends abstractaction{//private static final long serialversionuid = 1L; Abstractevent () {} public void actionperformed (ActionEvent e) {//Pop-up confirmation dialog box if (e.getactioncommand () = = "Open")
{Joptionpane.showmessagedialog (NULL, "open");
}else if (e.getactioncommand () = "Close") {Joptionpane.showmessagedialog (null, "off");
}else if (e.getactioncommand () = = "Run") {Joptionpane.showmessagedialog (null, "Run");
}else if (e.getactioncommand () = = "Stop") {Joptionpane.showmessagedialog (null, "Stop");
}} public class Testabstractevent {private static JMenuBar menubar; private static JfraMe frame;
The specific handler for the specified menuevent is completed by the Abstractevent class.
Final Action menuevent=new abstractevent ();
Public testabstractevent () {frame=new JFrame ("menu");
Frame.getcontentpane (). setlayout (New BorderLayout ());
Menubar=new JMenuBar ();
JMenu menufile=new jmenu ("file");
Instantiate a menu item and add Listener openaction, JMenuItem menuitemopen=new jmenuitem ("open");
Menuitemopen.addactionlistener (menuevent);
JMenuItem menuitemclose=new JMenuItem ("close");
Menuitemclose.addactionlistener (menuevent);
Menufile.add (Menuitemopen);
Menufile.add (Menuitemclose);
JMenu menutool=new jmenu ("tool");
JMenuItem menuitemrun=new JMenuItem ("Run");
Menuitemrun.addactionlistener (menuevent);
JMenuItem menuitemstop=new JMenuItem ("Stop");
Menuitemstop.addactionlistener (menuevent);
Menutool.add (Menuitemrun);
Menutool.add (Menuitemstop);
Menubar.add (Menufile);
Menubar.add (Menutool);
Menubar.setvisible (TRUE); Frame.add(Menubar,borderlayout.north);
Frame.setsize (400,200);
Frame.setvisible (TRUE);
public static void Main (string[] args) {new testabstractevent ();
}
}
Four, AbstractAction class + reflection method
Effect: Click the three buttons on the toolbar to reflect the response of the class with the same button name by the name of the button. &NBSP
Import Java.awt.BorderLayout;
Import java.awt.event.ActionEvent;
Import javax.swing.*;
Class Viewaction extends abstractaction{private String actionname= "";
Private JFrame Frame=null;
Private Action Action=null; Public viewaction () {} public viewaction (String actionname) {this.
Actionname=actionname;
This.frame=frame; @Override public void actionperformed (ActionEvent e) {Action action=getaction.
ActionName);
Action.execute (); Private Action getaction (String actionname) {try{if (this.action==null) {action action= (action)
Class.forName (ActionName). newinstance ();
This.action=action;
return this.action;
}catch (Exception e) {return null;
}} public class Testae extends JFrame {public JToolBar bar=new JToolBar ();
String buttonname[]={"B1", "B2", "B3"};
Public Testae () {Super ("event"); for (int i=0;i<buttonname.length;i++) {VIEWACtion action=new viewaction (buttonname[i]);
JButton button=new JButton (Buttonname[i]);
Button.addactionlistener (action);
Bar.add (button);
} this.getcontentpane (). Add (Bar,borderlayout.north);
This.setsize (300, 200);
This.setlocationrelativeto (NULL);
This.setvisible (TRUE);
public static void Main (String [] args) {new Testae ();
} interface action{void execute ();
Class B1 implements action{public void execute () {Joptionpane.showmessagedialog (null, "click B1");
The class B2 implements action{public void execute () {Joptionpane.showmessagedialog (null, "clicked B2");
The class B3 implements action{public void execute () {Joptionpane.showmessagedialog (null, "clicked B3");
}
}
The above examples are available in more detailed annotations and should not be difficult to understand. I hope the examples described in this article will be helpful to you.