2014-03-02 129 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