JavaFX:对单击,双击和三次单击做出反应 [英] JavaFX: Reacting to Single, Double, and Triple Click

查看:192
本文介绍了JavaFX:对单击,双击和三次单击做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置某些响应鼠标单击次数的操作.在我看来,单击和三次单击得到检测并应用.但是双击并不能真正起作用. 我试图做类似的事情:

I am trying to configure certain actions in response to number of mouse clicks. It seems to me that the single click and triple click get detected and applied. but the double click does not really work. I tried to do something like:

if (doubleClick)
else if (tripleClick)
else if (singleClick).

检查顺序也无济于事,双击操作永远不会触发,因为单击先触发.有关如何执行此操作的任何想法?

The order of checking did not help either, the action for the double click never gets triggered because that of the single click get triggered first. Any ideas on how to do this?

推荐答案

如果要检测每个不同的点击计数,则可以使用switch语句.

If you want to detect each of the different click counts, you could use a switch statement.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 *
 * @author jeffreyguenther
 */
public class ListTest extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        Circle c = new Circle();
        c.setRadius(100);
        c.setCenterX(100);
        c.setCenterY(100);
        c.setFill(Color.AQUA);
        c.setStroke(Color.BLACK);
        c.setStrokeWidth(3);

        c.setOnMousePressed((e) ->{

            switch(e.getClickCount()){
                case 1:
                    System.out.println("One click");
                    break;
                case 2:
                    System.out.println("Two clicks");
                    break;
                case 3:
                    System.out.println("Three clicks");
                    break;
            }

        });

        stage.setScene(new Scene(new Group(c)));
        stage.show();
    }

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

然后,当您单击,双击和三次单击时,将给出以下输出:

Then as you single, double, and triple click this output is given:

One click
Two clicks
Three clicks

您可以使用case语句来确定要处理的计数.例如,如果您只想处理双击和三次单击,则可以删除第一种情况.

You can use the case statements to determine which counts you want to handle. For example, if you only want to handle the double and triple clicks, you can remove the first case.

这篇关于JavaFX:对单击,双击和三次单击做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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