java - 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;
}
解决方案
推荐阅读
- node.js - 我必须为 REST API 访问 postgreSQL 数据库配置什么?
- android - 无法解析所有 androidx 依赖项
- c++ - 检查范围的元素是否可以移动?
- regex - 使用 PhpStorm 的搜索和替换对话框转换下一个文本的正则表达式
- angular - 如何在引导模式中显示 agm 方向?
- python - np.array MemoryError 与 tf.data.Dataset.from_generator
- android - Firebase RecyclerOptions 未从 firebase 检索数据
- python-3.x - 递归函数客栈python
- machine-learning - 当只使用最可能的类时是否使用 softmax?
- android - 如何在 Android 设备上接收新 Outlook 电子邮件的推送通知?