JavaFX:BorderPane 的布局问题 - 错误或用户错误?
JavaFX: Layout problem with BorderPane - bug or user error?
我遇到了 BorderPane(中心)内的 FlowPane 的奇怪行为。
如果我扩大 window 的宽度或缩小它,一切都很好。导致这种效果的只是窄宽度 +- 5 像素。重现代码:
public class LayoutBugTest extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
TextField tf1 = new TextField("");
TextField tf2 = new TextField("");
TextField tf3 = new TextField("");
tf1.setPrefColumnCount(20);
tf2.setPrefColumnCount(10);
tf3.setPrefColumnCount(10);
FlowPane flow = new FlowPane(10,10, tf1,tf2,tf3);
BorderPane box = new BorderPane();
box.setTop(new Label("Heading"));
box.setCenter(flow);
box.setStyle("-fx-border-width: 2px; -fx-border-color: black; -fx-border-radius: 1em; -fx-padding: 5px;");
VBox anyLayout = new VBox(5, new Label("Before"), box, new Label("After"));
Scene scene = new Scene(anyLayout);
stage.setScene(scene);
stage.show();
}
}
我是不是漏掉了什么,或者这是布局错误?
这似乎是一个奇怪的错误。我添加了一些调试信息。
scene.widthProperty().addListener(evt ->{
System.out.println( "box: " + box.prefHeight(anyLayout.getWidth()) + ", " + box.minHeight(anyLayout.getWidth()) + ", " + box.getHeight() );
System.out.println( "flow: " + flow.prefHeight(box.getWidth()) + ", " + flow.minHeight(box.getWidth()) + ", " + flow.getHeight());
});
程序启动时。两个字段窄字段并排,首选大小为:
box: 95.0, 95.0, 95.0
flow: 64.0, 64.0, 64.0
当我们减小 window 的宽度时,FlowPane 会切换方向,但它的最小尺寸和首选尺寸不会改变。
box: 95.0, 95.0, 95.0
flow: 64.0, 64.0, 101.0
当我们进一步减少并且布局再次看起来不错时。
box: 132.0, 132.0, 132.0
flow: 101.0, 101.0, 101.0
所以看起来 BorderPane 正在使用 FlowPane 的正确宽度来“布局”,但是在请求首选高度时它使用了不正确的宽度。例如
flow.prefHeight(box.getWidth() - 14);
那个首长。 height 遵循 FlowLayout 的实际高度。 14 来自内边距和边框。
我遇到了 BorderPane(中心)内的 FlowPane 的奇怪行为。
如果我扩大 window 的宽度或缩小它,一切都很好。导致这种效果的只是窄宽度 +- 5 像素。重现代码:
public class LayoutBugTest extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
TextField tf1 = new TextField("");
TextField tf2 = new TextField("");
TextField tf3 = new TextField("");
tf1.setPrefColumnCount(20);
tf2.setPrefColumnCount(10);
tf3.setPrefColumnCount(10);
FlowPane flow = new FlowPane(10,10, tf1,tf2,tf3);
BorderPane box = new BorderPane();
box.setTop(new Label("Heading"));
box.setCenter(flow);
box.setStyle("-fx-border-width: 2px; -fx-border-color: black; -fx-border-radius: 1em; -fx-padding: 5px;");
VBox anyLayout = new VBox(5, new Label("Before"), box, new Label("After"));
Scene scene = new Scene(anyLayout);
stage.setScene(scene);
stage.show();
}
}
我是不是漏掉了什么,或者这是布局错误?
这似乎是一个奇怪的错误。我添加了一些调试信息。
scene.widthProperty().addListener(evt ->{
System.out.println( "box: " + box.prefHeight(anyLayout.getWidth()) + ", " + box.minHeight(anyLayout.getWidth()) + ", " + box.getHeight() );
System.out.println( "flow: " + flow.prefHeight(box.getWidth()) + ", " + flow.minHeight(box.getWidth()) + ", " + flow.getHeight());
});
程序启动时。两个字段窄字段并排,首选大小为:
box: 95.0, 95.0, 95.0
flow: 64.0, 64.0, 64.0
当我们减小 window 的宽度时,FlowPane 会切换方向,但它的最小尺寸和首选尺寸不会改变。
box: 95.0, 95.0, 95.0
flow: 64.0, 64.0, 101.0
当我们进一步减少并且布局再次看起来不错时。
box: 132.0, 132.0, 132.0
flow: 101.0, 101.0, 101.0
所以看起来 BorderPane 正在使用 FlowPane 的正确宽度来“布局”,但是在请求首选高度时它使用了不正确的宽度。例如
flow.prefHeight(box.getWidth() - 14);
那个首长。 height 遵循 FlowLayout 的实际高度。 14 来自内边距和边框。