设置SplitPane的分隔位置 [英] Set divider position of SplitPane
本文介绍了设置SplitPane的分隔位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将SplitPane的分隔符设置为某个默认位置。这不起作用,分隔符保持在中间:
I want to set the divider of a SplitPane to a certain default position. This does not work, the divider stays in the middle:
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
这表示有些东西会重置到中间。
It suggests that something resets it to the middle.
我该怎么办?实现这个?
How can I achieve this?
推荐答案
问题似乎是当 SplitPane的宽度时重置分隔符位置
是在 Stage
最大化期间设置的。您应该可以通过收听窗口的显示
属性来设置它:
The issue seems to be the divider position being reset when the width of the SplitPane
is set during the maximizing of the Stage
. You should be able to set it afterwards by listening to the showing
property of the window:
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);
}
}
});
这篇关于设置SplitPane的分隔位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文