如何防止Labeled控件在文本更改时调整其他组件的大小?

时间:2014-12-30 21:15:08

标签: java javafx label fxml gridpane

正如标题所暗示的,当我更改javafx.scene.control.Label的文本时,它会调整包含在同一布局中的所有组件的大小。我究竟如何防止这种情况?这是我的FXML标记:

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

<?import org.think.software.test.javafx.view.login.LoginView?>

<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.PasswordField?>

<?import javafx.geometry.Insets?>

<LoginView fx:controller="org.think.software.test.javafx.view.login.LoginView"
    xmlns:fx="http://think.org/software/test/javafx/login" alignment="CENTER"
    hgap="10" vgap="10">
    <padding>
        <Insets top="35" right="35" bottom="35" left="35" />
    </padding>
    <Label fx:id="infoLabel" GridPane.columnIndex="0"
        GridPane.rowIndex="0" GridPane.columnSpan="3" text="%infoLabelText" />
    <Label fx:id="usernameLabel" GridPane.columnIndex="0"
        GridPane.rowIndex="1" text="%usernameLabelText" />
    <TextField fx:id="usernameField" GridPane.columnIndex="1"
        GridPane.rowIndex="1" GridPane.columnSpan="2" promptText="%usernameFieldPromptText" />
    <Label GridPane.columnIndex="0" GridPane.rowIndex="2"
        text="%passwordLabelText" />
    <PasswordField fx:id="passwordField"
        GridPane.columnIndex="1" GridPane.rowIndex="2" GridPane.columnSpan="2"
        promptText="%passwordFieldPromptText" />
    <Button fx:id="loginButton" GridPane.columnIndex="0"
        GridPane.rowIndex="3" GridPane.columnSpan="3"
        onAction="java.lang.System.out.println(infoLabel.setText('Logging in...'));"
        text="%loginButtonText" />
</LoginView>

before text change

after text change

1 个答案:

答案 0 :(得分:0)

使用一些列约束来控制不同列的扩展。

我不清楚为什么你的文本字段跨越两列,因为任何行中的组件不超过两个。我会尝试类似的东西:

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

<?import org.think.software.test.javafx.view.login.LoginView?>

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.ColumnConstraints?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.PasswordField?>

<?import javafx.geometry.Insets?>

<LoginView fx:controller="org.think.software.test.javafx.view.login.LoginView"
    xmlns:fx="http://think.org/software/test/javafx/login" alignment="CENTER"
    hgap="10" vgap="10">
    <padding>
        <Insets top="35" right="35" bottom="35" left="35" />
    </padding>
    <Label fx:id="infoLabel" GridPane.columnIndex="0"
        GridPane.rowIndex="0" GridPane.columnSpan="2" text="%infoLabelText" />
    <Label fx:id="usernameLabel" GridPane.columnIndex="0"
        GridPane.rowIndex="1" text="%usernameLabelText" />
    <TextField fx:id="usernameField" GridPane.columnIndex="1"
        GridPane.rowIndex="1" promptText="%usernameFieldPromptText" />
    <Label GridPane.columnIndex="0" GridPane.rowIndex="2"
        text="%passwordLabelText" />
    <PasswordField fx:id="passwordField"
        GridPane.columnIndex="1" GridPane.rowIndex="2" 
        promptText="%passwordFieldPromptText" />
    <Button fx:id="loginButton" GridPane.columnIndex="0"
        GridPane.rowIndex="3" GridPane.columnSpan="2"
        onAction="java.lang.System.out.println(infoLabel.setText('Logging in...'));"
        text="%loginButtonText" />

    <columnConstraints>
        <!-- left column -->
        <ColumnConstraints hgrow="NEVER" halignment="RIGHT" />
        <!-- right column -->
        <ColumnConstraints hgrow="SOMETIMES" halignment="LEFT" minWidth="150" maxWidth="600" />
    </columnConstraints>
</LoginView>

minWidthmaxWidth的值只是示例;你可以试验那些(或者可能完全省略它们)。您也可以尝试使用hgrow="ALWAYS"作为正确的列,具体取决于您想要的确切行为。