2016-04-06 6 views
1

1つの問題について助けを求めたがっています。私は1つの列に2つのラジオボタンを移動して、それらの上にあるテキストボックスと同じだけ多くのスペースを取るようにします。これどうやってするの?現在の不適切なバージョンのコードとスクリーンショットは次のとおりです:JavaFXを重複させずに2つのラジオボタンを1つの列に配置

あなたが100%確信していない場合は、私の質問に重複しないようにしてください。あなたの質問旗を他の質問の重複として持つことはうれしくなく、この他のものに行き、共通のことは両方ともプログラミングに関することであることが分かります。

enter image description here

GridPane formGrid = new GridPane(); 
    formGrid.setPadding(new Insets(5,5,5,5)); 
    formGrid.setVgap(6); 
    formGrid.setHgap(4); 

    //first row 
    Label nameLabel = new Label("Name"); 
    GridPane.setConstraints(nameLabel, 0, 0); 

    TextField nameInput = new TextField(); 
    GridPane.setConstraints(nameInput, 1, 0); 

    Label ageLabel = new Label("Age"); 
    GridPane.setConstraints(ageLabel, 2, 0); 

    TextField ageInput = new TextField(); 
    GridPane.setConstraints(ageInput, 3, 0); 

    //secondRow 
    Label colourLabel = new Label("Colour"); 
    GridPane.setConstraints(colourLabel, 0, 1); 

    TextField colourInput = new TextField(); 
    GridPane.setConstraints(colourInput, 1, 1); 

    Label genderLabel = new Label("Gender"); 
    GridPane.setConstraints(genderLabel, 2, 1); 

    ToggleGroup pickGender = new ToggleGroup(); 

    RadioButton pickMale = new RadioButton("Male"); 
    pickMale.setToggleGroup(pickGender); 
    pickMale.setSelected(true); 
    GridPane.setConstraints(pickMale, 3, 1, 1, 1); 

    RadioButton pickFemale = new RadioButton("Female"); 
    pickFemale.setToggleGroup(pickGender); 
    GridPane.setConstraints(pickFemale, 4, 1, 1, 1); 


    //third row 
    Label typeLabel = new Label("Type"); 
    GridPane.setConstraints(typeLabel, 0, 2); 
    //combobox 
    ComboBox<String> typeBox = new ComboBox<>(); 
    GridPane.setConstraints(typeBox, 1, 2); 

    Label breedLabel = new Label("Breed"); 
    GridPane.setConstraints(breedLabel, 2, 2); 

    TextField breedInput = new TextField(); 
    GridPane.setConstraints(breedInput, 3, 2); 

    //fourth row 
    Label categoryLabel = new Label("Category: "); 
    GridPane.setConstraints(categoryLabel, 0, 3); 

    ToggleGroup pickCategory = new ToggleGroup(); 

    RadioButton pickLost = new RadioButton("Lost"); 
    pickLost.setToggleGroup(pickCategory); 
    pickLost.setSelected(true); 
    GridPane.setConstraints(pickLost, 1, 3); 

    RadioButton pickFound = new RadioButton("Found"); 
    pickLost.setToggleGroup(pickCategory); 
    GridPane.setConstraints(pickFound, 2, 3); 

    RadioButton pickAdoption = new RadioButton("Adoption"); 
    pickLost.setToggleGroup(pickCategory); 
    GridPane.setConstraints(pickAdoption, 3, 3); 

    //fifth row 
    Label descriptionLabel = new Label("Description"); 
    GridPane.setConstraints(descriptionLabel, 0, 4); 

    TextArea descriptionInput = new TextArea(); 
    GridPane.setConstraints(descriptionInput, 1, 4, 2, 1); 



    formGrid.getChildren().addAll(nameLabel, nameInput, ageLabel, ageInput); 
    formGrid.getChildren().addAll(colourLabel, colourInput, genderLabel, pickMale, pickFemale); 
    formGrid.getChildren().addAll(typeLabel, typeBox, breedLabel, breedInput, categoryLabel); 
    formGrid.getChildren().addAll(pickLost, pickFound, pickAdoption, descriptionLabel, descriptionInput); 

答えて

2

他の方法で回避であると考えてください。 「女性」ラジオボタンは列4にあり、列3の右にあります。列3は、2つのテキストフィールドを保持するのに十分な幅があります。これは、そのラジオボタンを今までに強制しています右の方へ。あなたがテキストフィールドが2つのラジオボタンと同じ水平スペースをスパンしたい場合は、それらをラジオボタンが占める列の両方を使用してみましょう。つまり、テキストフィールドは今、列3と4

TextField ageInput = new TextField(); 
GridPane.setConstraints(ageInput, 3, 0, 2, 1); 

TextField breedInput = new TextField(); 
GridPane.setConstraints(breedInput, 3, 2, 2, 1); 

列3にまたがってみましょう4はこれらの2つのテキストフィールドを含み、3列目は「男性」ラジオボタンを含み、4列目は「女性」ラジオボタンを含む。

私はあなたが投稿すべてが全体のフォームであればわからないんだけど、あなたもそれをうまく動作させるためにテキスト領域の列スパンを増やすことができます:私が得るこれらの変更により

GridPane.setConstraints(descriptionInput, 1, 4, 4, 1); 

enter image description here

(それはあなたにとって重要な場合は、2に「養子縁組」のチェックボックスの列のスパンを設定することにより、右にプッシュされてから、「女性」のチェックボックスを回避することができます)

別のオプションは、HBoxに2つのラジオボタンをラップし、代わりにラジオボタンのグリッドペインにHBoxを配置する(元のコードで始まる)です:

RadioButton pickMale = new RadioButton("Male"); 
pickMale.setToggleGroup(pickGender); 
pickMale.setSelected(true); 

RadioButton pickFemale = new RadioButton("Female"); 
pickFemale.setToggleGroup(pickGender); 

HBox genderButtons = new HBox(4, pickMale, pickFemale); 
GridPane.setConstraints(genderButtons, 3, 1); 

// ... 

formGrid.getChildren().addAll(colourLabel, colourInput, genderLabel, genderButtons); 

はしかし、私は以前のを好みますアプローチ。 (あなたの走行距離は異なる場合があります)