JavaFX:鼠标事件

Posted xhBruce

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaFX:鼠标事件相关的知识,希望对你有一定的参考价值。

JavaFX:鼠标事件

fxml文件在最下面


fxml界面进入

MousePoint: setOnMouseEntered (461.77777777777777,208.0) 
MousePoint: setOnMouseMoved (461.77777777777777,208.0) 

鼠标点击

MousePoint: setOnMousePressed (360.44444444444446,203.55555555555554) 
MousePoint: setOnMouseReleased (360.44444444444446,203.55555555555554) 
MousePoint: setOnMouseClicked (360.44444444444446,203.55555555555554)

鼠标点击拖动

MousePoint: setOnMousePressed (451.1111111111111,206.22222222222223) 
MousePoint: setOnMouseDragged (451.55555555555554,206.66666666666666) 
MousePoint: setOnMouseDragged (451.55555555555554,207.55555555555554) 
MousePoint: setOnMouseDragged (451.55555555555554,208.0) 
MousePoint: setOnMouseReleased (451.55555555555554,208.0) 
MousePoint: setOnMouseClicked (451.55555555555554,208.0) 

鼠标滑动,不点击

MousePoint: setOnMouseMoved (459.55555555555554,209.77777777777777) 
MousePoint: setOnMouseMoved (459.55555555555554,210.66666666666666) 
MousePoint: setOnMouseMoved (459.55555555555554,211.11111111111111) 
MousePoint: setOnMouseMoved (459.55555555555554,211.55555555555554) 
MousePoint: setOnMouseMoved (458.6666666666667,211.55555555555554) 
MousePoint: setOnMouseMoved (458.6666666666667,212.0) 
MousePoint: setOnMouseMoved (458.6666666666667,212.88888888888889) 
MousePoint: setOnMouseMoved (458.6666666666667,213.33333333333334) 
MousePoint: setOnMouseMoved (458.6666666666667,213.77777777777777) 
MousePoint: setOnMouseMoved (458.6666666666667,214.66666666666666) 
MousePoint: setOnMouseMoved (458.6666666666667,215.11111111111111) 

鼠标左中右键

左键

MousePoint: PRIMARY (479.1111111111111,207.55555555555554) 
MousePoint: setOnMousePressed (479.1111111111111,207.55555555555554) 
MousePoint: setOnMouseReleased (479.1111111111111,207.55555555555554) 
MousePoint: setOnMouseClicked (479.1111111111111,207.55555555555554) 

中键

MousePoint: MIDDLE (479.1111111111111,207.55555555555554) 
MousePoint: setOnMousePressed (479.1111111111111,207.55555555555554) 
MousePoint: setOnMouseReleased (479.1111111111111,207.55555555555554) 
MousePoint: setOnMouseClicked (479.1111111111111,207.55555555555554) 

右键

MousePoint: SECONDARY (479.1111111111111,207.55555555555554) 
MousePoint: setOnMousePressed (479.1111111111111,207.55555555555554) 
MousePoint: setOnMouseReleased (479.1111111111111,207.55555555555554) 
MousePoint: setOnMouseClicked (479.1111111111111,207.55555555555554) 

fxml界面退出

MousePoint: setOnMouseExited (345.77777777777777,346.6666666666667) 

fxml文件

public class MousePointAnchorPane extends AnchorPane {

    public static String TAG = "MousePoint:";

    public MousePointAnchorPane() {
        super();
        MousePointListener();
    }

    public MousePointAnchorPane(Node... children) {
        super(children);
        MousePointListener();
    }

    private void MousePointListener() {
        setOnMouseClicked(event -> {
            System.out.println(TAG + " setOnMouseClicked (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseDragExited(event -> {
            System.out.println(TAG + " setOnMouseDragExited (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseDragOver(event -> {
            System.out.println(TAG + " setOnMouseDragOver (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMousePressed(event -> {
            switch (event.getButton()) {
                case PRIMARY:
                    System.out.println(TAG + " PRIMARY (" + event.getSceneX() + "," + event.getSceneY() + ") ");
                    break;
                case SECONDARY:
                    System.out.println(TAG + " SECONDARY (" + event.getSceneX() + "," + event.getSceneY() + ") ");
                    break;
                case MIDDLE:
                    System.out.println(TAG + " MIDDLE (" + event.getSceneX() + "," + event.getSceneY() + ") ");
                    break;
                default:
                    break;
            }
            System.out.println(TAG + " setOnMousePressed (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseDragged(event -> {
            System.out.println(TAG + " setOnMouseDragged (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseDragEntered(event -> {
            System.out.println(TAG + " setOnMouseDragEntered (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseDragReleased(event -> {
            System.out.println(TAG + " setOnMouseDragReleased (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseMoved(event -> {
            System.out.println(TAG + " setOnMouseMoved (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseEntered(event -> {
            System.out.println(TAG + " setOnMouseEntered (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseExited(event -> {
            System.out.println(TAG + " setOnMouseExited (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
        setOnMouseReleased(event -> {
            System.out.println(TAG + " setOnMouseReleased (" + event.getSceneX() + "," + event.getSceneY() + ") ");
        });
    }
}

以上是关于JavaFX:鼠标事件的主要内容,如果未能解决你的问题,请参考以下文章

JavaFX实战:几种事件监听实现,键盘按键事件监听,鼠标滚轮事件监听,鼠标按键事件监听,鼠标移动事件监听

JavaFX实战:几种事件监听实现,键盘按键事件监听,鼠标滚轮事件监听,鼠标按键事件监听,鼠标移动事件监听

javafx的鼠标事件对于带有透明的图片在部分区域无效

JavaFX:鼠标事件

JavaFx - 按下Button 一直触发按钮事件

使用 JavaFX 在任何地方处理鼠标事件