个性化阅读
专注于IT技术分析

GWT FormPanel用法

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

GWT FormPanel类声明

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

public class FormPanel extends SimplePanel

GWT FormPanel嵌套类

描述
FormPanel.SubmitCompleteHandler 它处理FormPanel.SubmitCompleteEvent事件。
FormPanel.SubmitCompleteEvent 成功提交表单后将触发该事件。
FormPanel.SubmitEvent 提交表单时将其触发
FormPanel.SubmitHandler 它处理FormPanel.SubmitEvent事件。

GWT FormPanel构造函数

建设者 描述
FormPanel() 它创建一个新的FormPanel。
FormPanel(Element element) 子类使用它显式使用现有元素。
FormPanel(Element element, boolean createIFrame) 子类使用它显式使用现有元素。
FormPanel(NamedFrame frameTarget) 它创建一个以NamedFrame为目标的FormPanel。
FormPanel(NamedFrame frameTarget) 它创建一个以NamedFrame为目标的FormPanel。
FormPanel(java.lang.String target) 它创建一个新的FormPanel, 将字符串作为输入。

GWT FormPanel方法

修饰符和类型 方法 描述
void addFormHandler(FormHandler handler) 它将一个窗体小部件添加到面板。
java.lang.String getAction() 它获取与此表单关联的“操作”。
java.lang.String getEncoding() 它获取用于提交此表单的编码。
java.lang.String getMethod() 它获取用于提交此表单的HTTP方法。
java.lang.String getTarget() 它获取表单的“目标”。
受保护的空白 onAttach() 当窗口小部件附加到浏览器的文档时, 将调用此方法。
protected void onDetach() 当小部件从浏览器的文档中分离时, 将调用此方法。
void onFrameLoad() 当目标框架完成加载时调用。
void setAction(SafeUri url) 它设置与此表单关联的“操作”。
void setAction(java.lang.String url) 它设置与此表单关联的“操作”。
void setEncoding(java.lang.String encodingType) 它设置用于提交此表单的编码。
void submit() 提交表格。
静态FormPanel wrap(Element element) 它创建一个包装现有元素的FormPanel。
静态FormPanel wrap(Element element, boolean createIFrame) 它创建一个包装现有元素的FormPanel。

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;
}

输出:

GWT FormPanel
赞(0)
未经允许不得转载:srcmini » GWT FormPanel用法

评论 抢沙发

评论前必须登录!