2010-02-20 8 views
2

Я просматривал образец gwt-кода, поставляемый с gwt, и при взломе с ним - у меня было два вопроса re. вещи, которые нам особенно не понятны (код, размещенный ниже - немного длинный: извините)GWT/Java - несколько базовых вопросов пользовательского интерфейса по образцу кода

1) Диалоговое окно всплывающего окна, кажется, не установлено как «скрытое» сначала, и хотя оно было собрано, оно фактически не отображается до тех пор, пока не будет нажата кнопка - почему это? (i dont, например, см. любой диалог 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

DialogBox не отображается по умолчанию - вы должны явно показать его через show() или center().

О второй части - это исходный код, который вы предоставили, вызывающий проблему? Кажется, это не быстрый взгляд. Проблема, о которой вы описали, похоже, может быть вызвана попыткой добавить контент в dialogVPanel (который содержит содержимое DialogBox) без предварительного ввода clear().

+0

спасибо! именно то, что я искал! – malangi

 Смежные вопросы

  • Нет связанных вопросов^_^