【问题标题】:JavaFX - Text is bigger when compiled than in SceneBuilderJavaFX - 编译时的文本比 SceneBuilder 中的大
【发布时间】:2018-08-28 11:03:20
【问题描述】:

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

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

【问题讨论】:

    标签: javafx scenebuilder


    【解决方案1】:

    字体大小/精确控件大小可能因不同的 JavaFX 版本/操作系统而异。你不应该在这里使用绝对位置。我建议使用一种布局,它会根据孩子的大小来选择孩子的位置。合适的布局是GridPane(如果你想确保单元格的数量保持不变,不管字体大小窗口大小等。ComboBox应该水平对齐)或FlowPane(如果你想放置一堆@ 987654324@es 类似于文本,不关心水平对齐或每行 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>
    ...
    

    【讨论】:

    • 谢谢,我为应用制作了一个 CSS 样式来设置所有文本的文本大小。它可以帮助我解决 JavaFX 版本的这个问题。我还使用了您关于布局的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多