2014-09-01 68 views
5

我正在嘗試從Oracle站點[fxml教程](http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm)的JavaFX教程。無法構建javafx.application.Application實例

package fxml; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

class Main extends Application { 

    public Main() {} 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("welcome.fxml")); 

     Scene scene = new Scene(root); 

     stage.setTitle("FXML UI"); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    public static void main(String args[]) { 
     launch(args); 
    } 

} 

這是我不斷得到的例外。 應用程序不顯示在屏幕上。 我從另一個堆棧溢出答案做非參數Main()構造函數,但它沒有解決它。

Exception in Application constructor 
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class fxml.Main 
    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:745) 
Caused by: java.lang.IllegalAccessException: Class com.sun.javafx.application.LauncherImpl$7 can not access a member of class fxml.Main with modifiers "public" 
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:101) 
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:295) 
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:287) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:398) 
    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) 
+0

您需要將'public'修飾符添加到您的外部類。 – 2014-09-01 05:45:46

回答

8

class Main extends Application缺少初始public關鍵字。

另外public Main() {}是不必要的。

+0

這讓我非常感謝你 – 2017-10-02 06:42:56