【发布时间】: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 可能会有所帮助!