2017-08-16 203 views
-1

我是JavaFx中的新成員,並且我花了太多時間嘗試動態地放置單選按鈕+文本字段。輸入號碼後,我想用那種方法顯示我的單選按鈕和我的TextField(藍色和紅色的)將多個節點添加到vbox javafx

see here[1]

但我得到這個:

see here

我試過vbox,hbox,他們都是,但它不起作用!

任何人都可以找出我的代碼中的問題,請!!!感謝您的幫助

RadioButton[] btn = new RadioButton[100]; //our Collection to hold newly created Buttons 
TextField[] xlist = new TextField[100]; //our Collection to hold newly created Buttons 
TextField[] ylist = new TextField[100]; 

final ToggleGroup grpBtn = new ToggleGroup(); 
@FXML 
private Group noeuds; 
@FXML 
private VBox vb2; 
@FXML 
private HBox hb2; 



@FXML 
public void addBtn(int i, RadioButton[] btn) { 
    btn[i] = new RadioButton(); 
    btn[i].setText(String.valueOf(i + 1)); 
    btn[i].setToggleGroup(grpBtn); 
    btn[i].setSelected(true); 
    btn[i].setTranslateX(-5); 
    btn[i].setTranslateY(-340); 
    btn[i].setPadding(new Insets(0, 0, 20, 20)); 
    vb2.getChildren().add(btn[i]); 


} 

@FXML 
public void addX(int i, TextField[] xlist) { 
    xlist[i] = new TextField(); 
    xlist[i].setTranslateX(-80); 
    xlist[i].setTranslateY(40); 
    xlist[i].setStyle("-fx-background-color: red;"); 
    xlist[i].setPrefSize(30, 30); 
    xlist[i].setTooltip(new Tooltip("X coordinate of " + (i + 1))); 
    hb2.getChildren().add(xlist[i]); 

} 

@FXML 
public void addY(int i, TextField[] ylist) { 
    ylist[i] = new TextField(); 
    ylist[i].setTranslateX(-78); 
    ylist[i].setTranslateY(40); 

    ylist[i].setStyle("-fx-background-color: blue;"); 
    ylist[i].setPrefSize(30, 30); 

    ylist[i].setTooltip(new Tooltip("Y coordinate of" + (i + 1))); 
    hb2.getChildren().add(ylist[i]); 



} 

public void initialize(URL url, ResourceBundle rb) { 

    //some code 

    for (int i = 0; i < Integer.parseInt(nodeID.getText()); i++) { 
     addBtn(i, btn); 
     // System.out.println("jjjj"+btn.length); 
     addX(i, xlist); 
     // System.out.println("mmmm"+xlist.length); 
     addY(i, ylist); 


    } 
} 
+1

請幫助! '( – Mira

回答

1

這個小應用程序可能會幫助你提升。閱讀代碼並嘗試理解。我試圖在代碼中發表評論。

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.RadioButton; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class JavaFXApplication7 extends Application { 

    @Override 
    public void start(Stage primaryStage) {   
     AnchorPane root = new AnchorPane(); 

     VBox vbox1 = new VBox(); 
     vbox1.setSpacing(5);//Set vbox spacing 

     //Handles the number of row to be added to the vbox 
     for(int i = 0; i < 4; i++) 
     { 
      vbox1.getChildren().add(addNewRow(i)); 
     } 

     root.getChildren().add(vbox1); 
     Scene scene = new Scene(root, 300, 250); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

    //Method creates all the nodes for a new row. 
    HBox addNewRow(int rowNumber) 
    { 
     //Create nodes and adding correct spaceing 
     HBox hbox = new HBox(); 
     hbox.setSpacing(5); 
     RadioButton radioButton = new RadioButton(); 
     radioButton.setPrefHeight(25); 
     TextField textField = new TextField(); 
     textField.setPrefWidth(40); 
     Label label = new Label(Integer.toString(rowNumber + 1)); 
     label.setPrefHeight(25); 
     HBox trailingHBox = new HBox(); 
     trailingHBox.setSpacing(5); 
     hbox.getChildren().addAll(radioButton, textField, label, trailingHBox); 

     //Event handler on textfield. Add right about of trailing textfields 
     textField.setOnKeyReleased((event)->{    
      if(textField.getText().trim().length() > 0 && Integer.parseInt(textField.getText()) > 0)//If textfield has some value greater than zero. I didn't catch for non integers 
      { 
       int tempInt = Integer.parseInt(textField.getText()); 
       //clear trailingHBox so that new Trailing hbox can be added 
       if(trailingHBox.getChildren().size() > 0) 
       { 
        trailingHBox.getChildren().clear(); 
       } 
       //add the correct number of textFields. 
       for(int i = 0; i < tempInt - 1; i++) 
       { 
        TextField tempTextField = new TextField(); 
        tempTextField.setPrefWidth(20); 
        trailingHBox.getChildren().add(tempTextField); 
       } 

       //add the blue and red textfields 
       TextField textFieldBlue = new TextField(); 
       textFieldBlue.setPrefWidth(40); 
       textFieldBlue.setStyle("-fx-background-color: BLUE"); 
       TextField textFieldRed = new TextField(); 
       textFieldRed.setPrefWidth(40); 
       textFieldRed.setStyle("-fx-background-color: RED"); 

       trailingHBox.getChildren().addAll(textFieldBlue, textFieldRed);     
      } 
      else{ 
       trailingHBox.getChildren().clear();//clear traingHbox if it's has no value 
      } 
     });  

     return hbox; 
    } 
} 

enter image description here

enter image description here

+0

其實它不工作!我試過你的解決方案,我沒有得到你的屏幕截圖 – Mira

+0

我將重新發布代碼。也許我複製了錯誤。 – Sedrick

+0

:非常感謝你的幫助,它是現在工作,我知道了,你是一個拯救生命的人! – Mira