java JavaFX 屏幕分辨率缩放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36469958/
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-11-03 01:29:55  来源:igfitidea点击:

JavaFX screen resolution scaling

javajavafxfxmlscreen-resolution

提问by Mayuso

I have been searching for a method to do this, but I only found This question without answers.

我一直在寻找一种方法来做到这一点,但我只找到了这个没有答案的问题

I am developing in a 1366x768 laptop, I use JavaFX to create a GUI (I am sorry, but I can't post the code, it's basically an AnchorPane containing a couple GridPanes, which contain more GridPanes, which contain Labels, TextFields and a couple Charts).

我正在 1366x768 的笔记本电脑上开发,我使用 JavaFX 创建一个 GUI(对不起,但我不能发布代码,它基本上是一个包含几个 GridPanes 的 AnchorPane,其中包含更多的 GridPanes,其中包含标签、TextFields 和一个夫妇图表)。

The problem comes when I load the project on another resolution (1920x1080 or 1600x900).

当我以另一种分辨率(1920x1080 或 1600x900)加载项目时,问题就出现了。

I am using stage.setMaximized(true);, but this only Scales the background, the stage itself, so all the elements and containers (Panels, Labels, TextFields.....) remain the same size as with the 1366x768 resolution, making the GUI "not fit" at resolutions that are not exactly 1366x768.

我正在使用stage.setMaximized(true);,但这只会缩放背景、舞台本身,因此所有元素和容器(面板、标签、文本字段.....)保持与 1366x768 分辨率相同的大小,使 GUI“不适合”在不完全是 1366x768 的分辨率下。

I hope I explained my problem well enough, my english is far from perfect.

我希望我能很好地解释我的问题,我的英语远非完美。

Does anyone have any suggestion on how to make elements scale with stage resolution?

有没有人对如何根据舞台分辨率使元素缩放有任何建议?

(I need the software to be maximised or full screen in every monitor, so making a fixed resolution (1366x768, for example) doesn't seem like a good solution).

(我需要在每台显示器上最大化或全屏显示软件,因此固定分辨率(例如 1366x768)似乎不是一个好的解决方案)。

Thank you all.

谢谢你们。

Update:

更新:

I just created a little example. It's a 2x1 GridPane inside the default JavaFX AnchorPane, I added two example Buttons to the grid pane.

我只是创建了一个小例子。它是默认 JavaFX AnchorPane 内的一个 2x1 GridPane,我在网格窗格中添加了两个示例按钮。

<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >
<children>
    <GridPane prefHeight="300.0" prefWidth="400.0">
        <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
        <children>
            <Button fx:id="example1" mnemonicParsing="false" text="Example 2" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Button fx:id="example2" mnemonicParsing="false" text="Example 2" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
        </children>
    </GridPane>
</children>

This code looks nice in a 400x300 screen (Too little, I know, it's just an example), when I scale it to 1366x768 (Not real full screen, just stage.setMaximized(true);), the GridPane does not scale, only the Stage itself, so I have a full screen background, with a 400x300 Pane (With its tiny Buttons) on the top-left corner of my screen.

这段代码在 400x300 屏幕上看起来不错(太少了,我知道,这只是一个例子),当我将它缩放到 1366x768(不是真正的全屏,只是stage.setMaximized(true);)时,GridPane 不缩放,只有舞台本身,所以我有全屏背景,屏幕左上角有一个 400x300 的窗格(带有小按钮)。

TL;DR: The example code the buttons stay in the top-left corner of the screen, and the rest is just an empty space when I scale up resolution.

TL;DR:示例代码将按钮保留在屏幕的左上角,当我放大分辨率时,其余部分只是一个空白区域。

Update 2;Forgot the Java Code.

更新2;忘记了 Java 代码。

Parent root = FXMLLoader.load(getClass().getResource("UI.fxml"));
Scene scene = new Scene(root, java.awt.Toolkit.getDefaultToolkit().getScreenSize().width,
            java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
stage = new Stage();
stage.setMaximized(false);
stage.setScene(scene);
stage.setTitle("Example Menu");
stage.show();

采纳答案by Tomas Bisciak

You need to set anchor pane constraints on child. in your case gridpane

您需要在 child 上设置锚定窗格约束。在你的情况下

AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"

This will go after your

这将在您之后

 GridPane prefHeight="300.0" prefWidth="400.0" **HERE** 

This will maximize Child component within container , hence 0 0 0 0 pixels from each side .

这将最大化容器内的子组件,因此每边 0 0 0 0 像素。