我的Javafx圖表從另一個類獲取時出現問題。當主要方法是創建如類的一個實例:在以下運行時錯誤從主方法啓動Javafx圖表作爲實例時出錯
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();
}
}
我想補充,'應用#推出()'創建一個單獨的JavaFX線程,只有一個實例是每個虛擬機允許的,所以調用啓動裏面的構造函數將拋出'java.lang.IllegalStateException:應用程序啓動不能被調用多次' – ItachiUchiha 2014-09-10 14:39:07
如果OP使用'Charts'作爲主應用程序條目,當然。但我明白他有一些其他入口點:'公共類主要{公共靜態無效主要(字符串[] ARGS){新圖表();}}'。但也許我錯過了一些東西。無論如何...這不是做這件事的方法。 – 2014-09-10 14:51:32
謝謝你。我仍然遇到了實現問題,並且在代碼不在主類中時運行代碼。 – TommyH 2014-09-11 09:43:33