java - JavaFX 在不同屏幕上的不同尺寸

标签 java javafx size resolution borderpane

我一直在努力使我的 JavaFX 应用程序在不同设备上保持适当的大小。我认为这是由于屏幕分辨率不同而发生的。我在分辨率为 2560x1600 的 Macbook Pro Retina 13 上进行所有开发,而运行 1920x1080 的 Windows 桌面上的窗口看起来有所不同。我用两张图来说明。

2560x1600

2560x1600

1920x1080

1920x1080

我的第一个猜测是,这是因为 BorderPane 在 1920x1080 上尺寸会增加。所以我尝试使用以下代码来修复大小。

<BorderPane maxHeight="304.0" maxWidth="414.0" minHeight="304.0" 
minWidth="414.0" prefHeight="304.0" prefWidth="414.0" 
xmlns="http://javafx.com/javafx/8.0.40" 
xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.GUIController">

但这并没有改变任何事情。有什么想法可以解决这个问题吗?

编辑:我希望它基本上看起来像2560x1600

编辑:FXML 文件如下所示。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.GUIController">
   <children>
      <BorderPane>
         <center>
            <ScrollPane BorderPane.alignment="CENTER">
              <content>
                <AnchorPane>
                     <children>
                        <TableView fx:id="itemList" prefHeight="214.0" prefWidth="412.0">
                          <columns>
                            <TableColumn fx:id="nameColumn" editable="false" resizable="false" text="Name" />
                            <TableColumn fx:id="powerColumn" editable="false" resizable="false" text="Power" />
                              <TableColumn fx:id="typeColumn" editable="false" resizable="false" text="Type" />
                          </columns>
                           <columnResizePolicy>
                              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                           </columnResizePolicy>
                        </TableView>
                     </children>
                  </AnchorPane>
              </content>
            </ScrollPane>
         </center>
         <bottom>
            <HBox alignment="CENTER_LEFT" BorderPane.alignment="CENTER">
               <children>
                  <HBox prefHeight="40.0" prefWidth="6.0" />
                  <Label prefHeight="17.0" prefWidth="37.0" text="Item: ">
                     <font>
                        <Font name="System Regular" size="12.0" />
                     </font>
                  </Label>
                  <ComboBox fx:id="newCBox" onAction="#addItemClicked" prefHeight="27.0" prefWidth="112.0" promptText="New" />
                  <HBox prefHeight="40.0" prefWidth="40.0" />
                  <Button mnemonicParsing="false" onAction="#importClicked" prefHeight="27.0" prefWidth="64.0" text="Import" />
                  <HBox prefHeight="40.0" prefWidth="5.0" />
                  <Button mnemonicParsing="false" onAction="#removeClicked" prefHeight="27.0" prefWidth="68.0" text="Remove" />
                  <HBox prefHeight="40.0" prefWidth="5.0" />
                  <Button mnemonicParsing="false" onAction="#upgradeClicked" prefHeight="27.0" prefWidth="72.0" text="Upgrade" />
               </children>
            </HBox>
         </bottom>
         <top>
            <VBox alignment="CENTER_RIGHT" prefHeight="58.0" prefWidth="414.0" BorderPane.alignment="CENTER">
               <children>
                  <MenuBar>
                    <menus>
                      <Menu mnemonicParsing="false" text="File">
                        <items>
                          <MenuItem fx:id="savedMenu" mnemonicParsing="false" onAction="#saveClicked" text="Save" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                              <MenuItem fx:id="openMenu" mnemonicParsing="false" onAction="#openClicked" text="Open" />
                        </items>
                      </Menu>
                        <Menu mnemonicParsing="false" text="Item">
                          <items>
                              <MenuItem fx:id="editItemMenu" mnemonicParsing="false" onAction="#editItemClicked" text="Edit" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                            <MenuItem fx:id="importMenu" mnemonicParsing="false" onAction="#importClicked" text="Import" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                              <MenuItem fx:id="removeMenu" mnemonicParsing="false" onAction="#removeClicked" text="Remove" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                              <MenuItem fx:id="upgradeMenu" mnemonicParsing="false" onAction="#upgradeClicked" text="Upgrade" />
                          </items>
                        </Menu>
                      <Menu mnemonicParsing="false" text="Profile">
                        <items>
                          <MenuItem id="editValues" fx:id="editValues" mnemonicParsing="false" onAction="#editValuesClicked" text="Edit &amp; New" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                              <MenuItem mnemonicParsing="false" onAction="#shareProfileClicked" text="Share Profile" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                              <MenuItem mnemonicParsing="false" onAction="#loadProfileClicked" text="Load Profile" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                              <MenuItem mnemonicParsing="false" onAction="#removeProfileClicked" text="Remove" />
                        </items>
                      </Menu>
                      <Menu mnemonicParsing="false" text="Help">
                        <items>
                          <MenuItem mnemonicParsing="false" onAction="#aboutClicked" text="About" />
                              <SeparatorMenuItem mnemonicParsing="false" />
                              <MenuItem mnemonicParsing="false" onAction="#newsClicked" text="News" />
                        </items>
                      </Menu>
                    </menus>
                  </MenuBar>
                  <HBox alignment="CENTER_LEFT">
                     <children>
                        <HBox prefHeight="31.0" prefWidth="5.0" />
                        <Label text="Display: " />
                        <ComboBox fx:id="displayCBox" onAction="#displayChanged" prefHeight="27.0" prefWidth="149.0" promptText="Display type" />
                        <HBox prefHeight="31.0" prefWidth="59.0" />
                        <ComboBox fx:id="profileCBox" onAction="#profileChanged" prefHeight="27.0" prefWidth="143.0" promptText="Choose Profile" />
                     </children>
                  </HBox>
               </children>
            </VBox>
         </top>
      </BorderPane>
   </children>
</AnchorPane>

最佳答案

就像 James_D 在评论中指出的那样,我的代码中有冗余节点。我使用了场景生成器,他们默认添加了不需要的 anchor Pane ,所以我假设它是准确的。然而事实并非如此。

关于java - JavaFX 在不同屏幕上的不同尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35523529/

相关文章:

java - 如何从列表 <String> 中删除换行符

java - java中静态方法覆盖的奇怪情况

java - FileWriter 仅写入第一行(启用追加模式,Java)

java - 当引用的方法抛出异常时,如何以一个方法作为参数调用另一个方法?

java - 底部/顶部对齐 JavaFX 文本内容(标签/文本区域)

java - 如何使用 Java 11 bundle 和启动原生 Mac/Windows 应用程序?

javafx - 按 F4 禁用组合框下拉菜单

ios - UIKeyboardWillShowNotification & UIKeyboardDidShowNotification 报错键盘高度

image - docker 和图像大小限制

mongodb - 如何找到 MongoDB 集合中占用大量空间的元素?