我在 BorderPane(的中心)内遇到 FlowPane 的奇怪行为。 
如果我放大或缩小窗口的宽度,一切都很好。导致这种效果的只是一个狭窄的宽度 +- 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();
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么还是这是一个布局错误?
这似乎是一个奇怪的错误。我添加了一些调试信息。
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());
});
Run Code Online (Sandbox Code Playgroud)
当程序启动时。两个字段窄字段并排,首选大小为:
盒式:95.0、95.0、95.0
流量:64.0、64.0、64.0
当我们减小窗口的宽度时,FlowPane 会切换方向,但它的最小尺寸和首选尺寸不会改变。
盒式:95.0、95.0、95.0
流量:64.0、64.0、101.0
当我们进一步减少时,布局又看起来不错了。
框:132.0、132.0、132.0
流:101.0、101.0、101.0
因此,BorderPane 似乎使用 FlowPane 的正确宽度进行“布局”,但在请求首选高度时使用了不正确的宽度。例如
flow.prefHeight(box.getWidth() - 14);
Run Code Online (Sandbox Code Playgroud)
那个首选。高度遵循 FlowLayout 的实际高度。14 从内边距和边框。
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |