2010-02-20 71 views
2

我正在瀏覽gwt提供的示例gwt代碼,並在剽竊它時 - 我有兩個問題。我們特別清楚的東西(代碼如下 - 略長:對不起)GWT/Java - 關於示例代碼的一些基本的UI問題

1)彈出式對話框似乎並未被設置爲「隱藏」,即使它已經組裝起來,它直到按鈕被點擊後才真正顯示出來 - 這是爲什麼? (我沒有,例如看到任何dialogBox.Hide()調用

2)在創建一個類似的對話框,基本上通過複製代碼,我面臨的問題之間調用對話框 - 內容持續,所以如果我用對話框上的按鈕隱藏盒子,下一次它被調用 - 新的內容只是附加在它上面...我做錯了什麼?

非常感謝!

public void onModuleLoad() { 
     final Button sendButton = new Button("Send"); 
     final TextBox nameField = new TextBox(); 
     nameField.setText("GWT User"); 
     final Label errorLabel = new Label(); 

     // We can add style names to widgets 
     sendButton.addStyleName("sendButton"); 

     // Add the nameField and sendButton to the RootPanel 
     // Use RootPanel.get() to get the entire body element 
     RootPanel.get("nameFieldContainer").add(nameField); 
     RootPanel.get("sendButtonContainer").add(sendButton); 
     RootPanel.get("errorLabelContainer").add(errorLabel); 

     // Focus the cursor on the name field when the app loads 
     nameField.setFocus(true); 
     nameField.selectAll(); 

     // Create the popup dialog box 
     final DialogBox dialogBox = new DialogBox(); 
     dialogBox.setText("Remote Procedure Call"); 
     dialogBox.setAnimationEnabled(true); 
     final Button closeButton = new Button("Close"); 
     // We can set the id of a widget by accessing its Element 
     closeButton.getElement().setId("closeButton"); 
     final Label textToServerLabel = new Label(); 
     final HTML serverResponseLabel = new HTML(); 
     VerticalPanel dialogVPanel = new VerticalPanel(); 
     dialogVPanel.addStyleName("dialogVPanel"); 
     dialogVPanel.add(new HTML("<b>Sending name to the server:</b>")); 
     dialogVPanel.add(textToServerLabel); 
     dialogVPanel.add(new HTML("<br><b>Server replies:</b>")); 
     dialogVPanel.add(serverResponseLabel); 
     dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); 
     dialogVPanel.add(closeButton); 
     dialogBox.setWidget(dialogVPanel); 

     // Add a handler to close the DialogBox 
     closeButton.addClickHandler(new ClickHandler() { 
      public void onClick(ClickEvent event) { 
       dialogBox.hide(); 
       sendButton.setEnabled(true); 
       sendButton.setFocus(true); 
      } 
     }); 

     // Create a handler for the sendButton and nameField 
     class MyHandler implements ClickHandler, KeyUpHandler { 
      /** 
      * Fired when the user clicks on the sendButton. 
      */ 
      public void onClick(ClickEvent event) { 
       sendNameToServer(); 
      } 

      /** 
      * Fired when the user types in the nameField. 
      */ 
      public void onKeyUp(KeyUpEvent event) { 
       if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { 
        sendNameToServer(); 
       } 
      } 

      /** 
      * Send the name from the nameField to the server and wait for a response. 
      */ 
      private void sendNameToServer() { 
       // First, we validate the input. 
       errorLabel.setText(""); 
       String textToServer = nameField.getText(); 
       if (!FieldVerifier.isValidName(textToServer)) { 
        errorLabel.setText("Please enter at least four characters"); 
        return; 
       } 

       // Then, we send the input to the server. 
       sendButton.setEnabled(false); 
       textToServerLabel.setText(textToServer); 
       serverResponseLabel.setText(""); 
       greetingService.greetServer(textToServer, 
         new AsyncCallback<String>() { 
          public void onFailure(Throwable caught) { 
           // Show the RPC error message to the user 
           dialogBox 
             .setText("Remote Procedure Call - Failure"); 
           serverResponseLabel 
             .addStyleName("serverResponseLabelError"); 
           serverResponseLabel.setHTML(SERVER_ERROR); 
           dialogBox.center(); 
           closeButton.setFocus(true); 
          } 

          public void onSuccess(String result) { 
           dialogBox.setText("Remote Procedure Call"); 
           serverResponseLabel 
             .removeStyleName("serverResponseLabelError"); 
           serverResponseLabel.setHTML(result); 
           dialogBox.center(); 
           closeButton.setFocus(true); 
          } 
         }); 
      } 
     } 

     // Add a handler to send the name to the server 
     MyHandler handler = new MyHandler(); 
     sendButton.addClickHandler(handler); 
     nameField.addKeyUpHandler(handler); 
    } 

回答

0

的對話框默認情況下不顯示 - 你必須通過show()center()明確地表現出來。

關於第二部分 - 您提供的源代碼是導致問題的原因嗎?這看起來不是很快。您描述的問題看起來好像可能是由於嘗試將內容添加到dialogVPanel(其中包含DialogBox的內容)而沒有第一個clear()

+0

謝謝!正是我期待的! – malangi 2010-02-20 12:37:57