Java學習筆記之swing 單擊事件mouseClicked與一般事件actionPerformed區別

來源:互聯網
上載者:User

//按一下滑鼠事件無論什麼時候都監聽,即使按鈕已經不能用了,事件依然走;

//一般事件,在設定按鈕不可用後就不在走了

例子很能說明問題:

 

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後,設定它不可用,再點擊它,就沒反應了,說明真的不可用了。

從這裡我們直觀的看出兩種事件的區別,更深的道理我就不說了,嘿嘿

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.