在 Mac 上使用 Eclipse 时,JavaFX 15 中 GUI 未显示

Chu*_*mas 5 java eclipse macos user-interface javafx

我一直按照说明使用 Java JDK 15 和 JavaFX 15 构建 Hello World JavaFX 应用程序。我添加了此处描述的运行配置:https: //openjfx.io/openjfx-docs/#IDE-Eclipse。当我在 Mac 上运行该程序时,我的控制台中没有收到任何错误,但 GUI 没有出现。在我的 Windows 计算机上构建相同的应用程序,我能够构建并查看 GUI。

主程序.java

package hellofx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("hellofx.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 400, 300));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器.java

package hellofx;

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class Controller {

    @FXML
    private Label label;

    public void initialize() {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        label.setText("Hello, JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + ".");
    }
}
Run Code Online (Sandbox Code Playgroud)

hellofx.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hellofx.Controller">
    <children>
        <Label fx:id="label" text="Label" />
    </children>
</StackPane>
Run Code Online (Sandbox Code Playgroud)

Chu*_*mas 7

这个问题的解决方案可以在这里找到。

转到主类的运行配置,然后在“参数”选项卡上,取消选中“使用 SWT 启动时使用 -XstartOnFirstThread 参数”框。