亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我應該如何使用另一個線程來獲取光標位置并使用坐標更新2個標簽?

我應該如何使用另一個線程來獲取光標位置并使用坐標更新2個標簽?

慕田峪9158850 2022-09-07 17:06:37
所以我在初始化中有這個:    double x = 1, y = 1;    while (x != 0 || y != 0) {        x = MouseInfo.getPointerInfo().getLocation().getX();        y = MouseInfo.getPointerInfo().getLocation().getY();        cursorXPositionLabel.setText("" + x);        cursorYPositionLabel.setText("" + y);        System.out.println("X = " + x + "\tY = " + y);    }但是,在光標不在 (0,0) 坐標之前,應用程序不會啟動。如果是這樣,應用程序將啟動,標簽顯示 0, 0。我希望應用程序啟動,并在移動光標的同時,使用實際坐標更新標簽。
查看完整描述

1 回答

?
慕的地8271018

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?


查看完整回答
反對 回復 2022-09-07
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號