Several ways to add events to Java (reprinted the Codebrother article, making a slight change):
/** * Java Event Listener processing-Self class implementation ActionListener interface, as Event listener * * @author Codebrother/class EventListener1 extends JFrame Implem
Ents ActionListener {Private JButton btblue, Btdialog;
Public EventListener1 () {settitle ("Java GUI Event Listener processing");
SetBounds (100, 100, 500, 350);
SetLayout (New FlowLayout ());
Btblue = new JButton ("Blue");
Btdialog = new JButton ("flick Window");
Add the button to the event listener Btblue.addactionlistener (this);
Btdialog.addactionlistener (this);
Add (Btblue);
Add (Btdialog);
SetVisible (TRUE);
Setdefaultcloseoperation (Jframe.exit_on_close); }//*************************** event handling *************************** @Override public void actionperformed (ActionEvent e)
{if (e.getsource () = = Btblue) {Container c = Getcontentpane ();
C.setbackground (Color.Blue);
else if (e.getsource () = = Btdialog) {jdialog dialog = new JDialog ();
Dialog.setbounds (300, 200, 400, 300);
Dialog.setvisible (TRUE); }
}
}
/** * Java Event Listener Processing--Internal class processing * * @author Codebrother/class EventListener3 extends JFrame {private JButton btblue, BTDI
Alog;
Construction method Public EventListener3 () {settitle ("Java GUI Event Listener processing");
SetBounds (100, 100, 500, 350);
SetLayout (New FlowLayout ());
Btblue = new JButton ("Blue");
Btdialog = new JButton ("flick Window");
Add Event Listener object (object-oriented thinking) Btblue.addactionlistener (new Coloreventlistener ());
Btdialog.addactionlistener (New Dialogeventlistener ());
Add (Btblue);
Add (Btdialog);
SetVisible (TRUE);
Setdefaultcloseoperation (Jframe.exit_on_close); }//Internal class Coloreventlistener, implement ActionListener interface class Coloreventlistener implements ActionListener {@Override P
ublic void actionperformed (ActionEvent e) {Container c = Getcontentpane ();
C.setbackground (Color.Blue);
}//Internal class Dialogeventlistener, implement ActionListener interface class Dialogeventlistener implements ActionListener {@Override public void actionperformed (ActionEvent e){JDialog dialog = new JDialog ();
Dialog.setbounds (300, 200, 400, 300);
Dialog.setvisible (TRUE); }}/** * Java Event Listener processing-Anonymous inner class processing * @author Codebrother/class EventListener2 extends JFrame {private JButton
Btblue, Btdialog;
Public EventListener2 () {settitle ("Java GUI Event Listener processing");
SetBounds (100, 100, 500, 350);
SetLayout (New FlowLayout ());
Btblue = new JButton ("Blue");
Btdialog = new JButton ("flick Window"); Add Event Listener (here is anonymous Class) Btblue.addactionlistener (new ActionListener () {//Event handling @Override public void AC
Tionperformed (ActionEvent e) {Container c = Getcontentpane ();
C.setbackground (Color.Blue);
}
}); and add Event listener Btdialog.addactionlistener (new ActionListener () {@Override public void actionperformed (actio
Nevent e) {jdialog dialog = new JDialog ();
Dialog.setbounds (300, 200, 400, 300);
Dialog.setvisible (TRUE);
}
}); Add (bTblue);
Add (Btdialog);
SetVisible (TRUE);
Setdefaultcloseoperation (Jframe.exit_on_close); }/** * Java Event Listener Processing--External class processing * * @author Codebrother/class EventListener4 extends JFrame {private JButton Btblu
E, Btdialog;
Public EventListener4 () {settitle ("Java GUI Event Listener processing");
SetBounds (100, 100, 500, 350);
SetLayout (New FlowLayout ());
Btblue = new JButton ("Blue");
Btdialog = new JButton ("flick Window");
Add the button to the event listener Btblue.addactionlistener (this) (new Coloreventlistener);
Btdialog.addactionlistener (New Dialogeventlistener ());
Add (Btblue);
Add (Btdialog);
SetVisible (TRUE);
Setdefaultcloseoperation (Jframe.exit_on_close); }//External class Coloreventlistener, implementing ActionListener Interface class Coloreventlistener implements ActionListener {private eventliste
Ner4 el;
Coloreventlistener (EventListener4 el) {this.el = El;
@Override public void actionperformed (ActionEvent e) {Container c = El.getcontentpane (); C.setbackground (ColOr.
BLUE); }//External class Dialogeventlistener, implementing ActionListener Interface class Dialogeventlistener implements ActionListener {@Override Publi
c void actionperformed (ActionEvent e) {jdialog dialog = new JDialog ();
Dialog.setbounds (300, 200, 400, 300);
Dialog.setvisible (TRUE);
} public class Actionlistenertest {public static void main (String args[]) {new EventListener2 ();
}
}