java - JavaFX 中的 while 循环导致窗口崩溃

标签 java javafx

我正在尝试创建一个非常简单、易于使用的程序,它可以主动读取并显示鼠标的位置。我看过许多教程,这些教程创建的程序仅当鼠标位于 GUI 应用程序的窗口内或按下按钮后才读取鼠标的位置,但我想要一个能够在屏幕的所有区域显示鼠标位置的程序。这就是我所拥有的:

import java.awt.MouseInfo;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MouseCoordinates extends Application{

     public static void main(String[] args) {

          launch();

     }

     public void start(Stage primaryStage) throws Exception{
          primaryStage.setTitle("Mouse Reader");
          Label x = new Label();
          Label y = new Label();
          StackPane layout = new StackPane();
          layout.getChildren().addAll(x, y);

          Scene scene = new Scene(layout, 600, 500);
          primaryStage.setScene(scene);
          primaryStage.show ();

          double mouseX = 1.0;
          double mouseY = 1.0;

          while(true){
               mouseX = MouseInfo.getPointerInfo().getLocation().getX();
               mouseY = MouseInfo.getPointerInfo().getLocation().getY();
               x.setText("" + mouseX);
               y.setText("" + mouseY);
          }
     }
}

我知道这个 while 循环是窗口崩溃的原因,但我无法找到解决方法。谁能解释一下为什么我不能对 JavaFX 使用 while 循环,以及解决这个问题的方法?

最佳答案

您的 start() 方法没有任何更改来退出循环,因此返回为您定义的无限循环:while(true){...} 没有 return 语句。

为什么不使用 Timeline

Timeline timeLine = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
        mouseX = MouseInfo.getPointerInfo().getLocation().getX();
        mouseY = MouseInfo.getPointerInfo().getLocation().getY();
        x.setText("" + mouseX);
        y.setText("" + mouseY);
    }
}));
timeLine.setCycleCount(Timeline.INDEFINITE);
timeLine.play();

或者使用 lambda :

Timeline timeLine = new Timeline(new KeyFrame(Duration.seconds(1), 
    e -> {
           mouseX = MouseInfo.getPointerInfo().getLocation().getX();
           mouseY = MouseInfo.getPointerInfo().getLocation().getY();
           x.setText("" + mouseX);
           y.setText("" + mouseY);
         }   
));
timeLine.setCycleCount(Timeline.INDEFINITE);
timeLine.play();
<小时/>

满足您的要求的另一种方法是在 StackPane 对象上使用 addEventFilter( MouseEvent.MOUSE_MOVED) :

layout.addEventFilter(MouseEvent.MOUSE_MOVED, e -> {             
       x.setText("" + e.getScreenX());
       y.setText("" + e.getScreenY());
});

MouseEvent 类提供设备上的 X 和 Y 绝对位置,并且 在源组件上:

getScreenX()

Returns absolute horizontal position of the event.

getScreenY()

Returns the absolute vertical y position of the event

getX();

Horizontal position of the event relative to the origin of the MouseEvent's source.

getY();

Vertical position of the event relative to the origin of the MouseEvent's source.

关于java - JavaFX 中的 while 循环导致窗口崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45394041/

相关文章:

java - 最终变量和构造函数重载

java - 为空舞台创建可调整大小的边框

JavaFX程序通过IDE运行,但不是独立运行(Ubuntu)

listview - 每个单元格中带有按钮的 javafx listview

java - 如何以编程方式获取有关 APNS 推送证书的信息?

java - Java代码大O时间复杂度计算工具?

java - 如何修复JavaFX中值发生变化的错误?

java - Hibernate二级缓存-同一实体的多个对象

java - 如何在 ControlsFX (JavaFX 8) 中使用 BreadCrumbBar

javafx - 在 ImageView 中缩放图像 (javafx)