首页 > 编程语言 > 详细

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

时间:2014-08-19 10:35:24      阅读:441      评论:0      收藏:0      [点我收藏+]

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.   JButton ok, cancel,exit;  //创建接受响应的组建,就是三个按钮 
  14.   public  ButtonListener(String title){ 
  15.   super(title); 
  16.   this.setLayout(new FlowLayout()); 
  17.   ok = new JButton("确定"); 
  18.   cancel = new JButton("返回"); 
  19.   exit = new JButton("退出"); 
  20. //下面三个语句 为按钮分别  注册监听器 
  21.   ok.addActionListener(this);     
  22.   cancel.addActionListener(this); 
  23.   exit.addActionListener(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.        System.out.println("确定"); 
  33.      if(e.getSource()==cancel) 
  34.        System.out.println("返回"); 
  35.      if(e.getSource()==exit) 
  36.          System.exit(0);; 
  37.   } 
  38.  
  39.   public static void main(String args[]) { 
  40.      ButtonListener pd=new ButtonListener("ActionEvent Demo"); 
  41.      pd.setSize(250,100); 
  42.     pd.setVisible(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.     ok = new JButton("确定"); 
  64.     cancel = new JButton("返回"); 
  65.     exit = new JButton("退出"); 
  66.     ok.addActionListener(new MyListener()); 
  67.     cancel.addActionListener(new MyListener());; 
  68.     exit.addActionListener(new MyListener());; 
  69.     getContentPane().add(ok); 
  70.     getContentPane().add(cancel); 
  71.     getContentPane().add(exit); 
  72.   } 
  73.   
  74.   public static void main(String args[]) { 
  75.      ButtonListener pd=new ButtonListener("ActionEvent Demo"); 
  76.      pd.setSize(250,100); 
  77.     pd.setVisible(true); 
  78.   } 
  79. } 
  80.    //监听动作事件 
  81. class  MyListener implements ActionListener{ 
  82.    public void  actionPerformed(ActionEvent e){ 
  83.      if(e.getActionCommand()=="确定") 
  84.         System.out.println("确定"); 
  85.      if(e.getActionCommand()=="返回") 
  86.         System.out.println("返回"); 
  87.       if(e.getActionCommand()=="退出") 
  88.          System.exit(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.             JOptionPane.showMessageDialog(null, "打开"); 
  114.         }else if (e.getActionCommand()=="close"){ 
  115.             JOptionPane.showMessageDialog(null, "关闭"); 
  116.         }else if (e.getActionCommand()=="run"){ 
  117.             JOptionPane.showMessageDialog(null, "运行"); 
  118.         }else if (e.getActionCommand()=="stop"){ 
  119.             JOptionPane.showMessageDialog(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.         frame=new JFrame("menu"); 
  131.         frame.getContentPane().setLayout(new BorderLayout()); 
  132.         menubar=new JMenuBar(); 
  133.         JMenu menuFile=new JMenu("file"); 
  134.          
  135.         //实例化一个菜单项,并添加监听openAction, 
  136.         JMenuItem menuItemopen=new JMenuItem("open"); 
  137.         menuItemopen.addActionListener(MenuEvent); 
  138.         JMenuItem menuItemclose=new JMenuItem("close"); 
  139.         menuItemclose.addActionListener(MenuEvent); 
  140.         menuFile.add(menuItemopen); 
  141.         menuFile.add(menuItemclose); 
  142.         JMenu menuTool=new JMenu("tool"); 
  143.         JMenuItem menuItemrun=new JMenuItem("run"); 
  144.         menuItemrun.addActionListener(MenuEvent); 
  145.         JMenuItem menuItemstop=new JMenuItem("stop"); 
  146.         menuItemstop.addActionListener(MenuEvent); 
  147.         menuTool.add(menuItemrun); 
  148.         menuTool.add(menuItemstop); 
  149.         menubar.add(menuFile); 
  150.         menubar.add(menuTool); 
  151.         menubar.setVisible(true); 
  152.         frame.add(menubar,BorderLayout.NORTH); 
  153.         frame.setSize(400,200); 
  154.         frame.setVisible(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.         Action action=getAction(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.     String buttonName[]={"b1","b2","b3"}; 
  199.     public TestAE(){ 
  200.         super("事件"); 
  201.         for (int i=0;i<buttonName.length;i++){ 
  202.             ViewAction action=new ViewAction(buttonName[i]); 
  203.             JButton button=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.         JOptionPane.showMessageDialog(null, "单击了 b1"); 
  222.     } 
  223. } 
  224. class b2 implements Action{ 
  225.     public void execute(){ 
  226.         JOptionPane.showMessageDialog(null, "单击了 b2"); 
  227.     } 
  228. } 
  229. class b3 implements Action{ 
  230.     public void execute(){ 
  231.         JOptionPane.showMessageDialog(null, "单击了 b3"); 
  232.     } 
  233. }</pre><br> 
  234. <br> 
  235. <p></p> 

java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射),布布扣,bubuko.com

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

原文:http://www.cnblogs.com/eiaino/p/3921324.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!