问题描述
如何实现整个SplitPane的比例调整大小?
How can I achieve proportional resize of whole SplitPane?
public class JfxSplitPaneTest extends Application {
@Override
public void start(Stage stage) throws Exception {
SplitPane split = new SplitPane();
StackPane child1 = new StackPane();
child1.setStyle("-fx-background-color: #0000AA;");
StackPane child2 = new StackPane();
child2.setStyle("-fx-background-color: #00AA00;");
StackPane child3 = new StackPane();
child3.setStyle("-fx-background-color: #AA0000;");
split.getItems().addAll(child1, child2, child3);
//split.setDividerPositions(0.1f, 0.6f, 0.9f)
stage.setScene(new Scene(split, 400, 300));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
启动程序:
设置分隔符我喜欢它们:
Set dividers how I like them:
使窗口宽度非常小(我继续使它变得更小,没有图片):
Make window width really small (I continued and made it even smaller, no pic):
调整大小并且观察分频器既不是我设置它们也不是它们在程序启动时的方式。
Resize back and observe that dividers are neither how I set them nor how they were when program started.
这样做会使它更接近我的期望:
Doing this makes it closer to what I'd expect:
child1.setMinWidth(Region.USE_PREF_SIZE)
child1.setPrefWidth(100)
child2.setMinWidth(Region.USE_PREF_SIZE)
child2.setPrefWidth(200)
child3.setMinWidth(Region.USE_PREF_SIZE)
child3.setPrefWidth(300)
除了分频器最初处于错误的位置(直到我调整SplitPane的大小,1px就足够了)并且当缩小窗口宽度时,分频器在组件内部。
except that dividers are initally at wrong position (until I resize SplitPane, 1px is enough) and when shrinking window width, dividers are inside components.
我怎样才能做到这一点好吗?
How can I make this work please?
推荐答案
尝试将Change Listener添加到 stage.widthProperty()和 stage.heightProperty()即可。并称之为:
Try to add Change Listener to stage.widthProperty() and stage.heightProperty(). And call this:
split.setDividerPositions(0.1f, 0.6f, 0.9f)
这篇关于JavaFX - SplitPane,调整大小&比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!