//按一下滑鼠事件無論什麼時候都監聽,即使按鈕已經不能用了,事件依然走;
//一般事件,在設定按鈕不可用後就不在走了
例子很能說明問題:
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() {@Overridepublic 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);}});}//00private void jButton0MouseMouseClicked(MouseEvent event) {jButton0.setEnabled(false);System.out.println("click 00");}//11private void jButton1ActionActionPerformed(ActionEvent event) {jButton1.setEnabled(false);System.out.println("click 1111");}}
點擊button0後,它設定成不可用,但它依然響應事件(裡面的列印輸出了)
而點擊button1後,設定它不可用,再點擊它,就沒反應了,說明真的不可用了。
從這裡我們直觀的看出兩種事件的區別,更深的道理我就不說了,嘿嘿