📜  GWT FormPanel

📅  最后修改于: 2021-01-02 12:36:04             🧑  作者: Mango

GWT FormPanel

本小组讨论HTML FORM的元素。在此面板中,我们可以添加将包装在HTML表单元素内的所有小部件。

GWT FormPanel类声明

让我们看看com.google.gwt.user.client.ui.FormPanel的声明

public class FormPanel extends SimplePanel

GWT FormPanel嵌套类

Class Description
FormPanel.SubmitCompleteHandler It handles for FormPanel.SubmitCompleteEvent events.
FormPanel.SubmitCompleteEvent It is fired when a form has been submitted successfully.
FormPanel.SubmitEvent It is fired when the form is submitte
FormPanel.SubmitHandler It handles for FormPanel.SubmitEvent events.

GWT FormPanel构造函数

Constructor Description
FormPanel() It creates a new FormPanel.
FormPanel(Element element) It is used by subclasses to explicitly use an existing element.
FormPanel(Element element, boolean createIFrame) It is used by subclasses to explicitly use an existing element.
FormPanel(NamedFrame frameTarget) It creates a FormPanel that targets a NamedFrame.
FormPanel(NamedFrame frameTarget) It creates a FormPanel that targets a NamedFrame.
FormPanel(java.lang.String target) It creates a new FormPanel that targets string as input.

GWT FormPanel方法

Modifier and Types Method Description
void addFormHandler(FormHandler handler) It adds a form widget to the panel.
java.lang.String getAction() It gets the ‘action’ associated with this form.
java.lang.String getEncoding() It gets the encoding used for submitting this form.
java.lang.String getMethod() It gets the HTTP method used for submitting this form.
java.lang.String getTarget() It gets the form’s ‘target’.
protected void onAttach() This method is called when a widget is attached to the browser’s document.
protected void onDetach() This method is called when a widget is detached from the browser’s document.
void onFrameLoad() It is called when the target frame is done loading.
void setAction(SafeUri url) It sets the ‘action’ associated with this form.
void setAction(java.lang.String url) It sets the ‘action’ associated with this form.
void setEncoding(java.lang.String encodingType) It sets the encoding used for submitting this form.
void submit() It submits the form.
static FormPanel wrap(Element element) It creates a FormPanel that wraps an existing
element.
static FormPanel wrap(Element element, boolean createIFrame) It creates a FormPanel that wraps an existing element.

GWT FormPanel示例

//SampleFormPanel.java:


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FormPanelExample implements EntryPoint {

  public void onModuleLoad() {
    // Create a FormPanel and point it at a service.
    final FormPanel form = new FormPanel();
    form.setAction("/myFormHandler");

    
    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);

    // Create a panel to hold all of the form widgets.
    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);

    // Create a TextBox, giving it a name so that it will be submitted.
    final TextBox tb = new TextBox();
    tb.setName("textBoxFormElement");
    panel.add(tb);

    // Create a ListBox, giving it a name and some values to be associated with its options.
    ListBox lb = new ListBox();
    lb.setName("listBoxFormElement");
    lb.addItem("Item1", "Item1Value");
    lb.addItem("Item2", "Item2Value");
    lb.addItem("Item3", "Item3Value");
    panel.add(lb);

    // Create a FileUpload widget.
    FileUpload upload = new FileUpload();
    upload.setName("uploadFormElement");
    panel.add(upload);

    // Add a 'submit' button.
    panel.add(new Button("Submit", new ClickHandler() {
      public void onClick(ClickEvent event) {
        form.submit();
      }
    }));
  // Add an event handler to the form.
    form.addSubmitHandler(new FormPanel.SubmitHandler() {
      public void onSubmit(SubmitEvent event) {
 
        if (tb.getText().length() == 0) {
          Window.alert("The text box must not be empty");
          event.cancel();
        }
      }
    });
    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
      public void onSubmitComplete(SubmitCompleteEvent event) {
        
        Window.alert(event.getResults());
      }
    });

    RootPanel.get().add(form);
  }
}

//SampleFormPanel.css:

body {
   text-align: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;
}

输出: