2016-11-30 113 views
2

努力实现:的JavaFX GridPane - 配置控制增长并占据所有可用空间

  1. Text Field 1Text Field 3应占据所有可用空间
  2. Button 1Button 2应该有相同的宽度

Issue:

Text Field 1 and Text Field 3不占用全部可用空间(尽管其增长一度Label完全显示)

下面是截图:

GridPane - Controls are not occupying all the available space

// Removed imports for brevity 

public class GridPaneTest extends Application 
{ 
    @Override 
    public void start(Stage primaryStage) 
    { 
    primaryStage.setTitle("GridPane Test"); 

    GridPane gridPane = new GridPane(); 
    // gridPane.setGridLinesVisible(true); 
    gridPane.setHgap(5); 
    gridPane.setVgap(5); 
    gridPane.setPadding(new Insets(5)); 

    TextField tf1 = new TextField("Text Field 1"); 
    GridPane.setHgrow(tf1, Priority.ALWAYS); 
    gridPane.add(tf1, 0, 0); 
    TextField tf2 = new TextField("Text Field 2"); 
    GridPane.setHgrow(tf2, Priority.NEVER); 
    gridPane.add(tf2, 1, 0); 
    Button button1 = new Button("Button 1"); 
    button1.setMaxWidth(140); 
    GridPane.setHgrow(button1, Priority.NEVER); 
    GridPane.setHalignment(button1, HPos.RIGHT); 
    gridPane.add(button1, 2, 0); 

    TextField tf3 = new TextField("Text Field 3"); 
    GridPane.setHgrow(tf3, Priority.ALWAYS); 
    gridPane.add(tf3, 0, 1, 2, 1); 
    Button button2 = new Button("Button 2 Button 2"); 
    button2.setMaxWidth(140); 
    GridPane.setHgrow(button2, Priority.NEVER); 
    GridPane.setHalignment(button2, HPos.RIGHT); 
    gridPane.add(button2, 2, 1); 

    Label label1 = new Label(
     "Label 1 Label 1 Label 1 Label 1 Label 1 Label 1 Label 1 Label 1 Label 1 Label 1 Label 1"); 
    GridPane.setHgrow(label1, Priority.ALWAYS); 
    gridPane.add(label1, 0, 2, 3, 1); 

    primaryStage.setScene(new Scene(gridPane)); 

    primaryStage.show(); 
    } 

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

回答

0

根据GridPane documentation

如果应用程序需要显式控制行或列的大小,则可以通过添加RowConstraints和ColumnConstraints obj学分指定这些度量

gridPane.getColumnConstraints() 
    .addAll(new ColumnConstraints(), new ColumnConstraints(), 
    new ColumnConstraints(140) /* Control the third column size */); 

屏幕截图:

GridPane - Demo configuring controls to grow and occupy all the available free space

+0

查阅按钮宽度可被设置为'Button.setMaxWidth(Double.MAX_VALUE)' - 为对两个按钮是相同的尺寸。 –

相关问题