GridPane忽略TextField.setPrefColumnCount

时间:2016-07-06 09:53:11

标签: javafx-8

我正在尝试使用TextField.setPrefColumnCount()在GridPane中为我提供不同大小的文本字段。 GridPane将所有字段的大小调整为最长字段。我在这里做错了什么,或者我是否需要明确地将字段的最大大小设置为所有字段的首选大小?

public class TestProject extends Application {

    @Override
    public void start(Stage primaryStage) {

        BorderPane base = new BorderPane();
        GridPane inner = new GridPane();

        inner.setHgap(4);
        inner.setVgap(4);
        inner.setPadding(new Insets(8));
        inner.setMaxWidth(Double.MAX_VALUE);
        inner.setGridLinesVisible(true);
        inner.setStyle("-fx-border-color:red");

        Label lA = new Label("Label A");
        Label lB = new Label("Label B");
        Label lC = new Label("Label C");

        TextField tA = new TextField();
        tA.setPrefColumnCount(30);

        TextField tB = new TextField();
        tB.setPrefColumnCount(15);

        TextField tC = new TextField();
        tC.setPrefColumnCount(6);

        inner.addRow(0, lA, tA);
        inner.addRow(1, lB, tB);
        inner.addRow(2, lC, tC);

        base.setCenter(inner);
        base.setPrefWidth(800);
        base.setPrefHeight(600);

        Scene scene = new Scene(base);

        primaryStage.setTitle("GridPane TextField Test");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

1 个答案:

答案 0 :(得分:1)

您可以为子节点设置GridPane的fillWidth属性,如下所示:

GridPane.setFillWidth(tA, false);
GridPane.setFillWidth(tB, false);
GridPane.setFillWidth(tC, false);