2017-08-07 61 views
0

循環目前,我有兩個問題,這種情況下:如何打破這樣做,而按鈕

  1. 進出口試圖找到一種方法,打破了無限循環點擊cancelButton後。我試圖設置變量標誌爲false,當點擊按鈕,並設置標誌變量作爲條件在循環中,但我不能實現這一點。

  2. 第二個問題是。如何從這個對話窗口初始化兩個單獨的數組列表。例如它返回一對字符串。我想要將這些值添加到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); 
    

回答

0

歡迎來到Stackoverflow!

我覺得周圍所有的方法都變成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()); 
+0

謝謝,這解決了我的問題! –

0

不它的工作做

if (result.isPresent()) { 
    Pair<String, String> values = result.get(); 
    // add values to list 
} else { 
    flag = false ; 
}