Java图形用户界面中,处理事件时所必须的步骤是:
1、创建接受响应的组件(控件)
2、实现相关事件监听接口
3、注册事件源的动作监听器
4、事件触发时的事件处理
相应的可以通过以下的集中方式来作出事件响应。
1. <span style="font-size: 18px;">一、容器类监听
2.
3. 效果:单击窗体中的三个按钮,实现相应的相应时间。
4.
5. </span><pre class="java" name="code">import java.awt.*;
6. import java.awt.event.*;
7. import javax.swing.*;
8.
9. //声明 类时,添加“implements ActionListener”实现监听的接口,如果要对多种监听方式进行监听,则用逗号间隔开
10. // 比如“implements ActionListener,KeyListener”
11.
12. class ButtonListener extends JFrame implements ActionListener{
13. //创建接受响应的组建,就是三个按钮
14. public ButtonListener(String title){
15. super(title);
16. this.setLayout(new FlowLayout());
17. new JButton("确定");
18. new JButton("返回");
19. new JButton("退出");
20. //下面三个语句 为按钮分别 注册监听器
21. this);
22. this);
23. this);
24. getContentPane().add(ok);
25. getContentPane().add(cancel);
26. getContentPane().add(exit);
27. }
28.
29. //完成 事件触发时的事件处理
30. public void actionPerformed(ActionEvent e){
31. if(e.getSource()==ok)
32. "确定");
33. if(e.getSource()==cancel)
34. "返回");
35. if(e.getSource()==exit)
36. 0);;
37. }
38.
39. public static void main(String args[]) {
40. new ButtonListener("ActionEvent Demo");
41. 250,100);
42. true);
43. }
44. }
45. </pre><br>
46. <br>
47. <pre></pre>
48. <p><span style="font-size: 18px;">二、监听类实现</span><br>
49. <br>
50. <pre style="margin: 4px 0px; font-size: 18px; background-color: rgb(240, 240, 240);" class="java" name="code"><span style="font-size: 18px;">效果:单击窗体中的三个按钮,实现相应的相应时间。
51. </span></pre><p></p>
52. <div><span style="font-size: 18px;"><br>
53. </span></div>
54. <pre style="font-size: 18px;" class="html" name="code">import java.awt.*;
55. import java.awt.event.*;
56. import javax.swing.*;
57.
58. class ButtonListener1 extends JFrame { //这里没有实现监听
59. JButton ok, cancel,exit;
60. public ButtonListener1(String title){
61. super(title);
62. this.setLayout(new FlowLayout());
63. new JButton("确定");
64. new JButton("返回");
65. new JButton("退出");
66. new MyListener());
67. new MyListener());;
68. new MyListener());;
69. getContentPane().add(ok);
70. getContentPane().add(cancel);
71. getContentPane().add(exit);
72. }
73.
74. public static void main(String args[]) {
75. new ButtonListener("ActionEvent Demo");
76. 250,100);
77. true);
78. }
79. }
80. //监听动作事件
81. class MyListener implements ActionListener{
82. public void actionPerformed(ActionEvent e){
83. if(e.getActionCommand()=="确定")
84. "确定");
85. if(e.getActionCommand()=="返回")
86. "返回");
87. if(e.getActionCommand()=="退出")
88. 0);;
89. }
90.
91. } </pre><br>
92. <span style="font-size: 18px;">三、使用 AbstractAction类实现监听</span><br>
93. <span style="font-size: 18px;"> 这个方法,我也没搞清,照着别人的例子做出来的<br>
94. 效果:单击菜单,作出响应</span><br>
95. <pre style="font-size: 18px;" class="java" name="code">import java.awt.BorderLayout;
96. import java.awt.event.ActionEvent;
97. import javax.swing.AbstractAction;
98. import javax.swing.Action;
99. import javax.swing.JFrame;
100. import javax.swing.JMenu;
101. import javax.swing.JMenuBar;
102. import javax.swing.JMenuItem;
103. import javax.swing.JOptionPane;
104.
105. //此类继承AbstractAction,必须实现actionPerformed()方法。
106. class AbstractEvent extends AbstractAction{
107. //private static final long serialVersionUID = 1L;
108. AbstractEvent(){
109. }
110. public void actionPerformed(ActionEvent e){
111. //弹出确认对话框
112. if (e.getActionCommand()=="open"){
113. null, "打开");
114. else if (e.getActionCommand()=="close"){
115. null, "关闭");
116. else if (e.getActionCommand()=="run"){
117. null, "运行");
118. else if (e.getActionCommand()=="stop"){
119. null, "停止");
120. }
121. }
122. }
123. public class TestAbstractEvent {
124. private static JMenuBar menubar;
125. private static JFrame frame;
126.
127. //指定MenuEvent的具体处理程序是AbstractEvent类完成的。
128. final Action MenuEvent=new AbstractEvent();
129. public TestAbstractEvent(){
130. new JFrame("menu");
131. new BorderLayout());
132. new JMenuBar();
133. new JMenu("file");
134.
135. //实例化一个菜单项,并添加监听openAction,
136. new JMenuItem("open");
137. menuItemopen.addActionListener(MenuEvent);
138. new JMenuItem("close");
139. menuItemclose.addActionListener(MenuEvent);
140. menuFile.add(menuItemopen);
141. menuFile.add(menuItemclose);
142. new JMenu("tool");
143. new JMenuItem("run");
144. menuItemrun.addActionListener(MenuEvent);
145. new JMenuItem("stop");
146. menuItemstop.addActionListener(MenuEvent);
147. menuTool.add(menuItemrun);
148. menuTool.add(menuItemstop);
149. menubar.add(menuFile);
150. menubar.add(menuTool);
151. true);
152. frame.add(menubar,BorderLayout.NORTH);
153. 400,200);
154. true);
155. }
156. public static void main(String[] args){
157. new TestAbstractEvent();
158. }
159. }</pre><br>
160. <span style="font-size: 18px;">四、</span><span style="font-size: 18px;"> AbstractAction类 + 反射 的方法<br>
161. <span style="font-size: 18px;">这个方法,我也没搞清,照着别人的例子做出来的!<br>
162. </span><br>
163. 效果:单击工具栏的三个按钮,通过按钮的名称,反射得到 与按钮名称相同的类实现响应。<br>
164. <br>
165. </span><pre class="java" name="code">import java.awt.BorderLayout;
166. import java.awt.event.ActionEvent;
167. import javax.swing.*;
168.
169. class ViewAction extends AbstractAction{
170. private String ActionName="";
171. //private JFrame frame=null;
172. private Action action=null;
173. public ViewAction(){
174. }
175. public ViewAction(String ActionName){
176. this.ActionName=ActionName;
177. //this.frame=frame;
178. }
179. @Override
180. public void actionPerformed(ActionEvent e) {
181. this.ActionName);
182. action.execute();
183. }
184. private Action getAction(String ActionName){
185. try{
186. if (this.action==null){
187. Action action=(Action)Class.forName(ActionName).newInstance();
188. this.action=action;
189. }
190. return this.action;
191. catch(Exception e){
192. return null;
193. }
194. }
195. }
196. public class TestAE extends JFrame {
197. public JToolBar bar=new JToolBar();
198. "b1","b2","b3"};
199. public TestAE(){
200. super("事件");
201. for (int i=0;i<buttonName.length;i++){
202. new ViewAction(buttonName[i]);
203. new JButton(buttonName[i]);
204. button.addActionListener(action);
205. bar.add(button);
206. }
207. this.getContentPane().add(bar,BorderLayout.NORTH);
208. this.setSize(300, 200);
209. this.setLocationRelativeTo(null);
210. this.setVisible(true);
211. }
212. public static void main(String [] args){
213. new TestAE();
214. }
215. }
216. interface Action{
217. void execute();
218. }
219. class b1 implements Action{
220. public void execute(){
221. null, "单击了 b1");
222. }
223. }
224. class b2 implements Action{
225. public void execute(){
226. null, "单击了 b2");
227. }
228. }
229. class b3 implements Action{
230. public void execute(){
231. null, "单击了 b3");
232. }
233. }</pre><br>
234. <br>
235. <p></p>