java - 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);
解决方案
只需使用两个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();
}
}
推荐阅读
- java - Java 反应式程序
- spring-boot - 如何在spring boot中通过POST方法读取url中的参数
- reactjs - 从 create-react-app (CRA) 转换为 CRA + Next.Js 应用
- javascript - 将子元素直接放置在父元素下方并向右对齐
- netty - Netty 4 中 DefualtChannelFuture 的替代品是什么
- python - 目录搜索返回的文件数不正确
- jq - jq 进行选择和包含的问题
- python - 来自导入模块的变量无法识别
- javascript - wProps 在反应开发工具中更新,但条件 jsx 无法正确呈现
- bash - 使用 wget 并行下载多个文件