我正在制作一个基本的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();
}
}
答案 0 :(得分:1)
这里:
StackPane layout2 = new StackPane();
layout1.getChildren().addAll(no2button, label2);
scene1 = new Scene(layout2, 400, 400);
您实际上并没有向layout2添加任何内容,但在此下方您将布局2设置为场景
scene1 = new Scene(layout2, 400, 400);
window.setScene(scene1);
window.setTitle("CSS Alarm");
window.show();