【问题标题】:How to check is mouse cursor is on the button in Java FX?如何检查鼠标光标是否在 Javafx 中的按钮上?
【发布时间】:2019-05-05 03:35:05
【问题描述】:

我是 Java 新手,我想知道是否可以检查鼠标光标是否在按钮上?我的意思是没有点击事件,只是在按钮上移动光标。

我的工作代码得到点击然后打印一些东西,但我想稍微改变一下,但我不知道为什么它不起作用。

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!");
}
}

【问题讨论】:

  • 回答为评论,因为我不确定了:我想我可能记得 Button 有 hoverProperty() 或类似的东西。编辑:是的,this 可能会有所帮助!

标签: java javafx


【解决方案1】:

我为你做了小代码。看一看。它在控制台中打印“Ho-Ho-Ho-Hovereed!”将鼠标悬停在按钮上。

 @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();
    }

【讨论】:

    【解决方案2】:

    我猜你可以使用事件处理程序或 css 来做到这一点,比如...

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

    或/带有样式(css)

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

    【讨论】:

      【解决方案3】:

      每个Node 都提供一个hover 属性来跟踪鼠标光标是否悬停在它上面。通过使用简单的侦听器,您可以检测鼠标何时开始和停止悬停:

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

      使用此侦听器,如果鼠标当前悬停在按钮上,newValue 将始终为 true,并在鼠标离开该区域时更改为 false

      【讨论】:

        【解决方案4】:

        在节点上还有一个有用的检查 - isHover() 您可以在 MouseEvents 上使用它来检查鼠标是否悬停在所需的节点上。这样的提示;)

        【讨论】:

          【解决方案5】:
          button.setOnMouseEntered( (event ) -> {
                  button.setTranslateX(button.getTranslateX() + 20);
                  button.setTranslateY(button.getTranslateY() + 20);
          
              });`
          

          用我的代码替换button.setOnMouseMoved(this);。这样就可以了!

          【讨论】:

          • 你的代码实际上做了什么?它如何从概念上回答这个问题?
          猜你喜欢
          • 1970-01-01
          • 2016-03-26
          • 2012-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-24
          • 1970-01-01
          相关资源
          最近更新 更多