需要有关修复JavaFX问题的帮助。 (我对Java很新)

时间:2017-07-27 20:10:58

标签: java

我不知道出了什么问题,我所知道的是我正在努力学习JavaFX并且我的程序突然停止工作。如果有帮助的话,我当时正在使用NetBeans。很抱歉,如果这是一个简单的问题,但很容易解决,但我完全不知道出了什么问题,而且使用Java只有2天的经验。

输入:

package javafx.testing;

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

public class JavaFXTesting extends Application {

    //main
    blic static void main(String[] args) {

        launch(args);

    }

    @Override
    public void start(Stage primaryStage) {

        primaryStage.show(); //shows the window
        primaryStage.setScene(helloworldscene);
        primaryStage.setTitle("JavaFX Testing"); //sets the window title

        Scene helloworldscene = new Scene(root, 500, 300); //creates a new scene

        Button helloworldbutton = new Button("Hello World"); //creates the button and gives it a display name
        helloworldbutton.setOnAction(e -> System.out.println("Hello World")); //prints "Hello World"

        StackPane root = new StackPane(); //no idea what the hell this one does
        root.getChildren().add(helloworldbutton);


    }

}

输出:

ant -f "C:\\Users\\andym\\Documents\\NetBeansProjects\\JavaFX Testing" jfxsa-run
init:
Deleting: C:\Users\andym\Documents\NetBeansProjects\JavaFX Testing\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\andym\Documents\NetBeansProjects\JavaFX Testing\build\built-jar.properties
Compiling 1 source file to C:\Users\andym\Documents\NetBeansProjects\JavaFX Testing\build\classes
C:\Users\andym\Documents\NetBeansProjects\JavaFX Testing\src\javafx\testing\JavaFXTesting.java:67: error: cannot find symbol
        primaryStage.setScene(helloworldscene);
  symbol:   variable helloworldscene
  location: class JavaFXTesting
C:\Users\andym\Documents\NetBeansProjects\JavaFX Testing\src\javafx\testing\JavaFXTesting.java:70: error: cannot find symbol
        Scene helloworldscene = new Scene(root, 500, 300); //creates a new scene
  symbol:   variable root
  location: class JavaFXTesting
2 errors
C:\Users\andym\Documents\NetBeansProjects\JavaFX Testing\nbproject\build-impl.xml:931: The following error occurred while executing this line:
C:\Users\andym\Documents\NetBeansProjects\JavaFX Testing\nbproject\build-impl.xml:271: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

2 个答案:

答案 0 :(得分:1)

问题是您在声明并初始化之前使用了root

public void start(Stage primaryStage) {
        ...
        Scene helloworldscene = new Scene(root, 500, 300); //error : root not defined
        ...
        StackPane root = new StackPane(); 
        root.getChildren().add(helloworldbutton);
    }

那你该怎么办?首先,您需要声明根,然后创建场景,然后显示阶段

像这样:

@Override
public void start(Stage primaryStage) {

    //1 - create the root
    StackPane root = new StackPane();
    //2 - add nodes to the root
    Button helloworldbutton = new Button("Hello World");
    root.getChildren().add(helloworldbutton);
    helloworldbutton.setOnAction(e -> System.out.println("Hello World"));
    //4 - create the scene with the root
    Scene helloworldscene = new Scene(root, 500, 300);
    //5 - finally set the scene to the stage and show it
    primaryStage.setScene(helloworldscene);
    primaryStage.setTitle("JavaFX Testing");
    primaryStage.show(); 

}

答案 1 :(得分:0)

您收到未定义变量的编译错误。 您在实际声明它们之前使用变量。在您的代码中,它们是在您尝试使用它们之后定义的。

primaryStage.setScene(helloworldscene);
...
Scene helloworldscene = new Scene(root, 500, 300);

应该是

Scene helloworldscene = new Scene(root, 500, 300);
primaryStage.setScene(helloworldscene);

root变量

相同