Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动)
1 /**
2 * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器
3 *
4 * @author codebrother
5 */
6 class EventListener1 extends JFrame implements ActionListener {
7 private JButton btBlue, btDialog;
8
9 public EventListener1() {
10 setTitle("Java GUI 事件监听处理");
11 setBounds(100, 100, 500, 350);
12 setLayout(new FlowLayout());
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 @Override
28 public void actionPerformed(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 = new JDialog();
35 dialog.setBounds(300, 200, 400, 300);
36 dialog.setVisible(true);
37 }
38 }
39
40 }
41
42 /**
43 * Java事件监听处理——内部类处理
44 *
45 * @author codebrother
46 */
47
48 class EventListener3 extends JFrame {
49 private JButton btBlue, btDialog;
50
51 // 构造方法
52 public EventListener3() {
53 setTitle("Java GUI 事件监听处理");
54 setBounds(100, 100, 500, 350);
55 setLayout(new FlowLayout());
56 btBlue = new JButton("蓝色");
57 btDialog = new JButton("弹窗");
58 // 添加事件监听器对象(面向对象思想)
59 btBlue.addActionListener(new ColorEventListener());
60 btDialog.addActionListener(new DialogEventListener());
61
62 add(btBlue);
63 add(btDialog);
64 setVisible(true);
65 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 }
67 // 内部类ColorEventListener,实现ActionListener接口
68 class ColorEventListener implements ActionListener {
69 @Override
70 public void actionPerformed(ActionEvent e) {
71 Container c = getContentPane();
72 c.setBackground(Color.BLUE);
73 }
74 }
75 // 内部类DialogEventListener,实现ActionListener接口
76 class DialogEventListener implements ActionListener {
77 @Override
78 public void actionPerformed(ActionEvent e) {
79 JDialog dialog = new JDialog();
80 dialog.setBounds(300, 200, 400, 300);
81 dialog.setVisible(true);
82 }
83 }
84
85 }
86
87
88
89 /**
90 * Java事件监听处理——匿名内部类处理
91 *
92 * @author codebrother
93 */
94 class EventListener2 extends JFrame {
95 private JButton btBlue, btDialog;
96
97 public EventListener2() {
98 setTitle("Java GUI 事件监听处理");
99 setBounds(100, 100, 500, 350);
100 setLayout(new FlowLayout());
101
102 btBlue = new JButton("蓝色");
103 btDialog = new JButton("弹窗");
104
105 // 添加事件监听器(此处即为匿名类)
106 btBlue.addActionListener(new ActionListener() {
107 // 事件处理
108 @Override
109 public void actionPerformed(ActionEvent e) {
110 Container c = getContentPane();
111 c.setBackground(Color.BLUE);
112 }
113 });
114
115 // 并添加事件监听器
116 btDialog.addActionListener(new ActionListener() {
117 @Override
118 public void actionPerformed(ActionEvent e) {
119 JDialog dialog = new JDialog();
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 * @author codebrother
137 */
138 class EventListener4 extends JFrame {
139 private JButton btBlue, btDialog;
140
141 public EventListener4() {
142 setTitle("Java GUI 事件监听处理");
143 setBounds(100, 100, 500, 350);
144 setLayout(new FlowLayout());
145 btBlue = new JButton("蓝色");
146 btDialog = new JButton("弹窗");
147 // 将按钮添加事件监听器
148 btBlue.addActionListener(new ColorEventListener(this));
149 btDialog.addActionListener(new DialogEventListener());
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 implements ActionListener {
160 private EventListener4 el;
161 ColorEventListener(EventListener4 el) {
162 this.el = el;
163 }
164 @Override
165 public void actionPerformed(ActionEvent e) {
166 Container c = el.getContentPane();
167 c.setBackground(Color.BLUE);
168 }
169 }
170 // 外部类DialogEventListener,实现ActionListener接口
171 class DialogEventListener implements ActionListener {
172 @Override
173 public void actionPerformed(ActionEvent e) {
174 JDialog dialog = new JDialog();
175 dialog.setBounds(300, 200, 400, 300);
176 dialog.setVisible(true);
177 }
178 }
179
180 public class ActionListenerTest
181 {
182 public static void main(String args[])
183 {
184 new EventListener2();
185 }
186 }
时间: 2024-12-08 16:21:30