我是JavaFX的新手。我正在嘗試創建一個簡單的居中菜單,其中包含帶下面按鈕的文本。JavaFX - 如何將多個元素添加到StackPane?
我創建了兩個元素Text title
和Button testButton
。然後我創建了StackPane stackPane
。然後,我試圖將這兩個元素添加到stackPanes
孩子,並將其添加到new Scene
。但是,只有最後一個元素出現。
如何添加多個元素到StackPane?
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Test Title");
Text title = new Text("hey!");
StackPane.setAlignment(title, Pos.TOP_CENTER);
Button testButton = new Button("Testing");
StackPane.setAlignment(testButton, Pos.TOP_CENTER);
StackPane stackPane = new StackPane();
stackPane.setPrefSize(300, 300);
stackPane.setPadding(new Insets(25, 0, 0, 0));
stackPane.getChildren().add(title);
stackPane.getChildren().add(testButton);
Scene scene = new Scene(stackPane);
primaryStage.setScene(scene);
primaryStage.show();
}
它們都在那裏,它們只是堆疊在一起(這是StackPane如何展示它的子節點)。只需使用佈局窗格,按照自己的想法擺放它們即可。 –