[英]Javafx show mouse coords
我想使用javafx
圖形庫編寫簡單的幫助程序。 無論鼠標是在場景內還是在桌面上,它都應該在桌面上顯示鼠標的鼠標坐標。 我設法做到這一點:
import javafx.geometry.Insets;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.animation.AnimationTimer;
import java.awt.MouseInfo;
import javafx.scene.layout.HBox;
public class Apka extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
new AnimationTimer() {
public void handle(long currentNanoTime) {
Text textX = new Text(MouseInfo.getPointerInfo().getLocation().getX() + "");
Text textY = new Text(MouseInfo.getPointerInfo().getLocation().getY() + "");
textX.setFont(new Font(20));
textY.setFont(new Font(20));
HBox root = new HBox(5);
root.setPadding(new Insets(1));
root.getChildren().add(textX);
root.getChildren().add(textY);
Scene scene = new Scene(root, 300, 30);
primaryStage.setTitle("Mouse Coordinates");
primaryStage.setScene(scene);
}
}.start();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
有用。
我遇到的明顯問題是Java程序始終位於最前面,我無法使用Alt-Tab Windows快捷方式切換程序。 我只想刷新Text對象,尤其是Mouse cords的值。 可以這樣做嗎,還是應該完全廢棄然后回去。
經過“優化”解決:
new AnimationTimer() {
public void handle(long currentNanoTime) {
textX.setText(MouseInfo.getPointerInfo().getLocation().getX() + "");
textY.setText(MouseInfo.getPointerInfo().getLocation().getY() + "");
}
}.start();
primaryStage.show();
我只需要使用setText()方法更新文本對象字段。 其他所有內容都放在AnimationTimer的“外部”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.