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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20732100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:52:42  来源:igfitidea点击:

JavaFX: Why does stage.setResizable(false) cause additional margins?

javajavafxwindows-7-x64javafx-8

提问by Jens Piegsa

This small JavaFX test application

这个小型 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();
    }
}

produce a window with unwanted padding:

生成一个带有不需要的填充的窗口:

stage with margin

有余量的阶段

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

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

stage without margin

无余地阶段

What is going wrong?

出了什么问题?

采纳答案by kleopatra

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

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

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

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

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

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

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

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.

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

回答by Mikhail_Bjornson

Try creating a dimension for the size you want the frame- Dimension name = new Dimension(height,width)- then set that Dimension size to the rectangle BorderPane and Scene. You do want everything to be the same size correct?

尝试为您想要框架的大小创建一个维度Dimension name = new Dimension(height,width)- 然后将该维度大小设置为矩形 BorderPane 和 Scene。您确实希望所有东西的大小都相同吗?

回答by Jens Piegsa

Although this is not explanation, it solves the problem:

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

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

Key is setting width and height of the Stageat the right time.

关键是Stage在正确的时间设置宽度和高度。