我希望通过将changelistener附加到整数来实现此目标
(mouse.x)。但是getPointerInfo()。getLocation();一旦初始化。之后,位置不会更新。如何使这些变更侦听器在值的更改上起作用?
也许有更好的方法可以做到这一点。任何帮助表示赞赏。
public class TheMidiApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Point mouse = java.awt.MouseInfo.getPointerInfo().getLocation();
System.out.println("MouseX " + mouse.x);
System.out.println("MouseY " + mouse.y);
//Changelistener X
IntegerProperty changeValueMouseX = new SimpleIntegerProperty(mouse.x);
changeValueMouseX.addListener(new ChangeListener<Number>(){
@Override
public void changed(ObservableValue <? extends Number> observableValue, Number oldValue, Number newValue){
System.out.println("OLD MOUSE X VALUE: " + oldValue);
System.out.println("NEW MOUSE X VALUE: " + newValue);
}
});
//Changelistener Y
IntegerProperty changeValueMouseY = new SimpleIntegerProperty(mouse.y);
changeValueMouseX.addListener(new ChangeListener<Number>(){
@Override
public void changed(ObservableValue <? extends Number> observableValue, Number oldValue, Number newValue){
System.out.println("OLD MOUSE Y VALUE: " + oldValue);
System.out.println("NEW MOUSE Y VALUE: " + newValue);
}
});
MouseX = mouse.x;
MouseY = mouse.y;
最佳答案
您在根节点上需要一个侦听器。
public class JavaFXApplication29 extends Application {
@Override
public void start(Stage primaryStage) {
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root, 500, 500);
root.setOnMouseMoved(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
System.out.println("mouse X: " + event.getX());
System.out.println("mouse Y: " + event.getY());
}
});
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
关于java - 如何使用changelistener监视鼠标位置y的变化?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40848902/