Java添加事件的四种方式

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

Java添加事件的四种方式的相关文章

SWT组件添加事件的四种方式

在我们CS日常开发过程中会经常去为组件添加事件,我们常用的为AWT与SWT.SWT的事件模型是和标准的AWT基本一样的.下面将按照事件的四种写法来实现它. 一.匿名内部类的写法 new MouseAdapter()就是一个匿名内部类,我们去创建一个MouseAdapter类,它继承了MouseListener类,在类中去重写MouseListener的方法. 使用匿名内部类的形式来写代码简单方便,但是也有一些需要注意的缺点: 1)由于事件处理代码会随着组件一起分散在代码的各个部分,所以不够集中,

Java添加事件的几种方式(转载了codebrother的文章)

/** * Java事件监听处理--自身类实现ActionListener接口,作为事件监听器 * * @author codebrother */ class EventListener1 extends JFrame implements ActionListener { private JButton btBlue, btDialog; public EventListener1() { setTitle("Java GUI 事件监听处理"); setBounds(100, 10

java解析xml文件四种方式介绍、性能比较和基本使用方法

一.介绍: 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息.分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.由于它是基于信息层次的,因而DOM被认为是基于树或基于对象的.DOM以及广义的基于树的处理具有几个优点.首先,由于树在内存中是持久的,因此可以修改它以便应用程序能对数据和结构作出更改.它还可以在任何时候在树中上下导航,而不

jQuery绑定事件的四种方式:bind、live、delegate、on

1.jQuery操作DOM元素的绑定事件的四种方式 jQuery中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undelegate.off. 2.必备的基础知识: DOM树 示例,这是在browser环境下的一棵模拟DOM树: 我们的页面可以理解为一棵DOM树,当我们在叶子结点上做什么事情的时候(如click一个a元素),如果我们没有人为的设置stopPropagation(Moder Browser), cancel

jQuery绑定事件的四种方式

jQuery绑定事件的四种方式 jQuery提供了多种绑定事件的方式,每种方式各有其特点,明白了它们之间的异同点,有助于我们在写代码的时候进行正确的选择,从而写出优雅而容易维护的代码.下面我们来看下jQuery中绑定事件的方式都有哪些. jQuery中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undelegate.off.在开始看他们之前 一:bind(type,[data],function(eventObject

点击事件的四种方式

点击事件的四种方式:         1.在布局文件中,给Button添加点击事件属性:android:onClick="方法名",然后在MainActivity中public void 方法名(View v){处理点击事件}         2.在MainActivity中或的Button的实例后,直接设置监听:用匿名内部类实现OnClickListener         button.setOnClickListener(new OnClickListener(){ @Overr

android点击事件的四种方式

第一种方式:创建内部类实现点击事件 代码如下: package com.example.dail; import android.text.TextUtils; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; imp

java线程实现的四种方式

java多线程的实现可以通过以下四种方式 1.继承Thread类,重写run方法 2.实现Runnable接口,重写run方法 3.通过Callable和FutureTask创建线程 4.通过线程池创建线程 方式1,2不再赘述. 方式3,通过Callable和FutureTask创建线程实现多线程 @Test public void MyCallableTest() throws Exception { //创建线程执行对象 MyCallable myCallable = new MyCalla

Java Array数组 遍历 四种方式(包含 Lambda 表达式遍历)

package com.hello; import java.util.Arrays; /** * @Author Miracle Luna * @Date 2019/6/9 23:33 * @Version 1.0 */public class ArrayLambda { public static void main(String[] args) { Integer[] items = { 1, 2, 3 }; // 普通for循环遍历 System.out.println("第一种方式:普