试图从 Java 调用 JavaFX 应用程序... NoSuchMethodException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21729362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:22:34  来源:igfitidea点击:

Trying to call a JavaFX application from Java... NoSuchMethodException

javajavafxnosuchmethod

提问by NexusTeddy

I have a main class which should call a JavaFX application (SimpleSun) to get Information from the user. Currently I create an Object of the JavaFX class and start it, but that doesn't seem to work. Does someone see the mistake in my work?

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

Here's my code and exception: Main.java:

这是我的代码和异常: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:

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
public class SimpleSun extends Application {

    private Stage primaryStage;
    Configuration configuration;

    public SimpleSun() {
        this.configuration = Main.getConfig();
    }
    //...
0(Unknown Source) at com.sun.javafx.application.LauncherImpl.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

采纳答案by assylias

You must provide a constructor with no arguments when you extend application. So you could do something like:

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

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

and in your Mainclass:

在你的Main课堂上:

##代码##

Alternatively you can pass Stringparameters to the class with launch(args)and get them back in the SimpleSunclass with getParameters().

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