首页 > 解决方案 > JavaFX 应用程序无法启动(使用 JDK11 的 MacOS)

问题描述

我正在尝试在 Eclipse 上运行 JavaFX 应用程序。该代码基于https://openjfx.io/openjfx-docs/#gradle在“JavaFX 和 Eclipse”->“来自 IDE 的模块化”下的说明。但是,当我尝试运行该应用程序时,没有出现任何窗口。

我使用的是 MacBook Pro 2018,唯一出现的是 Dock 上名为“java”的带有一杯咖啡的图像。我不能放弃它。它退出和停止的唯一方法是从 Eclipse 中强制终止程序。

package org.openjfx;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class MainApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("scene.fxml"));

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());

        stage.setTitle("JavaFX and Gradle");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

module hellofx {

    requires javafx.controls;
    requires javafx.fxml;
    requires transitive javafx.base;
    requires transitive javafx.graphics;

    opens org.openjfx to javafx.fxml;
    exports org.openjfx;

}

package org.openjfx;

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

public class FXMLController {

    @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 + ".");
    }    
}

“场景.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.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.openjfx.FXMLController">
   <children>
      <Label fx:id="label" text="Label" />
   </children>
</StackPane>

“样式.css”

.button {
    -fx-font-weight: bold;
}

运行时控制台的图像。没有抛出异常。

项目文件夹图像

标签: javaeclipsemacosjavafx

解决方案


推荐阅读