Three ways to listen for button events in Java

Source: Internet
Author: User

The swing component in Java, where the focus of the button is to listen for events, can be called by JButton's addActionListener () method. This method takes an object that implements the ActionListener interface as a parameter, and the ActionListener interface contains only a actionperformed () method, so if you want to associate the code of the event handling with JButton, There are three ways to do this:

The first type:

public class Button extends MyFrame {
Private JButton
B1 = new JButton ("Button1"),
b2 = new JButton ("Button2");
Private JTextField txt = new JTextField (10);
Class Buttonlistener implements actionlistener{//define inner class to implement event monitoring
public void actionperformed (ActionEvent e) {
Txt.settext (((JButton) E.getsource ()). GetText ());
/*
* String name = ((JButton) E.getsource ()). GetText ();
* Txt.settext (name);
*/
}
}

Private Buttonlistener B = new Buttonlistener ();
Public Button () {
B1.addactionlistener (b);
B2.addactionlistener (b);
This.setlayout (New FlowLayout ());
This.add (B1);
This.add (B2);
This.add (TXT);
}
public static void Main (string[] args) {
Swingutilities.invokelater (New Runnable () {
public void Run () {
New Button ();
}
});
}
}

The second type:

Object B of the ActionListener class is created directly, using an anonymous inner class
Private ActionListener b= New ActionListener () {//define inner classes to implement event snooping
public void actionperformed (ActionEvent e) {
Txt.settext (((JButton) E.getsource ()). GetText ());
/*
* String name = ((JButton) E.getsource ()). GetText ();
* Txt.settext (name);
*/
}

};

The third type:

Create a button's listener directly in the program without the need to introduce a listener


B1.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Txt.settext (((JButton) E.getsource ()). GetText ());
}
});
B2.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Txt.settext ("");
}
});

Three ways to listen for button events in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.