为什么我的textfield不会调整大小?

时间:2016-02-23 16:07:33

标签: javafx

我无法真正理解这种拉伸在JavaFX中是如何工作的。对于HBox和TextField,我有prefWidth到Infinity,因此如果我们调整框架大小,TextField应该更大。请帮帮我,我在这里错过了什么。谢谢。

<GridPane fx:id="first" hgap="5" vgap="5"  xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" >

<columnConstraints>
    <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" />
</columnConstraints>

<VBox.margin>
    <Insets left="10.0" top="15"/>
</VBox.margin>
<HBox GridPane.columnIndex="0" GridPane.rowIndex="0" prefWidth="Infinity"  maxWidth="Infinity">

    <fx:define>
        <ToggleGroup fx:id="myToggleGroup"/>
    </fx:define>
    <children>
        <RadioButton text="System" toggleGroup="$myToggleGroup">
        </RadioButton>
        <RadioButton text="Document" toggleGroup="$myToggleGroup">
            <HBox.margin>
                <Insets left="200.0"/>
            </HBox.margin>
        </RadioButton>
    </children>
</HBox>
<HBox GridPane.rowIndex="1" GridPane.columnIndex="0"  >
    <Label text="Name:" minWidth="50">
    </Label>

    <TextField maxWidth="Infinity" minWidth="450" prefWidth="Infinity">
        <HBox.margin>
            <Insets left="20.0"/>
        </HBox.margin>
    </TextField>
</HBox>

1 个答案:

答案 0 :(得分:0)

默认情况下,HBox会为控件指定首选大小。我不确定他们将如何处理"Infinity"的首选大小(我从布局计算中得到一些错误):我会删除所有prefWidth="Infinity"并告诉HBox允许文本字段增长:

<TextField HBox.hgrow="ALWAYS" maxWidth="Infinity">

这是一个完整的例子(稍加修改以适合SSCCE):

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

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.geometry.Insets?>

<GridPane fx:id="first" hgap="5" vgap="5"
    xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">

    <columnConstraints>
        <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" />
    </columnConstraints>

    <HBox GridPane.columnIndex="0" GridPane.rowIndex="0">

        <fx:define>
            <ToggleGroup fx:id="myToggleGroup" />
        </fx:define>
        <children>
            <RadioButton text="System" toggleGroup="$myToggleGroup">
            </RadioButton>
            <RadioButton text="Document" toggleGroup="$myToggleGroup">
                <HBox.margin>
                    <Insets left="200.0" />
                </HBox.margin>
            </RadioButton>
        </children>
    </HBox>
    <HBox GridPane.rowIndex="1" GridPane.columnIndex="0">
        <Label text="Name:" minWidth="50">
        </Label>

        <TextField HBox.hgrow="ALWAYS" maxWidth="Infinity">
            <HBox.margin>
                <Insets left="20.0" />
            </HBox.margin>
        </TextField>
    </HBox>
</GridPane>

以及运行它的代码:

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("layout.fxml"))));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}