java - 尝试从 Java 调用 JavaFX 应用程序... NoSuchMethodException

标签 java javafx nosuchmethod

我有一个主类,它应该调用 JavaFX 应用程序 (SimpleSun) 以从用户那里获取信息。目前我创建了一个 JavaFX 类的对象并启动它,但这似乎不起作用。有人看到我工作中的错误吗?

这是我的代码和异常: Main.java:

package ch.i4ds.stix.sim;

import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;


    public class Main{
    Configuration config;
    public static void main(String[] args) {
        ConfigurationFromFile config = new ConfigurationFromFile();
        SimpleSun ss = new SimpleSun(config);
        ss.show();
    }
}

SimpleSun.java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;

public class SimpleSun extends Application{

    private Stage primaryStage;
    Configuration configuration;

    public SimpleSun(ConfigurationFromFile config) {
        this.configuration = config;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Simple Sun - Alpha");
        System.out.println("Test");
        try {
            // Load the root layout from the fxml file
            FXMLLoader loader = new FXMLLoader(
                    Main.class.getResource("view/RootLayout.fxml"));
            BorderPane rootLayout = (BorderPane) loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            // Exception gets thrown if the fxml file could not be loaded
            e.printStackTrace();
        }
    }

    public void show(){
        launch();
    }

}

异常(exception):

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class ch.i4ds.stix.sim.SimpleSun
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: ch.i4ds.stix.sim.SimpleSun.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    ... 4 more

最佳答案

扩展应用程序时,必须提供不带参数的构造函数。所以你可以这样做:

public class SimpleSun extends Application {

    private Stage primaryStage;
    Configuration configuration;

    public SimpleSun() {
        this.configuration = Main.getConfig();
    }
    //...

在你的Main类中:

public static Configuration getConfig() { return new ConfigurationFromFile(); }

或者,您可以使用 launch(args)String 参数传递给类,并使用 将它们返回到 SimpleSun 类中getParameters().

关于java - 尝试从 Java 调用 JavaFX 应用程序... NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56005035/

相关文章:

java - 如何在iis中实现servlet

java - 如何将本地管理面板与 Android Studio 中的 Android 应用程序连接?

JavaFX 缩放至全屏

java - 我怎样才能编写一个方法来使这段代码可重用?

java - 在WebView中,按+和-键执行放大和缩小功能

flatMap 返回 List<Object> 而不是 List<String> 后,Java Stream 收集

css - 如何在JavaFX元素中使用CSS文件?

flutter - 什么是 NoSuchMethod 错误,我该如何解决?

Flutter: NoSuchMethodError : 方法 'fetchByID' 在 null 上被调用。接收方:null 尝试调用:fetchByID(2)

来自命令行的 javax.swing.JDialog.setAutoRequestFocus 的 Java NoSuchMethodError