2013-05-01 44 views
5

JavaFX 2でGirdPanesを使用すると、奇妙なレイアウト動作に直面しています。通常、複数のフィールド間で水平方向のスペースを均等に広げているように見えますが、それ以外の場合はそうしません。JavaFXのGridPaneにスペースを均等に分散させることはできますか?

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Control; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 

public class TestDialog extends Stage { 
    public TestDialog() { 
     super(); 
     setTitle("Test"); 

     GridPane grid = new GridPane(); 
     grid.setHgap(5); 
     grid.setVgap(5); 
     grid.setPadding(new Insets(10, 10, 10, 10)); 

     final TextField tField1 = new TextField(); 
     final TextField tField2 = new TextField(); 
     tField2.setMaxWidth(200); 

     grid.add(createLabel("Field 1:"), 0, 0); 
     grid.add(tField1, 1, 0); 
     grid.add(createLabel("Field 2:"), 2, 0); 
     grid.add(tField2, 3, 0); 

     Pane pane = new Pane(); 
//  pane.setMinSize(600, 100); // first row has weird layout behaviour 
     pane.setMinSize(100, 100); // first row is fine 
     pane.setStyle("-fx-background-color: black"); 
     grid.add(pane, 0, 1, 4, 1); 

     grid.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE); 

     setScene(new Scene(grid)); 
    } 

    private Label createLabel(String text) { 
     Label label = new Label(text); 
     label.setMinSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE); 
     return label; 
    } 

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

    public static class TestApplication extends Application { 
     @Override 
     public void start(Stage stage) throws Exception { 
      TestDialog dialog = new TestDialog(); 
      dialog.showAndWait(); 
     } 

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

この例では、そのまま実行するとダイアログのサイズがうまく調整されます。余分なペインの最小サイズがコメント内のバージョンに変更された場合、事態はうまくいかなくなります。最初の行の余分なスペースは(最初のフィールドを増やす代わりに)空のスペースに変わります。それはすでに小さな最初のフィールドから離れて、空の領域をそのまま残す。

GridPaneに実際に最初のフィールドに使用できる水平スペースを使用させる方法はありますか?ダイアログのサイズを最小値よりも小さくすることは必ずしも良い結果をもたらすとは限りませんが、最初にフィールドを拡張しないことは間違っているようです。

JDK 1.7.0_21以降のJavaFXを使用しています。

答えて

1

ColumnConstraintsクラスとRowConstraintsクラスを調べる必要があります。これらは、GridPaneにスペースの配布方法を伝えます。残念ながら、APIはここでは少し厄介です。不明な量の空き領域を均等に分散したい場合は、まず制約を設定してから#setPercentageWidthを使用する必要があります。このオプションはコンストラクタでは使用できません。すべてのすべてで

、3つの均等に分散列を持つGridPane(最小サイズを尊重しつつ、可能な場合)、次のようになります。あなたはそれに応じRowConstraintsを扱うことができる

public class Test extends Application { 
    public void start(final Stage stage) throws Exception { 
     final GridPane grid = new GridPane(); 
     grid.getColumnConstraints().setAll(
       ColumnConstraintsBuilder.create().percentWidth(100/3.0).build(), 
       ColumnConstraintsBuilder.create().percentWidth(100/3.0).build(), 
       ColumnConstraintsBuilder.create().percentWidth(100/3.0).build() 
     ); 
     grid.add(new Button("One"),0,0); 
     grid.add(new Button("Two"),1,0); 
     grid.add(new Button("and three"),2,0); 

     stage.setScene(new Scene(grid)); 
     stage.show(); 
    } 

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

。また、セルの整列を設定して、サイズ変更時にダイアログが良好に見えるようにすることもできます。

+0

私は制約を認識していますが、今までの私の実験では、興味深い結果が得られましたが、それほど有用ではありませんでした。幅の計算は、最小サイズが存在する状況で完全に壊れているように見えます。すべての列が25%の幅に設定されていると、ウィンドウが大きくなります。私はMiG Layoutに移行しました。 –