自己在网上看到的,觉得很好,收藏一下
来源:互联网 发布:神奇女侠 知乎 编辑:程序博客网 时间:2024/05/01 14:08
Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动):
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Java事件监听处理——自身类实现ActionListener接口,作为事件监听器
*
* @author codebrother
*/
class EventListener1 extends JFrame implements ActionListener {
private JButton btBlue, btDialog;
public EventListener1() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 将按钮添加事件监听器
btBlue.addActionListener(this);
btDialog.addActionListener(this);
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// ***************************事件处理***************************
@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事件监听处理——内部类处理
*
* @author codebrother
*/
class EventListener3 extends JFrame {
private JButton btBlue, btDialog;
// 构造方法
public EventListener3() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 添加事件监听器对象(面向对象思想)
btBlue.addActionListener(new ColorEventListener());
btDialog.addActionListener(new DialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 内部类ColorEventListener,实现ActionListener接口
class ColorEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
}
// 内部类DialogEventListener,实现ActionListener接口
class DialogEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
}
/**
* Java事件监听处理——匿名内部类处理
*
* @author codebrother
*/
class EventListener2 extends JFrame {
private JButton btBlue, btDialog;
public EventListener2() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 添加事件监听器(此处即为匿名类)
btBlue.addActionListener(new ActionListener() {
// 事件处理
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
});
// 并添加事件监听器
btDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent 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事件监听处理——外部类处理
*
* @author codebrother
*/
class EventListener4 extends JFrame {
private JButton btBlue, btDialog;
public EventListener4() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 将按钮添加事件监听器
btBlue.addActionListener(new ColorEventListener(this));
btDialog.addActionListener(new DialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// 外部类ColorEventListener,实现ActionListener接口
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);
}
}
// 外部类DialogEventListener,实现ActionListener接口
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();
}
}
- 自己在网上看到的,觉得很好,收藏一下
- 网上看到的一篇收藏自己看
- 在红客论坛上看到的,可以看看——自己觉得很好
- Struts中不同的Action和ActionForm组合(在技术中心看到的文章,觉得有用,所以自己收藏一下)引自http://blog.csdn.net/sean_gao/
- 网上看到对 DriverManager.getConnection() 的疑问,收藏一下
- 在网上看到一篇文章 安慰一下自己孤寂的心灵
- 网上看到的收藏了
- 【收藏】在网上看到的关于Eclipse的配置
- 在网上看到一些高效的编程方法,分享一下
- (网上看到的,觉得不错,转载了)你想不想使自己的收入加倍
- Java学习心得(看到后觉得很好,在博客上分享一下,希望对大家有启发)
- 一个很好的模拟弹出窗口的特效 偶尔看到了收藏一下- -b
- 在淘宝一个4钻卖家的主页上看到的一首诗,觉得很好。
- 网上看到的一篇关于串口收发短信的文章(收藏一下 vc版)
- 在网上看到的学习C++的建议(个人觉得说得不错)
- 从网上看到一些文章,自己再总结一下,在学习编程中一些要点
- 网上看到的,真TM说得太对了!特意跟男同胞们分享!觉得好就顶一下!
- 看到一篇很好的的dom4j总结,转载一下,方便大家方便自己~
- KVO,通知中心
- 获取WiFi MAC地址总结
- same-origin policy
- Socket 与 WebSocket
- linux grep基本用法
- 自己在网上看到的,觉得很好,收藏一下
- C++字符串函数全集
- Ubuntu下录制屏幕并转换成gif
- C++ 获取文件夹下的所有文件名
- [IOS开发]获取webView中网页的信息
- android按键事件和触屏事件(1)-几个重要方法的传递顺序
- gson json字符串转换
- 调试程序成员变量不显示值
- 魔法方法,属性和迭代器