JavaFX-如何检查鼠标是否已进入或退出窗格?
我在Javafx中有一个窗格,每当鼠标进入其边界时,我都希望对该窗格设置动画,并且我希望在鼠标退出该窗格后动画停止。我知道这需要一个监听器,但我找到的所有答案似乎都只与java有关。awt您可以使用这些方法并注册事件处理程序,它们可以启动或停止动画。下面是一个简单的例子:JavaFX-如何检查鼠标是否已进入或退出窗格?,java,javafx,mouseevent,listener,mouselistener,Java,Javafx,Mouseevent,Listener,Mouselistener,我在Javafx中有一个窗格,每当鼠标进入其边界时,我都希望对该窗格设置动画,并且我希望在鼠标退出该窗格后动画停止。我知道这需要一个监听器,但我找到的所有答案似乎都只与java有关。awt您可以使用这些方法并注册事件处理程序,它们可以启动或停止动画。下面是一个简单的例子: public class MainTest extends Application { public void start(Stage primaryStage) { Pane pane = new P
public class MainTest extends Application {
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setStyle("-fx-background-color: #ff0000");
pane.setLayoutX(100);
pane.setLayoutY(100);
pane.setPrefSize(300,300);
pane.setOnMouseEntered(event -> startAnimation());
pane.setOnMouseExited(event -> stopAnimation());
Scene scene = new Scene(new Pane(pane), 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
private void stopAnimation() {
System.out.println("stop");
// do whatever you need to start your animation
}
private void startAnimation() {
System.out.println("start");
// do whatever you need to stop your animation
}
}
您可以使用方法和注册事件处理程序,这些事件处理程序启动或停止动画。下面是一个简单的例子:
public class MainTest extends Application {
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setStyle("-fx-background-color: #ff0000");
pane.setLayoutX(100);
pane.setLayoutY(100);
pane.setPrefSize(300,300);
pane.setOnMouseEntered(event -> startAnimation());
pane.setOnMouseExited(event -> stopAnimation());
Scene scene = new Scene(new Pane(pane), 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
private void stopAnimation() {
System.out.println("stop");
// do whatever you need to start your animation
}
private void startAnimation() {
System.out.println("start");
// do whatever you need to stop your animation
}
}
或者,您可以使用*.fxml文件链和控制器类: 将两个事件onmouseintered=onMouseInto-onmouseexted=onMouseOut添加到in-fxml文件的窗格字符串中,使其类似于
或者,您可以使用*.fxml文件链和控制器类: 将两个事件onmouseintered=onMouseInto-onmouseexted=onMouseOut添加到in-fxml文件的窗格字符串中,使其类似于 , ,
@FXML
public AnchorPane rootPane;
public void onMouseInto(MouseEvent mouseEvent) {
//Your own event when cursor is gonna into the rootPane
rootPane.setStyle("-fx-background-color: #1F292E");
}
public void onMouseOut(MouseEvent mouseEvent) {
//Your own event when cursor is gonna out the rootPane
rootPane.setStyle("-fx-background-color: #C792EA");
}