2014-03-02 65 views
0

我無法運行此代碼。我從oracle教程中得到了這個。 這是一個簡單的hello world應用程序。我不能編譯它,但現在在將包含jfxrt.jar文件的路徑包含在classpath中之後,我可以編譯但現在無法運行。編譯並使用javFX運行

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

class A extends Application { 

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

    public void start(Stage primaryStage) { 
     primaryStage.setTitle("Hello World!"); 
     Button btn = new Button(); 
     btn.setText("Say 'Hello World'"); 
     btn.setOnAction(new EventHandler <ActionEvent>() { 


      public void handle(ActionEvent event) { 
       System.out.println("Hello World!"); 
      } 
     }); 

     StackPane root = new StackPane(); 
     root.getChildren().add(btn); 
     primaryStage.setScene(new Scene(root, 300, 250)); 
     primaryStage.show(); 
    } 
} 

我得到這些下列錯誤:

Exception in Application constructor 
Exception in thread "main" java.lang.RuntimeException: Unable to construct Appli 
cation instance: class A 
     at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherIm 
pl.java:393) 
     at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java: 
47) 
     at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) 
     at java.lang.Thread.run(Thread.java:744) 
Caused by: java.lang.NoSuchMethodException: A.<init>() 
     at java.lang.Class.getConstructor0(Class.java:2810) 
     at java.lang.Class.getConstructor(Class.java:1718) 
     at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherIm 
pl.java:275) 
     ... 3 more 

幫我了,怎麼解決這個問題。 感謝

回答

1

使類公共

public class A extends Application 

那麼它應該工作。

無論如何,你是否需要另一個導入聲明?我認爲ActionEvent的輸入缺失:

import javafx.event.ActionEvent; 
+0

哎!非常感謝。他工作過。現在你能向我解釋啓動(args)的意義嗎,它究竟做了什麼? – user3367768

+0

also primaryStage.setScene(new Scene(root,300,250)); primaryStage.show(); 其實我是新來的這個javafx包,所以沒有意識到這些方法和類。雖然我知道關於javax.swing evrythng,因爲我廣泛使用它。謝謝 – user3367768

+0

如果你不熟悉JavaFX並且想要使用它,我會建議從一些基本的教程開始。他們會給你一個很好的概述。例如從這裏開始:http://www.oracle.com/technetwork/java/javafx/documentation/index.html – elToro