不确定为什么我的JavaFX GUI不会加载?

时间:2018-03-18 23:01:50

标签: java user-interface javafx

我正在尝试执行此代码。这是一个让用户输入两个数字之和的程序。这两个数字是随机选择的,而另一个类中的get方法是检索它们的。我试图将该方法分配给questionField标签,但是当我尝试运行和编译时,我不断收到错误。有人可以帮助我,告诉我我做错了什么吗?

public class Assignment5 extends Application {

    // TODO: Instance Variables for View Components and Model
    private MathGame math1;
    private TextField answerField;
    private Label questionField;
    private Label answerRight;
    private Label answerWrong;

    // TODO: Private Event Handlers and Helper Methods
    private void mathHandler(ActionEvent e) {
        int n = Integer.parseInt(answerField.getText());
        math1.verify();
    }


    /**
     * This is where you create your components and the model and add event
     * handlers.
     *
     * @param stage The main stage
     * @throws Exception
     */
    @Override
    public void start(Stage stage) throws Exception {
        Pane root = new Pane();
        Scene scene = new Scene(root, 400, 225); // set the size here
        stage.setTitle("Assignment5"); // set the window title here
        stage.setScene(scene);
        // TODO: Add your GUI-building code here

        // 1. Create the model
        math1 = new MathGame();
        // 2. Create the GUI components
        answerField = new TextField("");
        Button input = new Button("OK");
        questionField = new Label("");
        answerRight = new Label("Test");
        answerWrong = new Label("Test");

        // 3. Add components to the root
        root.getChildren().add(answerWrong);
        root.getChildren().add(answerRight);
        root.getChildren().add(questionField);
        root.getChildren().add(answerField);
        root.getChildren().add(input);
        //root.getChildren().add(input);

        // 4. Configure the components (colors, fonts, size, location)
        questionField.setLayoutY(100);
        questionField.setLayoutX(30);
        answerField.setLayoutX(100);
        answerField.setLayoutY(100);
        answerField.setPrefWidth(40);
        answerField.setPrefHeight(20);
        input.setLayoutX(160);
        input.setLayoutY(100);
        answerRight.setLayoutY(140);
        answerWrong.setLayoutY(165);

        // 5. Add Event Handlers and do final setup
        // 6. Show the stage
        stage.show();

    }

    /**
     * Make no changes here.
     *
     * @param args unused
     */
    public static void main(String[] args) {
        launch(args);
    }
}

这是我运行时得到的错误消息

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:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Assignment5.Assignment5
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
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:423)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$160(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: Assignment5.MathGame
    at Assignment5.Assignment5.<init>(Assignment5.java:18)
    ... 13 more
Exception running application Assignment5.Assignment5
C:\Users\owner\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

1 个答案:

答案 0 :(得分:0)

我看到你正在使用Netbeans IDE:

  

C:\用户\所有者\应用程序数据\本地\的NetBeans \缓存\ 8.2 \执行人-片段\ run.xml:53

如果你仔细阅读了你的例外,你会发现一个Runtime Exception说:Unable to construct Application instance: class Assignment5.Assignment5,netbeans允许你运行代码,即使某些类不可编译。根据{{3}和Nick Fortescue 回答者。

因为您没有发布所有代码,所以我猜,这是包装问题。

所以试试这个:

1 - 再次清理并构建项目。

2-尝试取消选中项目属性中的“编译保存”设置(构建 - &gt;编译)。

我希望这对你有所帮助。