如何打破按钮上的 do-while 循环
How to break do-while loop on button
目前这个案例我有两个问题:
我正在尝试找到一种方法来打破单击取消按钮后的无限循环。我试图在单击按钮时将变量标志设置为 false,并将标志变量设置为循环中的条件,但我无法实现。
第二个问题是。如何从此对话框初始化两个单独的数组列表 window。例如,它返回一对字符串。我希望在循环中将值添加到 ArrayLists。
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.util.Pair;
import javafx.scene.control.Label;
import java.util.*;
public class Controller {
public void pick (ActionEvent event) {
boolean flag = true;
do {
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login Dialog");
dialog.setHeaderText("Look, a Custom Login Dialog");
//Set the button types.
ButtonType dalejButtonType = new ButtonType("Dalej", ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(dalejButtonType, cancelButton);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField imie = new TextField();
imie.setPromptText("Imię");
TextField email = new TextField();
email.setPromptText("eMail");
grid.add(new Label("Imię:"), 0, 0);
grid.add(imie, 1, 0);
grid.add(new Label("eMail:"), 0, 1);
grid.add(email, 1, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(dalejButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
imie.textProperty().addListener((observable, oldValue, newValue) -> {
loginButton.setDisable(newValue.trim().isEmpty());
});
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> imie.requestFocus());
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == dalejButtonType) {
return new Pair<>(imie.getText(), email.getText());
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
} while(flag);
做不行吗
if (result.isPresent()) {
Pair<String, String> values = result.get();
// add values to list
} else {
flag = false ;
}
?
欢迎使用 Whosebug!
我认为将所有方法的周围都变成do/while
不是一个好主意。在这种情况下,您每次都创建包含所有元素的对话框。不利于性能。如果只循环显示对话框会更好
关于列表的第二个问题:您可以创建此类列表并填充到转换器中。在这种情况下,do/while
可以简化为 while
,只检查结果呈现:
final List<String> names = new ArrayList<>();
final List<String> emails = new ArrayList<>();
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == dalejButtonType) {
names.add(imie.getText());
emails.add(email.getText());
return new Pair<>(imie.getText(), email.getText());
}
return null;
});
while(dialog.showAndWait().isPresent()) {
// do additional something if needed
}
另一种解决方案,其中列表填充在 do/while
但没有 flag
:
Optional<Pair<String, String>> result;
do{
result = dialog.showAndWait();
if (result.isPresent()) {
names.add(result.get().getKey());
emails.add(result.get().getValue());
}
} while(result.isPresent());
目前这个案例我有两个问题:
我正在尝试找到一种方法来打破单击取消按钮后的无限循环。我试图在单击按钮时将变量标志设置为 false,并将标志变量设置为循环中的条件,但我无法实现。
第二个问题是。如何从此对话框初始化两个单独的数组列表 window。例如,它返回一对字符串。我希望在循环中将值添加到 ArrayLists。
import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.scene.Node; import javafx.scene.control.*; import javafx.scene.layout.GridPane; import javafx.util.Pair; import javafx.scene.control.Label; import java.util.*; public class Controller { public void pick (ActionEvent event) { boolean flag = true; do { // Create the custom dialog. Dialog<Pair<String, String>> dialog = new Dialog<>(); dialog.setTitle("Login Dialog"); dialog.setHeaderText("Look, a Custom Login Dialog"); //Set the button types. ButtonType dalejButtonType = new ButtonType("Dalej", ButtonBar.ButtonData.OK_DONE); ButtonType cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE); dialog.getDialogPane().getButtonTypes().addAll(dalejButtonType, cancelButton); // Create the username and password labels and fields. GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(20, 150, 10, 10)); TextField imie = new TextField(); imie.setPromptText("Imię"); TextField email = new TextField(); email.setPromptText("eMail"); grid.add(new Label("Imię:"), 0, 0); grid.add(imie, 1, 0); grid.add(new Label("eMail:"), 0, 1); grid.add(email, 1, 1); // Enable/Disable login button depending on whether a username was entered. Node loginButton = dialog.getDialogPane().lookupButton(dalejButtonType); loginButton.setDisable(true); // Do some validation (using the Java 8 lambda syntax). imie.textProperty().addListener((observable, oldValue, newValue) -> { loginButton.setDisable(newValue.trim().isEmpty()); }); dialog.getDialogPane().setContent(grid); // Request focus on the username field by default. Platform.runLater(() -> imie.requestFocus()); // Convert the result to a username-password-pair when the login button is clicked. dialog.setResultConverter(dialogButton -> { if (dialogButton == dalejButtonType) { return new Pair<>(imie.getText(), email.getText()); } return null; }); Optional<Pair<String, String>> result = dialog.showAndWait(); } while(flag);
做不行吗
if (result.isPresent()) {
Pair<String, String> values = result.get();
// add values to list
} else {
flag = false ;
}
?
欢迎使用 Whosebug!
我认为将所有方法的周围都变成do/while
不是一个好主意。在这种情况下,您每次都创建包含所有元素的对话框。不利于性能。如果只循环显示对话框会更好
关于列表的第二个问题:您可以创建此类列表并填充到转换器中。在这种情况下,do/while
可以简化为 while
,只检查结果呈现:
final List<String> names = new ArrayList<>();
final List<String> emails = new ArrayList<>();
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == dalejButtonType) {
names.add(imie.getText());
emails.add(email.getText());
return new Pair<>(imie.getText(), email.getText());
}
return null;
});
while(dialog.showAndWait().isPresent()) {
// do additional something if needed
}
另一种解决方案,其中列表填充在 do/while
但没有 flag
:
Optional<Pair<String, String>> result;
do{
result = dialog.showAndWait();
if (result.isPresent()) {
names.add(result.get().getKey());
emails.add(result.get().getValue());
}
} while(result.isPresent());