您的位置:首页 > 编程语言 > Java开发

Java学习笔记之swing 单击事件mouseClicked与一般事件actionPerformed区别

2011-12-10 12:24 441 查看
//鼠标单击事件无论什么时候都监听,即使按钮已经不能用了,事件依然走;

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

例子很能说明问题:



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后,设置它不可用,再点击它,就没反应了,说明真的不可用了。

从这里我们直观的看出两种事件的区别,更深的道理我就不说了,嘿嘿
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: