2014-09-10 24 views
0

我的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(); 
    } 

} 

回答

-1

這不是推出JavaFX應用程序的支持方式。 (我無法告訴你爲什麼沒有堆棧跟蹤失敗的具體細節。)launch方法將爲您實例化Application類的新實例,啓動JavaFX子系統並執行start(...)方法。您應該將start(...)方法視爲應用程序中的入口點(實際上,在Java 8中,即使根本沒有main(...)方法,也可以從命令行啓動Application子類)。

如果你想讓你的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(); 
} 
+0

我想補充,'應用#推出()'創建一個單獨的JavaFX線程,只有一個實例是每個虛擬機允許的,所以調用啓動裏面的構造函數將拋出'java.lang.IllegalStateException:應用程序啓動不能被調用多次' – ItachiUchiha 2014-09-10 14:39:07

+0

如果OP使用'Charts'作爲主應用程序條目,當然。但我明白他有一些其他入口點:'公共類主要{公共靜態無效主要(字符串[] ARGS){新圖表();}}'。但也許我錯過了一些東西。無論如何...這不是做這件事的方法。 – 2014-09-10 14:51:32

+0

謝謝你。我仍然遇到了實現問題,並且在代碼不在主類中時運行代碼。 – TommyH 2014-09-11 09:43:33