从主方法启动Javafx图表作为实例时出错

时间:2014-09-10 13:51:10

标签: java charts javafx

我有一个问题让Javafx图表来自另一个班级。在Main方法中创建类的实例时,例如:

Charts c = new Charts();

以下代码导致以下运行时错误

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Charts
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:884)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.sun.javafx.application.LauncherImpl$7.run(LauncherImpl.java:791)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.IllegalStateException: Application launch must not be called more than once
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:137)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:118)
    at javafx.application.Application.launch(Application.java:241)
    at Charts.<init>(Charts.java:50)
    ... 11 more
Exception running application Charts

请有人帮忙纠正这种情况吗?我认为问题可能是由于在Charts方法中使用了launch()而没有提供特定的参数。

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class Charts extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        // defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        // creating the chart
        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(
                xAxis, yAxis);

        lineChart.setTitle("Stock Monitoring, 2010");
        // defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        // populating the series with data
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));
        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));

        Scene scene = new Scene(lineChart, 800, 600);
        lineChart.getData().add(series);

        stage.setScene(scene);
        stage.show();
    }

    public Charts() {
        launch();
    }

}

1 个答案:

答案 0 :(得分:-1)

这不是启动JavaFX应用程序的受支持方式。 (我无法告诉你没有堆栈跟踪失败的具体细节。)launch方法将为您实例化Application类的新实例,启动JavaFX子系统,执行start(...)方法。您应该将start(...)方法视为应用程序中的入口点(事实上,在Java 8中,即使它没有Application方法,您也可以从命令行启动main(...)子类。全部)。

如果你想让你的Charts类可以重用,那么它可以从各个地方启动,或者把它作为可以放在Scene中的某个子类,或者给它这种对象的存取方法。 Application的子类具有特定功能,作为JavaFX应用程序的“启动类”。

E.g。

public class Charts {
    private Parent content ;

    public Charts() {
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");

        // ... etc etc

        series.getData().add(new XYChart.Data(12, 25));

        content = new StackPane();
        content.getChildren().add(lineChart);
    }

    public Parent getContent() {
        return content ;
    }
}

然后您可以创建使用

的应用程序类
public class MyChartApp extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("My chart app");
        Charts c = new Charts();
        Scene scene = new Scene(c.getContent(), 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

您可以添加一些实用工具方法,例如

public Scene createScene() {
    return new Scene(content, 800, 600);
}

public void configureStage(Stage stage) {
    stage.setScene(createScene());
}
如果您需要/需要,请

Charts课程。

然后(当然)您的start(...)方法可以简化为

public void start(Stage primaryStage) {
    new Charts().configureStage(primaryStage);
    primaryStage.show();
}