When you have designed the interface, you always need to add corresponding execution actions to the components. There is a corresponding time processing mechanism in JAVA, called "listener". The process of adding corresponding execution actions to the components is called "registration", in which "Listener" is an interface, which contains corresponding execution functions. What you need to do is to implement the method functions yourself, and then "register" it to the component. In layman's terms, it means that my mother asked me to buy sauce. Oil, my mother doesn’t care how many days I will walk around the streets, how I will bargain with the boss, or how I will go home. The final result that my mother needs is that I can deliver the soy sauce to her hands. The process of buying soy sauce during the semester was realized by me. , I am the "listener" in the event processing mechanism and accepts my mother's instructions. When my mother asks me to buy soy sauce, she "registers" the event to me. Haha, it once again perfectly reflects the separation of implementation and interface in JAVA.
Component events in JAVA include ActionEvent, KeyEvent, FocusEvent, ComponentEvent, MouseEvent, AdjustmentEvent, etc. Each component supports all or part of the events. The corresponding event has a corresponding Listener to monitor the occurrence of the event and implement the interface method, program What the member has to do is to create an event class object, implement the functions in it, and then register it with the corresponding component. Here is a code demonstration:
Copy the code code as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonDemo {
privateJFrame
frame=new JFrame("ButtonDemo");
privateJButton
b1 = new JButton("Button 1"),
b2 = new JButton("Button 2");
privateJTextField
txt = new JTextField(10);
//Here is an anonymous class used to monitor buttons.
private ActionListener bl = new ActionListener() {
//Implement the abstract function in the listening class
public void actionPerformed(ActionEvent e) {
String name = ((JButton)e.getSource()).getText();
txt.setText(name);
}
};
public ButtonDemo () {
//Register the listening object to the 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 obvious that the program cannot run, because you must implement all the functions in the interface before it can be compiled and the program can run. Get up, what should we do? Haha, obviously the JAVA language designers have taken this into consideration, so they provide a guy called "Adapter", which implements all the functions in the interface by default, inherits the "Adapter" class, and overrides the function you are interested in. That's it:
Copy the code code as follows:
class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
//Realize the action when the mouse is clicked
}
}