JavaFX 在不同屏幕上的不同大小

Different sizes in JavaFX on different screens

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

2560x1600

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 指出的那样,我的代码中存在冗余节点。我使用过 Scene Builder,他们默认添加了不需要的锚点窗格,所以我假设它是准确的。然而,事实并非如此。