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

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

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

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

例子很能说明问题:

  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. }  
package eeeee;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class bbbb extends JFrame {

	private static final long serialVersionUID = 1L;
	private JButton jButton0;
	private JButton jButton1;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public bbbb() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJButton0(), new Constraints(new Leading(52, 10, 10), new Leading(39, 10, 10)));
		add(getJButton1(), new Constraints(new Leading(195, 10, 10), new Leading(39, 12, 12)));
		setSize(320, 240);
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("jButton1");
			jButton1.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jButton1ActionActionPerformed(event);
				}
			});
		}
		return jButton1;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("jButton0");
			jButton0.addMouseListener(new MouseAdapter() {
	
				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				bbbb frame = new bbbb();
				frame.setDefaultCloseOperation(bbbb.EXIT_ON_CLOSE);
				frame.setTitle("bbbb");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}
//00
	private void jButton0MouseMouseClicked(MouseEvent event) {
		jButton0.setEnabled(false);
		System.out.println("click 00");
	}
//11
	private void jButton1ActionActionPerformed(ActionEvent event) {
		jButton1.setEnabled(false);
		System.out.println("click 1111");
	}

}


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

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

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值