java swing 单击事件mouseClicked与一般事件actionPerformed区别

java swing 单击事件mouseClicked与一般事件actionPerformed区别
分类: swing 692人阅读 评论(0) 收藏 举报

//鼠标单击事件无论什么时候都监听,即使按钮已经不能用了,事件依然走;

//一般事件,在设置按钮不可用后就不在走了

例子很能说明问题:

[java] view plain copy print ?
  1. package eeeee;  
  2.   
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.awt.event.MouseAdapter;  
  6. import java.awt.event.MouseEvent;  
  7.   
  8. import javax.swing.JButton;  
  9. import javax.swing.JFrame;  
  10. import javax.swing.SwingUtilities;  
  11. import javax.swing.UIManager;  
  12.   
  13. import org.dyno.visual.swing.layouts.Constraints;  
  14. import org.dyno.visual.swing.layouts.GroupLayout;  
  15. import org.dyno.visual.swing.layouts.Leading;  
  16.   
  17. //VS4E -- DO NOT REMOVE THIS LINE!   
  18. public class bbbb extends JFrame {  
  19.   
  20.     private static final long serialVersionUID = 1L;  
  21.     private JButton jButton0;  
  22.     private JButton jButton1;  
  23.     private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";  
  24.     public bbbb() {  
  25.         initComponents();  
  26.     }  
  27.   
  28.     private void initComponents() {  
  29.         setLayout(new GroupLayout());  
  30.         add(getJButton0(), new Constraints(new Leading(521010), new Leading(391010)));  
  31.         add(getJButton1(), new Constraints(new Leading(1951010), new Leading(391212)));  
  32.         setSize(320240);  
  33.     }  
  34.   
  35.     private JButton getJButton1() {  
  36.         if (jButton1 == null) {  
  37.             jButton1 = new JButton();  
  38.             jButton1.setText("jButton1");  
  39.             jButton1.addActionListener(new ActionListener() {  
  40.       
  41.                 public void actionPerformed(ActionEvent event) {  
  42.                     jButton1ActionActionPerformed(event);  
  43.                 }  
  44.             });  
  45.         }  
  46.         return jButton1;  
  47.     }  
  48.   
  49.     private JButton getJButton0() {  
  50.         if (jButton0 == null) {  
  51.             jButton0 = new JButton();  
  52.             jButton0.setText("jButton0");  
  53.             jButton0.addMouseListener(new MouseAdapter() {  
  54.       
  55.                 public void mouseClicked(MouseEvent event) {  
  56.                     jButton0MouseMouseClicked(event);  
  57.                 }  
  58.             });  
  59.         }  
  60.         return jButton0;  
  61.     }  
  62.   
  63.     private static void installLnF() {  
  64.         try {  
  65.             String lnfClassname = PREFERRED_LOOK_AND_FEEL;  
  66.             if (lnfClassname == null)  
  67.                 lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();  
  68.             UIManager.setLookAndFeel(lnfClassname);  
  69.         } catch (Exception e) {  
  70.             System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL  
  71.                     + " on this platform:" + e.getMessage());  
  72.         }  
  73.     }  
  74.   
  75.     /** 
  76.      * Main entry of the class. 
  77.      * Note: This class is only created so that you can easily preview the result at runtime. 
  78.      * It is not expected to be managed by the designer. 
  79.      * You can modify it as you like. 
  80.      */  
  81.     public static void main(String[] args) {  
  82.         installLnF();  
  83.         SwingUtilities.invokeLater(new Runnable() {  
  84.             @Override  
  85.             public void run() {  
  86.                 bbbb frame = new bbbb();  
  87.                 frame.setDefaultCloseOperation(bbbb.EXIT_ON_CLOSE);  
  88.                 frame.setTitle("bbbb");  
  89.                 frame.getContentPane().setPreferredSize(frame.getSize());  
  90.                 frame.pack();  
  91.                 frame.setLocationRelativeTo(null);  
  92.                 frame.setVisible(true);  
  93.             }  
  94.         });  
  95.     }  
  96. //00   
  97.     private void jButton0MouseMouseClicked(MouseEvent event) {  
  98.         jButton0.setEnabled(false);  
  99.         System.out.println("click 00");  
  100.     }  
  101. //11   
  102.     private void jButton1ActionActionPerformed(ActionEvent event) {  
  103.         jButton1.setEnabled(false);  
  104.         System.out.println("click 1111");  
  105.     }  
  106.   
  107. }  


点击button0后,它设置成不可用,但它依然响应事件(里面的打印输出了)

而点击button1后,设置它不可用,再点击它,就没反应了,说明真的不可用了。

从这里我们直观的看出两种事件的区别,更深的道理我就不说了,嘿嘿

你可能感兴趣的:(swing)