在我的程序中,我有一个加号和减号按钮,可以增加或减少文本字段的数量。现在我要做的是在填写所有字段后打开一个新面板。我遇到的问题是,如果我只填写其中一个文本字段并单击验证按钮,即使其他文本字段为空,也会转到下一个面板。
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 :(得分: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");
}
});