JavaFXUI元素未显示在阶段中

JavaFXUI元素未显示在阶段中,java,javafx,Java,Javafx,我正在制作一个基本的JavaFx程序。程序在第一个场景中显示文本和按钮,单击按钮时程序将导航到另一个场景。代码运行正常,但窗口中没有显示按钮或文本。有人能解释为什么会发生这种情况吗?任何意见都将不胜感激。完整程序如下: import javafx.application.*; import javafx.application.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.Button;

我正在制作一个基本的JavaFx程序。程序在第一个场景中显示文本和按钮,单击按钮时程序将导航到另一个场景。代码运行正常,但窗口中没有显示按钮或文本。有人能解释为什么会发生这种情况吗?任何意见都将不胜感激。完整程序如下:

import javafx.application.*;
import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.*;

public class Main extends Application{

Stage window;
Scene scene1, scene2;

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

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;

    //Create Elements for scene1
    Label label = new Label("Welcome to scene 1 click button to go to scene 2");
    Button button = new Button("Go to scene 2");
    button.setOnAction(e -> window.setScene(scene2));

    //Add Elements and set layout for scene1
    StackPane layout1 = new StackPane();
    layout1.getChildren().addAll(button, label);
    scene1 = new Scene(layout1, 400, 400);

    //Create Elements for scene2
    Label label2 = new Label("This is scene 2 click button to go back to scene 1");
    Button no2button = new Button("Go back to scene 1");
    no2button.setOnAction(e -> window.setScene(scene1));

    //Add Elements and set layout for scene2
    StackPane layout2 = new StackPane();
    layout1.getChildren().addAll(no2button, label2);
    scene1 = new Scene(layout2, 400, 400);

    window.setScene(scene1);
    window.setTitle("CSS Alarm");
    window.show();
}
}
在这里:

实际上,您并没有向layout2添加任何内容,但就在下面,您将layout2设置为场景

scene1 = new Scene(layout2, 400, 400);


        window.setScene(scene1);
        window.setTitle("CSS Alarm");
        window.show();

哈哈,抱歉,我不知道我怎么错过了那一次。谢谢,这已经奏效了。
scene1 = new Scene(layout2, 400, 400);


        window.setScene(scene1);
        window.setTitle("CSS Alarm");
        window.show();