2016-07-06 50 views
1

我試圖使用TextField.setPrefColumnCount()給我GridPane中不同大小的文本字段。 GridPane將所有的字段大小調整到最長的字段。我在這裏做錯了什麼,或者我是否需要明確地將字段的最大大小設置爲所有字段的首選大小?GridPane忽略TextField.setPrefColumnCount

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

您可以設置GridPane的fillWidth屬性像這樣子節點:

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

原來在ColumnConstraints相同的設置會做到這一點。謝謝! – Geoff