鼠标协调只在短的特定范围内工作javafx
Mouse coordination just work in short specific range javafx
我是 javafx 新手。
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
mouseCoordination.setOnMouseMoved(e ->
mouseCoordination.setText(e.getX()+","+ e.getY())
);
StackPane root = new StackPane();
root.setPrefHeight(100);
root.setPrefWidth(400);
root.getChildren().add(mouseCoordination);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
这里我试图让鼠标协调,所以我写了这个但是它只能在短范围内工作,比如 (0,0) 到 (48,17),之后它不会更新标签。我实际上是根据站点
中的一个问题写的
那是因为标签不是 width
× height
而你给标签添加了鼠标监听;如果您将侦听器添加到 root
,它将按照您想要的方式工作。
@Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
StackPane root = new StackPane();
root.setPrefWidth(400);
root.setPrefHeight(100);
root.getChildren().add(mouseCoordination);
root.setOnMouseMoved(e -> {
mouseCoordination.setText(e.getX() + "," + e.getY());
});
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
为了清楚起见,例如,您将鼠标侦听器添加到宽度仅为 30 且高度仅为 40 的标签;大小取决于显示的文本以及它占用的大小 space。只有鼠标在标签上时才会调用鼠标侦听器;但是,由于您希望它显示您当前的鼠标位置,除非标签具有与 window.
相同的宽度和高度,否则它不会工作
我是 javafx 新手。
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
mouseCoordination.setOnMouseMoved(e ->
mouseCoordination.setText(e.getX()+","+ e.getY())
);
StackPane root = new StackPane();
root.setPrefHeight(100);
root.setPrefWidth(400);
root.getChildren().add(mouseCoordination);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
这里我试图让鼠标协调,所以我写了这个但是它只能在短范围内工作,比如 (0,0) 到 (48,17),之后它不会更新标签。我实际上是根据站点
中的一个问题写的那是因为标签不是 width
× height
而你给标签添加了鼠标监听;如果您将侦听器添加到 root
,它将按照您想要的方式工作。
@Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
StackPane root = new StackPane();
root.setPrefWidth(400);
root.setPrefHeight(100);
root.getChildren().add(mouseCoordination);
root.setOnMouseMoved(e -> {
mouseCoordination.setText(e.getX() + "," + e.getY());
});
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
为了清楚起见,例如,您将鼠标侦听器添加到宽度仅为 30 且高度仅为 40 的标签;大小取决于显示的文本以及它占用的大小 space。只有鼠标在标签上时才会调用鼠标侦听器;但是,由于您希望它显示您当前的鼠标位置,除非标签具有与 window.
相同的宽度和高度,否则它不会工作