当前位置:   article > 正文

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

swing 单击

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

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

例子很能说明问题:

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/350268
推荐阅读
相关标签
  

闽ICP备14008679号