JavaFx 多种布局

1 javafx

我目前正在尝试创建此Layout

我试过使用:

StackPane rootPane = new StackPane();
Scene scene = new Scene(rootPane,...);
Pane pane1 = new Pane();
Pane pane2 = new Pane();
rootPane.getChildren().addAll(pane1,pane2);
Run Code Online (Sandbox Code Playgroud)

让我创建一个菜单栏以及它正下方的文本字段,但它不允许我因为文本字段被菜单栏隐藏。

我不确定在我的情况下需要哪些。我查看了 vbox - 这与我需要的类似,但我不确定如何在最后一行中添加 2 个表并留有间隙

如果你能指出我需要的方向,那将是一个很大的帮助。

Jam*_*s_D 5

StackPane在这里不是一个好的选择:它只是按 z 顺序将子节点堆叠在一起。我建议您阅读布局教程以获取所有内置布局窗格的完整描述,但一种选择是使用VBox. 要将项目放置在底行,您可以使用 an AnchorPane,其中一个项目锚定在左侧,另一个锚定在右侧。

这是使用这种方法的 SSCCE:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LayoutExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(5);
        root.setPadding(new Insets(5));

        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().add(new Menu("File"));

        TextArea textArea = new TextArea();
        VBox.setVgrow(textArea, Priority.ALWAYS);

        AnchorPane bottomRow = new AnchorPane();
        Label table1 = new Label("Table 1");
        table1.setStyle("-fx-background-color: gray");
        table1.setMinSize(200, 200);
        Label table2 = new Label("Table 2");
        table2.setStyle("-fx-background-color: gray");
        table2.setMinSize(200, 200);

        AnchorPane.setLeftAnchor(table1, 0.0);
        AnchorPane.setRightAnchor(table2, 0.0);
        bottomRow.getChildren().addAll(table1, table2);

        root.getChildren().addAll(menuBar, textArea, bottomRow);

        Scene scene = new Scene(root, 800, 800);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

另一种类似的方法是使用 aBorderPane作为根,菜单栏在顶部,文本区域在中心,锚点窗格在底部。