2016-12-03 64 views
0

在我的程序中,我有一个加号和减号按钮,用于增加或减少文本字段的数量。现在我想要做的是在填满所有字段后打开一个新面板。我遇到的问题是,如果我只填写其中一个文本字段并单击验证按钮,即使其他文本字段为空,它也会转到下一个面板。验证javaFX中的TextField数组

stage = primaryStage; 

    pane = new BorderPane(); 
    pane.setStyle("-fx-background: #CEF6D8;"); 

    centerPane1 = new Pane(); 

    GridPane mainGrid = new GridPane(); 
    mainGrid.setPadding(new Insets(150, 0, 0, 100)); 
    mainGrid.setVgap(50); 

    // ADULTS, CHILDREN AND INFANT GRID 
    noPassGrid = new GridPane(); 
    noPassGrid.setHgap(10); 

    plus = new Button("+"); 
    plus.setMinWidth(50); 
    plus.setFont(Font.font("Verdana", 20)); 
    GridPane.setConstraints(plus, 1, 0); 

    plus.setOnAction(e -> { 
     plus(count); 
     getArraySize(); 
    }); 

    count = new Label("1"); 
    count.setFont(Font.font("Verdana", 20)); 
    GridPane.setConstraints(count, 2, 0); 

    minus = new Button("-"); 
    minus.setMinWidth(50); 
    minus.setFont(Font.font("Verdana", 20)); 
    GridPane.setConstraints(minus, 3, 0); 

    minus.setOnAction(e -> { 
     minus(count); 
      getArraySize(); 
    }); 


    noPassGrid.getChildren().addAll(plus, count, minus); 

    GridPane.setConstraints(noPassGrid, 0, 2); 

    mainGrid.getChildren().addAll(noPassGrid); 


    search = new Button("SEARCH"); 
    search.setPrefSize(300, 100); 
    search.setFont(new Font("Verdana", 30)); 
    grid = new GridPane(); 
    grid.setPadding(new Insets(150, 0, 0, 450)); 
    grid.setVgap(20); 
    grid.setHgap(20); 

    test = new Label(); 
    test.setFont(Font.font("Verdana", 20)); 
    test.setLayoutX(500); 
    test.setLayoutX(100); 



    search.setOnAction(e -> { 

     for(int i=0; i<arrayLength; i++) { 
      passName[i] = new TextField(); 
      passName[i].setFont(Font.font("Verdana", 20)); 
      passName[i].setPromptText("Enter Name"); 
      GridPane.setConstraints(passName[i], 2, i); 

      grid.getChildren().addAll(passName[i]); 
     } 
    }); 

    verify = new Button("VERIFY"); 
    verify.setPrefSize(300, 100); 
    verify.setFont(new Font("Verdana", 30)); 

    verify.setOnAction(e -> { 
     for(int i=0; i<arrayLength; i++) { 
      if(passName[i].getText().equals("")) { 
       test.setText("INCORRECT"); 
      } 
      else { 
       center2 = new Pane(); 
       pane.setCenter(center2); 
      } 
     } 
    }); 


    centerPane1.getChildren().addAll(mainGrid, grid, test); 
    pane.setCenter(centerPane1); 

    bottom1 = new AnchorPane(verify, search); 
    bottom1.setPadding(new Insets(0, 0, 100, 0)); 
    AnchorPane.setRightAnchor(search, 150.0); 
    AnchorPane.setLeftAnchor(verify, 150.0); 
    pane.setBottom(bottom1); 

    scene = new Scene(pane, 1200, 600); 
    stage.setScene(scene); 
    stage.show(); 

} 


public void getArraySize() { 
    arrayLength = counter; 
    passName = new TextField[arrayLength]; 
} 

public void plus(Label l) { 
    if(counter < 8) { 
     counter++; 
     l.setText("" + counter); 
    } 
} 

public void minus(Label l) { 
    if(counter > 1) { 
     counter--; 
     l.setText("" + counter); 
    } 
} 

你能告诉我我的代码有什么问题吗?

感谢, 马尔科

+0

您能解释一下您想达到的目标吗?您是否希望验证按钮仅在所有文本字段都已填充的情况下才能使用? – aleb2000

+0

如果textFields为空,我想显示该消息。当他们都满了,我想打开一个新的面板 – MDC

回答

0

的问题是在验证按钮的逻辑,如果文本框的至少一个被填满,在另一方面你的按钮将会使面板看起来你想窗格只有在所有文本框都已填充的情况下才会出现,因此您应该这样做:

verify.setOnAction(e -> { 
    boolean valid = true; // Variable to store whether the inputs are filled 
    for(int i=0; i<arrayLength; i++) { 
     if(passName[i].getText().equals("")) { 
      valid = false; 
     } 
    } 

    if(valid) { 
     center2 = new Pane(); 
     pane.setCenter(center2); 
    } else { 
     test.setText("INCORRECT"); 
    } 
}); 
+0

排序,非常感谢! – MDC