JavaFX:为什么stage.setResizable(false)会导致额外的边距?

这个小型JavaFX测试应用程序

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class ApplicationWithNonResizableStage extends Application { public static void main(final String[] args) { launch(args); } @Override public void start(final Stage primaryStage) throws Exception { final Rectangle rectangle = new Rectangle(200, 100, Color.POWDERBLUE); final BorderPane pane = new BorderPane(rectangle); final Scene scene = new Scene(pane); primaryStage.setScene(scene); primaryStage.setResizable(false); primaryStage.show(); } } 

产生一个不需要的填充窗口:

保证金阶段

删除调用primaryStage.setResizable(false)也会删除效果:

无保证金的阶段

出了什么问题?

正如已经评论过的,这种不同的行为!/ resizable闻起来像一个bug(有人可能会考虑提交问题;-)

较短(比手动resize)方式是明确地将舞台适合场景:

 primaryStage.setScene(scene); primaryStage.setResizable(false); primaryStage.sizeToScene(); 

刚刚注意到这适用于jdk8,但不适用于jdk7。

为方便起见,更新了一个错误:由jewelsea提交的原始报告作为(在新坐标中)的副本关闭了https://bugs.openjdk.java.net/browse/JDK-8089008 – 仍然打开,评论为win-只要。

虽然这不是解释,但它解决了这个问题:

 @Override public void start(final Stage primaryStage) throws Exception { final Dimension d = new Dimension(210, 110); final Rectangle rectangle = new Rectangle(d.width, d.height, Color.POWDERBLUE); final BorderPane pane = new BorderPane(rectangle); pane.maxWidth(d.height); pane.maxWidth(d.width); final Scene scene = new Scene(pane, d.width, d.height); primaryStage.setScene(scene); primaryStage.setResizable(false); primaryStage.setWidth(d.width); primaryStage.setHeight(d.height); primaryStage.show(); } 

关键是在适当的时间设置Stage宽度和高度。

尝试为您想要框架的Dimension name = new Dimension(height,width)创建尺寸 – Dimension name = new Dimension(height,width) – 然后将尺寸尺寸设置为矩形BorderPane和Scene。 你确实希望一切都大小正确吗?