1 /**
2 * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器3 *4 *@authorcodebrother5 */
6 class EventListener1 extends JFrame implementsActionListener {7 privateJButton btBlue, btDialog;8
9 publicEventListener1() {10 setTitle("Java GUI 事件监听处理");11 setBounds(100, 100, 500, 350);12 setLayout(newFlowLayout());13 btBlue = new JButton("蓝色");14 btDialog = new JButton("弹窗");15
16 //将按钮添加事件监听器
17 btBlue.addActionListener(this);18 btDialog.addActionListener(this);19
20 add(btBlue);21 add(btDialog);22
23 setVisible(true);24 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);25 }26 //***************************事件处理***************************
27 @Override28 public voidactionPerformed(ActionEvent e) {29 if (e.getSource() ==btBlue) {30 Container c =getContentPane();31 c.setBackground(Color.BLUE);32 }33 else if (e.getSource() ==btDialog) {34 JDialog dialog = newJDialog();35 dialog.setBounds(300, 200, 400, 300);36 dialog.setVisible(true);37 }38 }39
40 }41
42 /**
43 * Java事件监听处理——内部类处理44 *45 *@authorcodebrother46 */
47
48 class EventListener3 extendsJFrame {49 privateJButton btBlue, btDialog;50
51 //构造方法
52 publicEventListener3() {53 setTitle("Java GUI 事件监听处理");54 setBounds(100, 100, 500, 350);55 setLayout(newFlowLayout());56 btBlue = new JButton("蓝色");57 btDialog = new JButton("弹窗");58 //添加事件监听器对象(面向对象思想)
59 btBlue.addActionListener(newColorEventListener());60 btDialog.addActionListener(newDialogEventListener());61
62 add(btBlue);63 add(btDialog);64 setVisible(true);65 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);66 }67 //内部类ColorEventListener,实现ActionListener接口
68 class ColorEventListener implementsActionListener {69 @Override70 public voidactionPerformed(ActionEvent e) {71 Container c =getContentPane();72 c.setBackground(Color.BLUE);73 }74 }75 //内部类DialogEventListener,实现ActionListener接口
76 class DialogEventListener implementsActionListener {77 @Override78 public voidactionPerformed(ActionEvent e) {79 JDialog dialog = newJDialog();80 dialog.setBounds(300, 200, 400, 300);81 dialog.setVisible(true);82 }83 }84
85 }86
87
88
89 /**
90 * Java事件监听处理——匿名内部类处理91 *92 *@authorcodebrother93 */
94 class EventListener2 extendsJFrame {95 privateJButton btBlue, btDialog;96
97 publicEventListener2() {98 setTitle("Java GUI 事件监听处理");99 setBounds(100, 100, 500, 350);100 setLayout(newFlowLayout());101
102 btBlue = new JButton("蓝色");103 btDialog = new JButton("弹窗");104
105 //添加事件监听器(此处即为匿名类)
106 btBlue.addActionListener(newActionListener() {107 //事件处理
108 @Override109 public voidactionPerformed(ActionEvent e) {110 Container c =getContentPane();111 c.setBackground(Color.BLUE);112 }113 });114
115 //并添加事件监听器
116 btDialog.addActionListener(newActionListener() {117 @Override118 public voidactionPerformed(ActionEvent e) {119 JDialog dialog = newJDialog();120 dialog.setBounds(300, 200, 400, 300);121 dialog.setVisible(true);122 }123 });124
125 add(btBlue);126 add(btDialog);127 setVisible(true);128 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);129 }130
131 }132
133 /**
134 * Java事件监听处理——外部类处理135 *136 *@authorcodebrother137 */
138 class EventListener4 extendsJFrame {139 privateJButton btBlue, btDialog;140
141 publicEventListener4() {142 setTitle("Java GUI 事件监听处理");143 setBounds(100, 100, 500, 350);144 setLayout(newFlowLayout());145 btBlue = new JButton("蓝色");146 btDialog = new JButton("弹窗");147 //将按钮添加事件监听器
148 btBlue.addActionListener(new ColorEventListener(this));149 btDialog.addActionListener(newDialogEventListener());150
151 add(btBlue);152 add(btDialog);153 setVisible(true);154 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);155 }156
157 }158 //外部类ColorEventListener,实现ActionListener接口
159 class ColorEventListener implementsActionListener {160 privateEventListener4 el;161 ColorEventListener(EventListener4 el) {162 this.el =el;163 }164 @Override165 public voidactionPerformed(ActionEvent e) {166 Container c =el.getContentPane();167 c.setBackground(Color.BLUE);168 }169 }170 //外部类DialogEventListener,实现ActionListener接口
171 class DialogEventListener implementsActionListener {172 @Override173 public voidactionPerformed(ActionEvent e) {174 JDialog dialog = newJDialog();175 dialog.setBounds(300, 200, 400, 300);176 dialog.setVisible(true);177 }178 }179
180 public classActionListenerTest181 {182 public static voidmain(String args[])183 {184 newEventListener2();185 }186 }