如何创建扩展Application的A类实例

时间:2017-01-07 20:49:31

标签: eclipse javafx

我刚刚设计了一个简单的javaFx应用程序。虽然运行它独奏工作,但当我尝试分离并创建它的所有实例时我得到:

at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more

我的代码

import javafx.application.Application;
import javafx.stage.Stage;

public class Demo 
{
    public static void main(String[] args) 
    {

        Demos dm = new Demos();

    }
}

class Demos extends Application {
    private String args;
    private Stage stage;

    public Demos()
    {
        main(args);
        start(stage);

    }

    public void main(String args) 
    {
        this.args=args;
        launch(this.args);

    }

    @Override
    public void start(Stage stage)
    {
        this.stage=stage;
        this.stage.setTitle("Simple JavaFX Application");
        this.stage.setResizable(false);
        this.stage.show();
    }

}

1 个答案:

答案 0 :(得分:0)

Application.launch要求Application课程为public。对于您的Demos课程,情况并非如此。

附加说明

private String args;
private Stage stage;

public Demos()
{
    main(args);
    ...
}

public void main(String args) 
{
    this.args=args;
    ...
}

只需将args的初始值分配给自己,这将始终导致args剩余null

Application.launch是一种创建static实例本身的Application方法。从实例调用此表单毫无意义。

如果您要发布特定的Application,请将Application课程传递给Application.launch

public static void main(String[] args) {
    Application.launch(Demos.class);
}
public Demos extends Application {
    private Stage stage;

    public Demos(){
    }

    @Override
    public void start(Stage stage) {
        this.stage=stage;
        this.stage.setTitle("Simple JavaFX Application");
        this.stage.setResizable(false);
        this.stage.show();
    }

}