本文介绍了JavaFX:为什么 stage.setResizable(false) 会导致额外的边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个小型 JavaFX 测试应用程序

This small JavaFX test application

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();
    }
}

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

produce a window with unwanted padding:

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

Removing the call primaryStage.setResizable(false) also removes the effect:

出了什么问题?

推荐答案

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

As already commented, this different behaviour of !/resizable smells like a bug (somebody might consider filing an issue ;-)

更短的(比手动调整大小)方法是明确地将舞台适合场景:

A shorter (than sizing manually) way around is to explicitly fit the stage to the scene:

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

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

Just noticed that this works for jdk8, but not jdk7.

为了方便起见,更新了一个错误:jewelsea 提交的原始报告已作为(在新坐标中)https://bugs.openjdk.java.net/browse/JDK-8089008 - 仍然打开,评论为仅赢.

For convenience, a bug update: the original report filed by jewelsea was closed as a duplicate of (in new coordinates) https://bugs.openjdk.java.net/browse/JDK-8089008 - still open, commented to be win-only.

这篇关于JavaFX:为什么 stage.setResizable(false) 会导致额外的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 03:13