JavaFX屏幕分辨率缩放 [英] JavaFX screen resolution scaling

查看:277
本文介绍了JavaFX屏幕分辨率缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找执行此操作的方法,但我只找到此问题没有答案.

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

我正在使用1366x768笔记本电脑进行开发,我使用JavaFX创建GUI(很抱歉,但是我无法发布代码,它基本上是一个AnchorPane,其中包含几个GridPanes,其中包含更多的GridPanes,其中包含Labels, TextFields和几个图表).

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).

当我以其他分辨率(1920x1080或1600x900)加载该项目时,就会出现问题.

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

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

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.

我希望我已经很好地解释了我的问题,我的英语还远远不够完美.

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?

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

(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).

谢谢大家.

更新:

我刚刚创建了一个小例子.这是默认JavaFX AnchorPane中的2x1 GridPane,我在网格窗格中添加了两个示例Button.

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.

<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>

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

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.

TL; DR:该示例代码中的按钮位于屏幕的左上角,其余部分在我提高分辨率时只是一个空白.

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.

更新2; 忘记了Java代码.

Update 2; Forgot the Java Code.

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

推荐答案

您需要在子级上设置锚定窗格约束.在您的情况下,网格窗格

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

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

这将在您的

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

这将最大化容器中的Child组件,因此从每一侧开始0 0 0 0像素.

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

这篇关于JavaFX屏幕分辨率缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆