我正在尝试在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 + ".");
}
}
“ scene.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>
“ style.css”
.button {
-fx-font-weight: bold;
}
Image of console when running. There are no exceptions thrown.