【发布时间】:2015-11-17 08:38:23
【问题描述】:
我希望能够使用 JavaFX 检测鼠标何时移动以及何时不在 AnchorPane 上移动。
我目前在我的窗格中有一个“鼠标移动事件”,它在鼠标移动时输出一些文本,但还想在鼠标不移动时包含一些输出。
我不知道如何在保持相对简单的同时解决问题的后半部分。
每当鼠标移动时,以下输出“正在移动”,但当鼠标停止时,什么也没有发生。我可以看到这是因为 checkMovement 仅在它移动时被调用。
是否有反函数或类似的东西——会做相反的事情?
我确实相信我可以使用特定的事件处理程序和/或计时器来实现我想要的,但这对于看起来很简单的事情来说过于复杂了。
我是否遗漏了一些非常明显的东西??
我只是使用 FX 的新手,所以请随时指出我应该以不同的方式做的任何事情。
编辑:
我确实忘记指定我使用 FXML 来定义控件。
我需要将此过程放在控制器(AppController.java)中还是为了“易于使用”而将其留在 Main.java 中?
我什至会怎么做/这可能吗?
显然mouseMovementMethod需要修改。
AppController.Java
公共类 AppController 实现 Initializable{
boolean moved = false;
@FXML
private AnchorPane anchorPane;
@FXML
private ImageView killpic;
@FXML
private ImageView processpic;
@FXML
private Text exit;
@FXML
private Button processAnimalButton;
/*
* ...Other Variables
*/
@FXML
void mouseMovedMethod(Event event){
//Mouse Movement Event
}
@FXML
void closeMenu(Event event) {
Stage stage = (Stage) exit.getScene().getWindow();
stage.close();
}
@FXML
void selectProcess(Event event) {
Image killimg = new Image("/images/knife_blood.png");
Image proimg = new Image("/images/p_selected.png");
killpic.setImage(killimg);
processpic.setImage(proimg);
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
Image killimg = new Image("/images/knife_blood_selected.png");
Image proimg = new Image("/images/p.png");
killpic.setImage(killimg);
processpic.setImage(proimg);
}
}
Main.java:
//*Required imports*//
public class Main extends Application {
Stage thestage;
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("popup.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
thestage = primaryStage;
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
【问题讨论】:
-
每次
mouseMoved被触发时,都会以短暂的延迟重新启动某种计时器。当定时器触发时,鼠标将停止移动。您可能还想跟踪鼠标退出事件