本文介绍了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的这种不同行为闻起来像一个bug(有人可能会考虑提交问题;-)

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提交的原始报告已作为(新坐标) - 仍然开放,评论为仅赢。

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)会导致额外的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 21:01