首页 > 学院 > 开发设计 > 正文

java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射)

2019-11-14 20:56:26
字体:
来源:转载
供稿:网友
java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射)

Java图形用户界面中,处理事件时所必须的步骤是:1、创建接受响应的组件(控件)2、实现相关事件监听接口3、注册事件源的动作监听器4、事件触发时的事件处理相应的可以通过以下的集中方式来作出事件响应。

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表