首页 > 解决方案 > JavaFX Borderpane 与 Splitpane 相结合

问题描述

我对 JavaFX 很陌生。对于一个学校项目,我必须创建一个包含三个扇区(左、中和下)的 JavaFX 应用程序,必须通过拖动分隔线来调整它们的大小。

为了创建此布局,我尝试使用 BorderPane(用于部分)并将其与 SplitPane 组合以使其可调整大小。但我不知道如何组合它。这甚至可能还是我需要另一个窗格对象?

BorderPane root = new BorderPane();
SplitPane splitPane = new SplitPane();
ScrollPane leftPane = new ScrollPane(new Button("Button 1"));
ScrollPane bottomPane = new ScrollPane(new Button("Button 2"));
FlowPane centerPane = new FlowPane(new Button("Button 3"));

//splitPane.getItems().addAll(leftPane, centerPane, bottomPane);
//root.getChildren().add(splitPane);
root.setLeft(leftPane);
root.setCenter(centerPane);
root.setBottom(bottomPane);

标签: javajavafx

解决方案


只需使用两个SplitPanes具有不同方向的(并忘记BorderPane):

package org.example;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class SplitPanesExampleApp extends Application {

    @Override
    public void start(Stage stage) {

        // Creating the controls:
        ScrollPane leftPane = new ScrollPane(new Button("Left")),
                bottomPane = new ScrollPane(new Button("Bottom"));

        FlowPane centerPane = new FlowPane(new Button("Center (or right)"));

        SplitPane horizontalSplitPane = new SplitPane(leftPane, centerPane),
                verticalSplitPane = new SplitPane(horizontalSplitPane, bottomPane);

        // Setting orientations:
        verticalSplitPane.setOrientation(Orientation.VERTICAL);
        // horizontalSplitPane.setOrientation(Orientation.HORIZONTAL); // horizontal is the default value

        // Setting initial divider positions:
        verticalSplitPane.getDividers().get(0).setPosition(.8);
        horizontalSplitPane.getDividers().get(0).setPosition(.2);

        // Prepare and show stage:
        stage.setScene(new Scene(verticalSplitPane, 600, 400));
        stage.show();
    }

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


推荐阅读