Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助解决JavaFX问题。(我对Java相当陌生)_Java - Fatal编程技术网

需要帮助解决JavaFX问题。(我对Java相当陌生)

需要帮助解决JavaFX问题。(我对Java相当陌生),java,Java,我不知道出了什么问题,我只知道我正在努力学习JavaFX,我的程序突然停止工作。如果有帮助的话,我当时正在使用NetBeans。很抱歉,如果这是一个愚蠢的问题,很容易解决,但我完全不知道出了什么问题,实际上只有2天的Java使用经验 输入: package javafx.testing; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler

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

输入:

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)

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

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

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

root
变量

相同,问题是您在声明和初始化它之前使用了
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(); 

}

每个人都知道你需要帮助,你不必在标题中提及。编辑标题以反映您所询问的实际问题。请阅读您是否偶然更新了操作系统中的java安装或Netbeans中的java版本?看起来你不知怎的丢失了javafx库。请告诉我们你的程序应该做什么,成功的输出是什么。谢谢,我不认为顺序会那么重要哈哈。