JavaFX - 编译时文本比在 SceneBuilder 中大

JavaFX - Text is bigger when compiled than in SceneBuilder

我在 JavaFX 中遇到问题。当我 运行 编程时文本比在 SceneBuilder 中大。

运行 程序: JavaFX Window 场景生成器: Window in SceneBuilder

这是 fxml 文件:

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="200.0" minWidth="450.0" prefHeight="200.0"prefWidth="450.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <GridPane layoutX="24.0" layoutY="14.0" prefWidth="406.0" AnchorPane.bottomAnchor="60.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="5.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="180.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="85.0" minWidth="10.0" prefWidth="85.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Rozszerzenie" />
            <Label text="Główny katalog" GridPane.rowIndex="1" />
            <Label text="Katolog docelowy" GridPane.rowIndex="2" />
            <Button mnemonicParsing="false" onAction="#chooseMainDirectory" text="Przeszukaj" GridPane.columnIndex="2" GridPane.rowIndex="1">
               <GridPane.margin>
                  <Insets left="10.0" />
               </GridPane.margin>
            </Button>
            <Button mnemonicParsing="false" onAction="#chooseTargetDirectory" text="Przeszukaj" GridPane.columnIndex="2" GridPane.rowIndex="2">
               <GridPane.margin>
                  <Insets left="10.0" />
               </GridPane.margin>
            </Button>
            <TextField fx:id="mainDirectory" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <TextField fx:id="targetDirectory" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <AnchorPane fx:id="choiceDeck" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
               <children>
                  <CheckBox layoutY="6.0" mnemonicParsing="false" text=".jpg" />
                  <CheckBox layoutX="48.0" layoutY="6.0" mnemonicParsing="false" text=".jpeg" />
              <CheckBox layoutX="104.0" layoutY="6.0" mnemonicParsing="false" text=".gif" />
                  <CheckBox layoutX="150.0" layoutY="6.0" mnemonicParsing="false" text=".bmp" />
                  <CheckBox layoutY="30.0" mnemonicParsing="false" text=".png" />
                  <CheckBox layoutX="49.0" layoutY="30.0" mnemonicParsing="false" text=".tiff" />
               </children>
            </AnchorPane>
         </children>
      </GridPane>
      <Button layoutX="368.0" layoutY="161.0" mnemonicParsing="false" onAction="#confirm" text="Zatwierdź" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0" />
      <Button layoutX="14.0" layoutY="161.0" mnemonicParsing="false" onAction="#cancel" text="Anuluj" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" />
   </children>
</AnchorPane>

请帮我解决这个问题。在另一台电脑上它工作正常。

字体 size/exact 控件大小在不同的 JavaFX versions/OS 之间可能不同。你不应该在这里使用绝对位置。我建议使用一种布局,而不是根据它们的大小选择 children 的位置。合适的布局是 GridPane(如果你想确保单元格的数量保持不变,无论字体大小 window 大小等,ComboBox 应该水平对齐)或 FlowPane (如果你想放置一堆类似于文本的 ComboBoxes 并且不关心水平对齐方式或每行 ComboBoxes 的确切数量)。

...
<FlowPane fx:id="choiceDeck" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
  <children>
      <CheckBox mnemonicParsing="false" text=".jpg" />
      <CheckBox mnemonicParsing="false" text=".jpeg" />
      <CheckBox mnemonicParsing="false" text=".gif" />
      <CheckBox mnemonicParsing="false" text=".bmp" />
      <CheckBox mnemonicParsing="false" text=".png" />
      <CheckBox mnemonicParsing="false" text=".tiff" />
   </children>
</FlowPane>
...
...
<GridPane fx:id="choiceDeck" prefHeight="200.0" prefWidth="200.0" hgap="3" vgap="5" GridPane.columnIndex="1">
  <children>
      <CheckBox mnemonicParsing="false" text=".jpg" />
      <CheckBox GridPane.columnIndex="1" mnemonicParsing="false" text=".jpeg" />
      <CheckBox GridPane.columnIndex="2" mnemonicParsing="false" text=".gif" />
      <CheckBox GridPane.columnIndex="3" mnemonicParsing="false" text=".bmp" />
      <CheckBox GridPane.rowIndex="1" mnemonicParsing="false" text=".png" />
      <CheckBox GridPane.rowIndex="1" GridPane.columnIndex="1" mnemonicParsing="false" text=".tiff" />
   </children>
</GridPane>
...