A comprehensive introduction to event handling for Java page design

  • 2020-04-01 01:25:51
  • OfStack

When you're good interface design, always need to add the corresponding action to the component, in JAVA have corresponding time processing mechanism, called "listener", add the corresponding to the component to perform an action in the process is called "registration", and "listener" is an interface that contains the corresponding executive function, need to do is the method to realize their functions, and then "register" to components, popular point is mom asked me to buy soy sauce, old mama don't care about I turned the street a few days, will be how to bargain with the boss how to go home, mom needs the end result is that I am able to delivered the soy sauce to the her hand, The process of buying soy sauce in the middle of the process is I realized, I am the event processing mechanism inside the "listener" to accept my mother's instructions, my mother let me buy soy sauce, is the corresponding event "registration" to me, oh, once again perfect embodiment of JAVA implementation and interface separation.

In JAVA component events have an ActionEvent, KeyEvent, FocusEvent, ComponentEvent, MouseEvent, AdjustmentEvent etc., each component support all or part of the event, the event has a corresponding Listener to monitor the occurrence of the event and the realization method of the interface, the programmer to do is to create an event object, realize the functions inside it, and then register it to the corresponding components, demonstrate it by using code below:
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class ButtonDemo { 
private JFrame 
frame=new JFrame("ButtonDemo"); 
private JButton 
b1 = new JButton(" button 1"), 
b2 = new JButton(" button 2"); 
private JTextField 
txt = new JTextField(10); 
//Here we use an anonymous class to listen for buttons
private ActionListener bl = new ActionListener() { 
//Implement abstract functions in the listening class
public void actionPerformed(ActionEvent e) { 
String name = ((JButton)e.getSource()).getText(); 
txt.setText(name); 
} 
}; 
public ButtonDemo () { 
//Registers the listener object to two buttons
b1.addActionListener(bl); 
b2.addActionListener(bl); 
frame.setLayout(new FlowLayout()); 
frame.add(b1); 
frame.add(b2); 
frame.add(txt); 
frame.setVisible(true); 
frame.setSize(200,150); 
} 
public static void main(String[] args) { 
new ButtonDemo (); 
} 
} 

If there is more than one function in the listening interface, and I only want to implement one of the functions, it is very clear that the program cannot run, because you have to implement all the functions in the interface, can be compiled, the program can run, then what should we do? Well, obviously the JAVA language designers have taken this into account, so there's a guy called "adapter" that implements all the functions in the interface by default. Just inherit the "adapter" class and override the function you're interested in:
 
class MyMouseListener extends MouseAdapter{ 
public void mouseClicked(MouseEvent e){ 
//To perform actions when the mouse is clicked
} 
} 

Related articles: