简体   繁体   English

Java MouseListener并非始终有效

[英]Java MouseListener not always working

I find it odd that in my project, the mouselistener is not always heard. 我感到奇怪的是,在我的项目中,并非总是听到mouselistener的声音。 Has anyone else come across this? 还有其他人遇到吗? Or am I doing something wrong? 还是我做错了什么? I have to sometimes click it several times before it opens. 有时我必须多次单击它才能将其打开。 I do wait between clicks to see if it is a performance issue. 我确实在两次点击之间等待,看看是否是性能问题。

JButton btnPin = new JButton("Pin");
btnPin.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent arg0) {
        if(staffPinDialog == null || staffPinDialog.isShowing() == false)
            staffPinDialog = new StaffPinDialog(idClicked);                 
    }
});

StaffPinDialog is a JDialog, where the reference has been created elsewhere. StaffPinDialog是一个JDialog,其中的引用已在其他位置创建。 idClicked is also always initialized. idClicked也总是被初始化。

You're not supposed to use a MouseListener to detect button clicks. 您不应该使用MouseListener来检测按钮的单击。 Use an ActionListener instead. 请改用ActionListener。

This will also have the additional advantage of being able to click the button using the keyboard. 这还将具有能够使用键盘单击按钮的其他优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM