fxml 上的 JavaFX TextField 宽度不同

标签 java javafx

我在处理一堆文本字段时遇到了一些麻烦,我希望它们具有相同的宽度,但其中一个拒绝更改。这是它现在的实际外观。

JavaFX window

正如您所见,第一个文本字段与其余文本字段不匹配。 这是 fxml 代码:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import org.kordamp.ikonli.javafx.FontIcon?>

<AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="@../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.arpentechnologies.software.core.info.FXMLInfoEmpresaController">
    <children>
        <Button id="backButton" fx:id="volverInicioButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="56.0" prefWidth="54.0" styleClass="backButton">
            <graphic>
                <FontIcon iconLiteral="mdi-keyboard-backspace" iconSize="30" wrappingWidth="30.0" />
            </graphic>
        </Button>
        <StackPane AnchorPane.leftAnchor="70.0" AnchorPane.rightAnchor="70.0" AnchorPane.topAnchor="19.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Información de la Empresa" textAlignment="CENTER" wrappingWidth="399.0546875">
                    <font>
                        <Font size="30.0" />
                    </font>
                </Text>
            </children>
        </StackPane>
        <HBox alignment="CENTER" layoutX="14.0" layoutY="107.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="95.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Nombre de la empresa" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="nombreComunTF" maxWidth="1.7976931348623157E308" prefHeight="40.0" prefWidth="0.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="15.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="14.0" layoutY="100.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="150.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Nombre Fiscal" wrappingWidth="173.3173828125" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="nombreFiscalTF" prefHeight="40.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="98.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="14.0" layoutY="194.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="205.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="CIF" wrappingWidth="83.96484375" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="cifTF" prefHeight="40.0" prefWidth="0.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="188.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="14.0" layoutY="239.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="260.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Dirección" wrappingWidth="133.1640625" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="direccionTF" prefHeight="40.0" prefWidth="0.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="138.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="15.0" layoutY="289.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="315.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Población" wrappingWidth="139.1796875" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="poblacionTF" prefHeight="40.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="132.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="12.0" layoutY="341.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="425.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Código postal" wrappingWidth="175.111328125" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="codigoPostalTF" prefHeight="40.0" prefWidth="0.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="96.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="14.0" layoutY="387.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="480.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="País" wrappingWidth="96.236328125" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="paisTF" prefHeight="40.0" prefWidth="0.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="175.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="12.0" layoutY="336.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="370.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Teléfono" wrappingWidth="132.5302734375" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="telefonoTF" prefHeight="40.0" prefWidth="0.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="139.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="12.0" layoutY="474.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="535.0">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="E-mail" wrappingWidth="112.24609375" HBox.hgrow="ALWAYS">
                    <font>
                        <Font size="24.0" />
                    </font>
                </Text>
                <TextField fx:id="emailTF" prefHeight="40.0" prefWidth="0.0" HBox.hgrow="ALWAYS">
                    <HBox.margin>
                        <Insets left="159.0" />
                    </HBox.margin>
                    <font>
                        <Font size="18.0" />
                    </font>
                </TextField>
            </children>
        </HBox>
        <HBox alignment="CENTER" layoutX="306.0" layoutY="599.0" spacing="40.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="590.0">
            <children>
                <Button fx:id="guardarButton" mnemonicParsing="false" onAction="#onClickGuardar" prefHeight="58.0" prefWidth="101.0" text="Guardar">
                    <font>
                        <Font size="18.0" />
                    </font>
                </Button>
                <Button fx:id="cancelarButton" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="58.0" prefWidth="108.0" text="Cancelar">
                    <font>
                        <Font size="18.0" />
                    </font>
                </Button>
            </children>
        </HBox>
    </children>
</AnchorPane>

知道如何修复它吗?我想让它具有响应能力,这样当屏幕尺寸改变时它看起来就不会很糟糕。

最佳答案

使用 GridPane布局,因为它使您能够创建灵活的行和列网格来布局控件:

<GridPane layoutX="76.0" layoutY="128.0" prefHeight="452.0" prefWidth="843.0">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="415.0" minWidth="10.0" prefWidth="301.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="542.0" minWidth="10.0" prefWidth="542.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
    <children>
        <TextField fx:id="nombreComunTF" maxWidth="1.7976931348623157E308" prefHeight="40.0" prefWidth="0.0" GridPane.columnIndex="1">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="nombreFiscalTF" prefHeight="40.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="cifTF" prefHeight="40.0" prefWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="direccionTF" prefHeight="40.0" prefWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="3">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="poblacionTF" prefHeight="40.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="telefonoTF" prefHeight="40.0" prefWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="5">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="codigoPostalTF" prefHeight="40.0" prefWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="paisTF" prefHeight="40.0" prefWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="7">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <TextField fx:id="emailTF" prefHeight="40.0" prefWidth="0.0" GridPane.columnIndex="1" GridPane.rowIndex="8">
            <font>
                <Font size="18.0" />
            </font>
        </TextField>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="E-mail" wrappingWidth="112.24609375" GridPane.rowIndex="8">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="País" wrappingWidth="96.236328125" GridPane.rowIndex="7">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Código postal" wrappingWidth="175.111328125" GridPane.rowIndex="6">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Teléfono" wrappingWidth="132.5302734375" GridPane.rowIndex="5">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Población" wrappingWidth="139.1796875" GridPane.rowIndex="4">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Dirección" wrappingWidth="133.1640625" GridPane.rowIndex="3">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="CIF" wrappingWidth="83.96484375" GridPane.rowIndex="2">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Nombre Fiscal" wrappingWidth="173.3173828125" GridPane.rowIndex="1">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Nombre de la empresa">
            <font>
                <Font size="24.0" />
            </font>
        </Text>
    </children>
</GridPane>

enter image description here

作为可视化操作 FXML 文件的好工具,我推荐 Scene Builder .

关于fxml 上的 JavaFX TextField 宽度不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48682399/

相关文章:

Javafx 和 wav 文件

android - 如何在 Android 上运行 JavaFX

java - 添加 FilteredList 和 SortedList 以在 Javafx 中创建搜索栏会返回 java.lang.reflect.InitationTargetException

java - 预期为 BEGIN_ARRAY,但在第 1 行第 1 列路径 $ android Retrofit gson 处为 STRING

java - 使用 PreparedStatement 时 executeBatch() 和 executeLargeBatch() 之间的区别

java - Gradle 尝试编译 jar 的源代码

JavaFX 使用 fxml 打开新窗口问题

java - 您如何模拟 JavaFX 工具包初始化?

java - HashMap,其中键的顺序很重要

java.io.IOException : Server returned HTTP response code: 403 for URL:unable to resolve