TextField和标签数组

时间:2018-10-22 18:19:18

标签: arrays javafx

在这里,我试图在数组中创建TextFieldLabel并将它们添加到GridPane中,但是在注释行及以后出现问题。

GridPane pane=new GridPane();
pane.setPadding(new Insets(50,0,0,50));
pane.setVgap(20);
pane.setHgap(40);
Label Arrival= new Label("Arrival Time");
Label Burst= new Label("Burst Time");
TextField[] ArrvialInput= new TextField[10];
TextField[] BurstInput= new TextField[10];
Label Process= new Label();
for (int i=0; i<=10-1; i++) {
    TextField textFieldA = new TextField();
    TextField textFieldB = new TextField();
    ArrvialInput[i] = textFieldA;
    BurstInput[i] = textFieldB;
    Process= new Label("P"+(i+1));
}
for (int i=0; i<=10-1; i++) {
    pane.add(Process, 0, i+1); //Run Time error here 
    pane.add(Arrival, 1, i+1);
    pane.add(ArrvialInput[i], 2, i+1);
    pane.add(Burst, 0, i);
    pane.add(BurstInput[i], 4, i+1);
}

1 个答案:

答案 0 :(得分:0)

如果不需要访问标签,则可以执行以下操作,但@fabian正确地说:“将同一节点多次添加到同一父节点会导致异常”

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        GridPane pane=new GridPane();
        pane.setPadding(new Insets(50,0,0,50));
        pane.setVgap(20);
        pane.setHgap(40);
//        Label Arrival= new Label("Arrival Time");
//        Label Burst= new Label("Burst Time");
        TextField[] ArrvialInput= new TextField[10];
        TextField[] BurstInput= new TextField[10];
//        Label Process= new Label();
        for (int i=0; i<=10-1; i++) {
            TextField textFieldA = new TextField();
            TextField textFieldB = new TextField();
            ArrvialInput[i] = textFieldA;
            BurstInput[i] = textFieldB;
//            Process= new Label("P"+(i+1));
        }
        for (int i=0; i<=10-1; i++) {
            pane.add(new Label("P"+(i+1)), 0, i+1);
            pane.add(new Label("Arrival Time"), 1, i+1);
            pane.add(ArrvialInput[i], 2, i+1);
            pane.add(new Label("Burst Time"), 3, i+1);
            pane.add(BurstInput[i], 4, i+1);
        }


        Scene scene = new Scene(pane);
        stage = new Stage();
        stage.setScene(scene);
        stage.show();

    }


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