The examples in this article summarize common event response methods in Java, including container class monitoring, listener classes, AbstractAction, reflection, etc. For your reference. The specific methods are as follows:
First of all, in the Java graphical user interface, the steps necessary to handle events are :
1. Create a component (control) that accepts responses
2. Implement relevant event listening interfaces
3. Register the action listener of the event source
4. Event processing when the event is triggered
Accordingly, incident response can be made in the following centralized manner.
1. Container class monitoring <br/>Effect: Click three buttons in the form to achieve the corresponding response time.
import java.awt.*; import java.awt.event.*; import javax.swing.*; //When declaring a class, add "implements ActionListener" to implement the listening interface. If you want to monitor multiple listening methods, then Separate with commas // For example, "implements ActionListener,KeyListener" class ButtonListener extends JFrame implements ActionListener{ JButton ok, cancel,exit; //Create a component that accepts responses, which is three buttons public ButtonListener(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 listeners for buttons ok.addActionListener(this); cancel.addActionListener(this); exit.addActionListener(this); getContentPane().add(ok); getContentPane().add(cancel); getContentPane().add(exit); } //Complete event processing when the event is triggered public void actionPerformed(ActionEvent e){ if(e.getSource ()==ok) System.out.println("OK"); if(e.getSource()==cancel) System.out.println("Return"); 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); } }
2. Monitoring class realizes the effect: click three buttons in the form to realize the corresponding corresponding time.
import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonListener1 extends JFrame { //There is no implementation of monitoring JButton here 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); } } //Listen to action events class MyListener implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e .getActionCommand()=="OK") System.out.println("OK"); if(e.getActionCommand()=="Return") System.out.println("Return"); if(e.getActionCommand()=="Exit") System.exit(0);; } }
3. Use the AbstractAction class to implement monitoring
Effect: 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, "Close"); }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 specifying 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 a listener for openAction, JMenuItem menuItemopen=new JMenuItem("open"); menuItemopen.addActionListener(MenuEvent); JMenuItem menuItemclose=new JMenuItem("close"); menuItemclose.addActionListener(MenuEvent); menuFile.add(menuItemopen); menuFile.add(menuItemclose); 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(); } }
4. AbstractAction class + reflection method <br/>Effect: Click three buttons on the toolbar, and through the name of the button, reflection will get a class implementation response with the same name as the button.
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(this.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, "b1 was clicked"); } } class b2 implements Action{ public void execute(){ JOptionPane.showMessageDialog(null, " b2 was clicked"); } } class b3 implements Action{ public void execute(){ JOptionPane.showMessageDialog(null, "Clicked b3"); } }
The above examples are well explained and should not be difficult to understand. I hope the examples described in this article will be helpful to everyone.