OnMouseEvent(JavaFX2)不会捕获双击 [英] Double click is not being caught by OnMouseEvent (JavaFX2)

查看:66
本文介绍了OnMouseEvent(JavaFX2)不会捕获双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

双击被捕获为单击:(

在fxml文件中:

<Button fx:id="A_button" onMouseClicked="#buttonAClicked">

在控制器中

private void buttonAClicked(MouseEvent mouseEvent) {
    if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
        if (mouseEvent.getClickCount() == 2) {
            System.out.println("Double clicked A_button");
        }
        if (mouseEvent.getClickCount() == 1) {
            System.out.println("Single clicked A_button");
        }
    }
}

不幸的是,我发现没有捕获到双击-只有单击. 在调试器中,单击计数为1.

Unfortunately I am finding that double click is not being caught - only single clicks. In the debugger, click count is 1.

更新: 由于我无法弄清为什么它在JavaFX 2.2.3-b05上不起作用,因此我做了一个变通办法,并消除了双击的需要.我向用户界面添加了加载"按钮.现在,用户必须单击并按下加载按钮.

Update: Since I cant figure out why it is not working for me on JavaFX 2.2.3-b05, I did a workaround and removed the need for double-click. I added a "Load" button to the UI. Now user must single click AND press the load button.

推荐答案

它已在JavaFX 2.2中修复,请参见 http://javafx-jira.kenai.com/browse/RT-19346

It was fixed in JavaFX 2.2, see http://javafx-jira.kenai.com/browse/RT-19346

请注意,双击将收到两个事件:

Note, that on double click you will receive two events:

  • 使用getClickCount()== 1鼠标单击
  • 使用getClickCount()== 2进行鼠标点击

例如如果您在下面运行代码,然后双击按钮输出将是:

E.g. if you run code below and double click on button output would be:

clicks: 1
clicks: 2

代码(已通过2.2.4测试):

Code (tested with 2.2.4):

public class DoubleClicks extends Application {
    @Override public void start(Stage stage) {
        Button btn = new Button();
        btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("clicks: " + event.getClickCount());
            }
        });

        stage.setScene(new Scene(new Group(btn), 300, 250));
        stage.setTitle(VersionInfo.getRuntimeVersion());
        stage.show();
    }

    public static void main(String[] args) { launch(); }
}

这篇关于OnMouseEvent(JavaFX2)不会捕获双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆