- GWT 教程
- GWT - 主頁
- GWT - 概覽
- GWT - 環境設定
- GWT - 應用程式
- GWT - 建立應用程式
- GWT - 部署應用程式
- GWT - 使用 CSS 進行樣式設定
- GWT - 基本小部件
- GWT - 表單小部件
- GWT - 複雜小部件
- GWT - 佈局面板
- GWT - 事件處理
- GWT - 自定義小部件
- GWT - UIBinder
- GWT - RPC 通訊
- GWT - JUnit 整合
- GWT - 除錯應用程式
- GWT - 國際化
- GWT - 歷史記錄類
- GWT - 書籤支援
- GWT - 日誌記錄框架
- GWT 實用資源
- GWT - 常見問題解答
- GWT - 快速指南
- GWT - 實用資源
- GWT - 討論
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.xml、HelloWorld.css、HelloWorld.html 和 HelloWorld.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 - 建立應用程式 一章中所做的那樣。如果您的應用程式一切正常,它將生成以下結果 -