JavaFx多个布局

时间:2017-04-20 15:41:40

标签: javafx

我目前正在尝试创建此Layout

我试过用:

StackPane rootPane = new StackPane();
Scene scene = new Scene(rootPane,...);
Pane pane1 = new Pane();
Pane pane2 = new Pane();
rootPane.getChildren().addAll(pane1,pane2);

让我创建一个菜单栏以及直接位于其下方的文本字段,但它不会让我因为文本字段被menuBar隐藏。

我不确定在我的情况下需要哪些。我看了一下vbox - 这与我需要的东西相似,但是我不确定如何在最后一行添加2个表并留有空白

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

1 个答案:

答案 0 :(得分:3)

StackPane在这里不是一个好选择:它只是将子节点以z顺序堆叠在一起。我建议您阅读tutorial on layouts以获取所有内置布局窗格的完整说明,但有一个选项是使用VBox。要将项目放在底行,您可以使用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);
    }
}

enter image description here

另一种类似的方法是使用BorderPane作为根,顶部是菜单栏,中间是文本区域,底部是锚页面。

相关问题