1 回答

TA貢獻1796條經驗 獲得超4個贊
對不起,我不明白你希望它擴展到應用程序之外,檢查出這個例子,它經過測試和工作
public class Main extends Application {
private Label xLabel;
private Label yLabel;
@Override
public void start(Stage primaryStage) {
xLabel = new Label("X Coordinate: 0");
yLabel = new Label("y Coordinate: 0");
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
vBox.getChildren().addAll(xLabel, yLabel);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.show();
updateCoordintates();
}
private void updateCoordintates(){
new Thread(()->{ //Splits off the Main thread
double lastX = 1;
double lastY = 1;
while (true) { //Will run forever you may want to change this not sure of your constraints
double x = MouseInfo.getPointerInfo().getLocation().getX();
double y = MouseInfo.getPointerInfo().getLocation().getY();
//The platform.runlater will allow you to post the change to the screen when available
if(x!=lastX)
Platform.runLater(()->xLabel.setText(String.valueOf(x)));
if(y!=lastY)
Platform.runLater(()->yLabel.setText(String.valueOf(y)));
lastX = x;
lastY = y;
System.out.println("X = " + x + "\tY = " + y);
}
}).start();
}
public static void main(String[] args) { launch(args); }
}
Y?o?u? ?d?o? ?n?o?t? ?n?e?e?d? ?t?o? ?i?t? ?o?n? ?a? ?b?a?c?k?g?r?o?u?n?d? ?t?h?r?e?a?d? ?t?o? ?t?h?i?s? ?j?u?s?t? ?f?i?r?e? ?a? ? ?f?r?o?m? ?w?h?a?t?e?v?e?r? ?c?o?n?t?a?i?n?e?r? ?y?o?u? ?a?r?e? ?u?s?i?n?g? ?h?e?r?e? ?i?s? ?a? ?r?u?n?n?a?b?l?e? ?e?x?a?m?p?l?e? ?t?h?a?t? ?u?p?d?a?t?e?s? ?t?h?e? ?c?o?o?r?d?i?n?a?t?e?s? ?o?n? ?m?o?v?e?m?e?n?t?.?s?e?t?O?n?M?o?u?s?e?M?o?v?e?d?
添加回答
舉報