如何在SplitPane JavaFX中锁定分隔符?(How to lock the divider in SplitPane JavaFX?)

我有一个SplitPane,我需要将布局分为25%和75%。 另外,我需要禁止拖拽超过25%的分割。 但是我可以在25%的空间内拖动到任何程度。 请帮忙。

I have a SplitPane and I need to divide the layout 25% and 75%. Also, I need to disallow dragging towards right side beyond the 25% split. However I can drag to any extent within the 25% space. Please help.

最满意答案

SplitPane将尊重它包含的组件( items )的最小和最大尺寸。 因此,要获得所需的行为,请将左侧组件的maxWidth绑定到splitPane.maxWidthProperty().multiply(0.25) :

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.SplitPane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class ConstrainedSplitPane extends Application { @Override public void start(Stage primaryStage) { StackPane leftPane = new StackPane(new Label("Left")); StackPane rightPane = new StackPane(new Label("Right")); SplitPane splitPane = new SplitPane(); splitPane.getItems().addAll(leftPane, rightPane); splitPane.setDividerPositions(0.25); //Constrain max size of left component: leftPane.maxWidthProperty().bind(splitPane.widthProperty().multiply(0.25)); primaryStage.setScene(new Scene(new BorderPane(splitPane), 800, 600)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }

SplitPane will respect the min and max dimensions of the components (items) it contains. So to get the behavior you want, bind the maxWidth of the left component to splitPane.maxWidthProperty().multiply(0.25):

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.SplitPane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class ConstrainedSplitPane extends Application { @Override public void start(Stage primaryStage) { StackPane leftPane = new StackPane(new Label("Left")); StackPane rightPane = new StackPane(new Label("Right")); SplitPane splitPane = new SplitPane(); splitPane.getItems().addAll(leftPane, rightPane); splitPane.setDividerPositions(0.25); //Constrain max size of left component: leftPane.maxWidthProperty().bind(splitPane.widthProperty().multiply(0.25)); primaryStage.setScene(new Scene(new BorderPane(splitPane), 800, 600)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }

更多推荐