简体   繁体   English

如何检查鼠标光标是否在 Java FX 的按钮上?

[英]How to check is mouse cursor is on the button in Java FX?

I'm new to Java and I'm wondering if it is possible to check if mouse cursor is for example on the button?我是 Java 新手,我想知道是否可以检查鼠标光标是否在按钮上? I mean not getting clicking events but just moving cursor on the button.我的意思是没有获得点击事件,而只是在按钮上移动光标。

I had working code getting click and then printing something, but I want to change it a little and I can't find out why it doesn't work.我有工作代码被点击然后打印一些东西,但我想稍微改变它,我不知道为什么它不起作用。

public class Main extends Application implements EventHandler<MouseEvent> {

Button button;
Stage window;
Scene scene;

@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Where's the button?");
    button.setText("Click me!");
    button.setOnMouseMoved(this);

    StackPane layout = new StackPane();
    layout.getChildren().add(button);

    Scene scene = new Scene(layout, 300,350);
    primaryStage.setScene(scene);
    primaryStage.show();

}


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

@Override
public void handle(MouseEvent actionEvent) {
    System.out.println("You clicked the button!");
}
}

I have made small code for you. 我为您编写了小代码。 Take a look. 看一看。 It prints in the console "Ho-Ho-Ho-Hovereed!" 它在控制台上显示“ Ho-Ho-Ho-Hovereed!”。 once you hover over your button. 将鼠标悬停在按钮上后

 @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Hover over me.'");

        btn.hoverProperty().addListener((event)->System.out.println("Ho-Ho-Ho-Hovereeed!"));        

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Mouse manpulation example in JavaFX!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

i guess you can do that with event handler or css, like... 我想你可以使用事件处理程序或CSS来做到这一点,例如...

button.setOnMouseEntered(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {                
            System.out.println("Cursor Over Button");
        }
    });

or/with styles (css) 或/具有样式(css)

.button:hover{ -fx-background-color: red; }

Each Node provides a hover property to track whether the mouse cursor is hovering over it or not. 每个Node提供一个hover属性,以跟踪鼠标光标是否悬停在其上。 By using a simple listener, you can detect when the mouse starts and stops hovering: 通过使用一个简单的侦听器,您可以检测到鼠标何时开始和停止悬停:

button.hoverProperty().addListener((observable, oldValue, newValue) -> {
    if (newValue) {
        System.out.println("Hovering...");
    } else {
        System.out.println("Retreating...");
    }
});

With this listener, newValue will always be true if the mouse is currently hovering over the button and change to false when the mouse leaves the area. 使用此侦听器,如果鼠标当前悬停在按钮上,则newValue始终为true当鼠标离开该区域时, newValue将始终为false

There is also a useful check on a Node - isHover() You can use it on MouseEvents to check if the mouse is hovering over the needed node.还有一个对节点的有用检查 - isHover() 您可以在 MouseEvents 上使用它来检查鼠标是否悬停在所需节点上。 Such a tip ;)这样的提示;)

button.setOnMouseEntered( (event ) -> {
        button.setTranslateX(button.getTranslateX() + 20);
        button.setTranslateY(button.getTranslateY() + 20);

    });`

Replace button.setOnMouseMoved(this); 替换button.setOnMouseMoved(this); with my code. 与我的代码。 This will do! 这样就可以了!

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

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