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

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