Java common event response method instance summary

  • 2020-04-01 03:26:55
  • OfStack

This example summarizes the common event-response methods in Java, including container class listeners, listener classes, AbstractAction, reflection, and so on. For your reference. Specific methods are as follows:

First of all, In the Java graphical user interface, the steps required to process events are :

1. Create a component (control) that accepts the response
2. Implement relevant event monitoring interface
3. Register the action listener of the event source
4. Event handling when the event is triggered

The corresponding event response can be made in the following centralized manner.

1. Container monitoring  
 
Effect: click the three buttons in the form to achieve the corresponding corresponding time.  
 


import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
//When you declare a class, add "implements ActionListener" to implement the interface to listen to, separated by commas if you want to listen to multiple ways of listening
//Such as "implements ActionListener, KeyListener"
 
class ButtonListener extends JFrame implements ActionListener{ 
 JButton ok, cancel,exit; //Create the component that accepts the response, which is three buttons
 public ButtonListener(String title){ 
 super(title); 
 this.setLayout(new FlowLayout()); 
 ok = new JButton(" determine "); 
 cancel = new JButton(" return "); 
 exit = new JButton(" exit "); 
//The following three statements register listeners for the button
 ok.addActionListener(this);   
 cancel.addActionListener(this); 
 exit.addActionListener(this); 
 getContentPane().add(ok); 
 getContentPane().add(cancel); 
 getContentPane().add(exit); 
} 
 
//Completes event handling when an event is triggered
 public void actionPerformed(ActionEvent e){ 
   if(e.getSource()==ok) 
    System.out.println(" determine "); 
   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); 
 } 
} 
 

Ii. Implementation of monitoring type  
 
Effect: click the three buttons in the form to achieve the corresponding corresponding time.  


import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
class ButtonListener1 extends JFrame { //There is no listening
 JButton ok, cancel,exit; 
 public ButtonListener1(String title){ 
  super(title); 
  this.setLayout(new FlowLayout()); 
  ok = new JButton(" determine "); 
  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); 
 } 
} 
  //Monitor action event
class MyListener implements ActionListener{ 
  public void actionPerformed(ActionEvent e){ 
   if(e.getActionCommand()==" determine ") 
    System.out.println(" determine "); 
   if(e.getActionCommand()==" return ") 
    System.out.println(" return "); 
   if(e.getActionCommand()==" exit ") 
     System.exit(0);; 
  } 
}

Use the AbstractAction class to implement listening

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; 
 
//For this class of subclass AbstractAction, you must implement the actionPerformed () method.
class AbstractEvent extends AbstractAction{ 
  //private static final long serialVersionUID = 1L; 
  AbstractEvent(){ 
  } 
  public void actionPerformed(ActionEvent e){ 
    //A confirmation dialog box pops up
    if (e.getActionCommand()=="open"){ 
      JOptionPane.showMessageDialog(null, " Open the "); 
    }else if (e.getActionCommand()=="close"){ 
      JOptionPane.showMessageDialog(null, " Shut down "); 
    }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 concrete handler for specifying a MenuEvent is done 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 listening 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 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(); 
  } 
} 

AbstractAction class + reflection method  
 
Effect: click the three buttons in the toolbar, and by the name of the button, the reflection gets the same class implementation response as the name of 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(" The 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 on the  b1"); 
  } 
} 
class b2 implements Action{ 
  public void execute(){ 
    JOptionPane.showMessageDialog(null, " Click on the  b2"); 
  } 
} 
class b3 implements Action{ 
  public void execute(){ 
    JOptionPane.showMessageDialog(null, " Click on the  b3"); 
  } 
}

The above example is well annotated and should be easy to understand. Hopefully the examples described in this article will be helpful.


Related articles: