You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Preferences初始化设置SplitPane分割线位置?求更优方案

启动时设置SplitPane分割线位置的临时方案(求更优解)

嘿各位大佬,我最近做JavaFX项目时碰到了启动时设置SplitPane分割线位置的需求,目前折腾出了个临时方案凑合用,但总觉得不够优雅,想请教下有没有更好的实现方式!

临时方案的代码片段

控制器类片段

public class MainController implements Initializable {
    @FXML
    private SplitPane mainSplitPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // 靠延迟执行绕开初始化阶段布局未完成的问题
        Platform.runLater(() -> {
            // 设置左侧面板占整体宽度的30%
            mainSplitPane.setDividerPositions(0.3);
        });
    }
}

FXML片段

<SplitPane fx:id="mainSplitPane" dividerPositions="0.3" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
    <!-- 左侧面板内容 -->
    <AnchorPane>
        <Label text="左侧面板" />
    </AnchorPane>
    <!-- 右侧面板内容 -->
    <AnchorPane>
        <Label text="右侧面板" />
    </AnchorPane>
</SplitPane>

补充说明

我之前还找到过一个相关的已回答问题,但翻完后发现并没有能解决我问题的有效方案,所以来这儿求助各位——有没有不需要依赖Platform.runLater()的更优思路呀?

内容的提问来源于stack exchange,提问作者Aubin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:55:42