fxml上的JavaFX TextField宽度不同

时间:2018-02-08 09:52:08

标签: java javafx

我对一堆TextFields有点麻烦,我希望它们具有相同的宽度,但其中一个是抵制变化。以下是它现在的实际情况。

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>

任何想法我该如何解决?我想让它具有响应性,这样当屏幕尺寸发生变化时,它看起来不会很糟糕。

1 个答案:

答案 0 :(得分:1)

使用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