/** * Java Event Listener processing--Self class implementation ActionListener interface, as Event listener * * @author codebrother */class EventListener1 extends JFrame implements A Ctionlistener {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 ("Pop 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, Btdialog; 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 ("Pop Window"); Add Event Listener object (Object oriented idea) Btblue.addactionlistener (new Coloreventlistener ()); Btdialog.addactionlistener (New Dialogeventlistener ()); Add (Btblue); Add (Btdialog); SetVisible (TRUE); Setdefaultcloseoperation (Jframe.exit_on_close); }//Internal class Coloreventlistener, implementing ActionListener Interface class Coloreventlistener implements ActionListener {@Override Publi c void actionperformed (ActionEvent e) {Container c = Getcontentpane (); C.setbackground (Color.Blue); }}//internal class Dialogeventlistener, implementing 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 internal class processing * * @author codebrother */class EventListener2 extends JFrame {private JButton btblue, BT Dialog; Public EventListener2 () {settitle ("Java GUI Event Listener processing"); SetBounds (100, 100, 500, 350); SetLayout (New FlowLayout ()); Btblue = new JButton ("Blue"); Btdialog = new JButton ("Pop Window"); Add Event Listener (anonymous class here) Btblue.addactionlistener (new ActionListener () {//Event handling @Override public void action Performed (ActionEvent e) {Container c = Getcontentpane (); C.setbackground (Color.Blue); } }); and add Event listener Btdialog.addactionlistener (new ActionListener () {@Override public void actionperformed (Actionev Ent 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 classesDaniel * * @author codebrother */class EventListener4 extends JFrame {private JButton btblue, Btdialog; Public EventListener4 () {settitle ("Java GUI Event Listener processing"); SetBounds (100, 100, 500, 350); SetLayout (New FlowLayout ()); Btblue = new JButton ("Blue"); Btdialog = new JButton ("Pop Window"); Add the button to the event listener Btblue.addactionlistener (new Coloreventlistener (this)); Btdialog.addactionlistener (New Dialogeventlistener ()); Add (Btblue); Add (Btdialog); SetVisible (TRUE); Setdefaultcloseoperation (Jframe.exit_on_close); }}//External class Coloreventlistener, implement ActionListener interface class Coloreventlistener implements ActionListener {private EventListener4 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 public 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 (); }}
Several ways to add events to Java (reprinted with Codebrother articles)