gpt4 book ai didi

javafx - 设置SplitPane的分隔线位置

转载 作者:行者123 更新时间:2023-12-02 06:51:07 24 4
gpt4 key购买 nike

我想将 SplitPane 的分隔线设置为某个默认位置。这不起作用,分隔线保持在中间:

public void start(Stage primaryStage) throws Exception{

SplitPane splitPane = new SplitPane(new Pane(), new Pane());

// Report changes to the divider position
splitPane.getDividers().get(0).positionProperty().addListener(
o -> System.out.println(splitPane.getDividerPositions()[0])
);

// Doesn't work:
splitPane.setDividerPositions(0.8);

// The docs seem to recommend the following (with floats instead of
// doubles, and with one number more than there are dividers, which is
// weird), but it doesn't work either:
//splitPane.setDividerPositions(0.8f, 0.2f);

primaryStage.setScene(new Scene(splitPane));
primaryStage.setMaximized(true);
primaryStage.show();
}

输出:

0.8
0.5

screenshot of result

这表明某些东西将其重置到中间。

我怎样才能实现这个目标?

最佳答案

问题似乎是在最大化 Stage 期间设置 SplitPane 宽度时,分隔线位置会重置。之后通过监听窗口的显示属性来设置分隔线位置,如下所示:

primaryStage.showingProperty().addListener(new ChangeListener<Boolean>() {

@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue) {
splitPane.setDividerPositions(0.8);
observable.removeListener(this);
}
}
});

关于javafx - 设置SplitPane的分隔线位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com