为辅助鼠标按钮Javafx添加鼠标事件

所以我有这个锚板,我希望为辅助鼠标键添加鼠标列表器我尝试了以下但我不断得到一个错误,任何人都知道问题是什么?

   mainDisplayPanel.addEventHandler(MouseButton.SECONDARY, new EventHandler<MouseButton>() {

                    @Override
                    public void handle(MouseButton event) {
                        System.out.Println("Works");

                    }
                });

为了记录,我也试过这个:

            mainDisplayPanel.addEventHandler(MouseButton.SECONDARY, new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    System.out.println("WOrks");
                }
            });

堆栈跟踪:

Bound mismatch: The generic method addEventHandler(EventType,
EventHandler) of type Node is not applicable for the
arguments (MouseButton, new EventHandler(){}). The
inferred type MouseButton&Event is not a valid substitute for the
bounded parameter

和另外一个:

Bound mismatch: The type MouseButton is not a valid substitute for the bounded parameter of the type EventHandler

解决方法:

没有基于MouseButton.SECONDARY的EventType.您需要检查MouseEvent本身:

mainDisplayPanel.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {
        if (event.getButton() == MouseButton.SECONDARY) {
           System.out.println("Works");
        }
    }
});
上一篇:我可以在Android模拟器上更改鼠标的图片吗?


下一篇:java – JFreeChart:将鼠标附近的鼠标坐标显示为提示(鼠标移动时)