GWT - 隐藏小部件



简介

Hidden 小部件表示 HTML 表单中的隐藏字段。

类声明

下面是 com.google.gwt.user.client.ui.Hidden 类的声明 -

public class Hidden
   extends Widget
      implements HasName

类构造函数

编号 构造函数和说明
1

Hidden()

Hidden 的构造函数。

2

Hidden(Element element)

此构造函数可供子类使用,以显式使用现有元素。

3

Hidden(java.lang.String name)

Hidden 的构造函数。

4

Hidden(java.lang.String name, java.lang.String value)

Hidden 的构造函数。

类方法

编号 函数名称和说明
1

java.lang.String getDefaultValue()

获取隐藏字段的默认值。

2

java.lang.String getID()

获取隐藏字段的 id。

3

java.lang.String getName()

获取隐藏字段的名称。

4

java.lang.String getValue()

获取隐藏字段的值。

5

void setDefaultValue(java.lang.String defaultValue)

设置隐藏字段的默认值。

6

void setID(java.lang.String id)

设置隐藏字段的 id。

7

void setName(java.lang.String name)

设置隐藏字段的名称。

8

void setValue(java.lang.String value)

设置隐藏字段的值。

9

static Hidden wrap(Element element)

创建一个 Hidden 小部件,该小部件包装现有的 <input type='hidden'> 元素。

继承方法

此类从以下类继承方法 -

  • com.google.gwt.user.client.ui.UIObject

  • com.google.gwt.user.client.ui.Widget

  • java.lang.Object

隐藏小部件示例

此示例将引导您完成简单的步骤,以了解如何在 GWT 中使用 Hidden 小部件。请按照以下步骤更新我们在 GWT - 创建应用程序 章节中创建的 GWT 应用程序 -

步骤 说明
1 com.tutorialspoint 包下创建一个名为 HelloWorld 的项目,如 GWT - 创建应用程序 章节中所述。
2 修改 HelloWorld.gwt.xmlHelloWorld.cssHelloWorld.htmlHelloWorld.java,如下所示。保持其余文件不变。
3 编译并运行应用程序以验证已实现逻辑的结果。

以下是修改后的模块描述符 src/com.tutorialspoint/HelloWorld.gwt.xml 的内容。

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
   <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name = 'com.google.gwt.user.User'/>

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name = 'com.google.gwt.user.theme.clean.Clean'/>

   <!-- Specify the app entry point class.                         -->
   <entry-point class = 'com.tutorialspoint.client.HelloWorld'/>

   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>

</module>

以下是修改后的样式表文件 war/HelloWorld.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;
}

以下是修改后的 HTML 主机文件 war/HelloWorld.html 的内容。

<html>
   <head>
      <title>Hello World</title>
      <link rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>

   <body>
      <h1>Hidden Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

让我们提供 Java 文件 src/com.tutorialspoint/HelloWorld.java 的以下内容,该内容将展示 Hidden 小部件的使用。

package com.tutorialspoint.client;

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.Hidden;
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 HelloWorld implements EntryPoint {
   public void onModuleLoad() {
      //create textboxes
      final TextBox textBox = new TextBox(); 
      textBox.setWidth("275");
      Button button1 = new Button("Set Value of Hidden Input");
      Button button2 = new Button("Get Value of Hidden Input");
      final Hidden hidden = new Hidden();

      button1.addClickHandler(new ClickHandler() {
         @Override
         public void onClick(ClickEvent event) {
            hidden.setValue(textBox.getValue());
            Window.alert("Value of Hidden Widget Updated!");
         }
      });

      button2.addClickHandler(new ClickHandler() {
         @Override
         public void onClick(ClickEvent event) {
            Window.alert("Value of Hidden Widget: " + hidden.getValue());
         }
      });

      // Add widgets to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.add(textBox);
      panel.add(button1);
      panel.add(hidden);
      panel.add(button2);

      RootPanel.get("gwtContainer").add(panel);
   }	
}

一旦您完成所有更改,让我们编译并运行开发者模式中的应用程序,就像我们在 GWT - 创建应用程序 一章中所做的那样。如果您的应用程序一切正常,它将生成以下结果 -

GWT Hidden Widget
gwt_form_widgets.htm
广告
© . All rights reserved.