GWT - 密碼文字框控制元件



介紹

PasswordTextBox 控制元件表示一個標準的單行文字框,它會視覺上隱藏輸入內容以防止竊聽。

類宣告

以下是 com.google.gwt.user.client.ui.PasswordTextBox 類的宣告:

public class PasswordTextBox
   extends TextBox

CSS 樣式規則

以下預設 CSS 樣式規則將應用於所有 PasswordTextBox 控制元件。您可以根據需要覆蓋它。

.gwt-PasswordTextBox {}

.gwt-PasswordTextBox-readonly {} 

類建構函式

序號 建構函式和說明
1

PasswordTextBox()

建立一個空的密碼文字框。

2

PasswordTextBox(Element element)

子類可以使用此建構函式顯式地使用現有元素。

類方法

序號 函式名稱和說明
1

static PasswordTextBox wrap(Element element)

建立一個 PasswordTextBox 控制元件,該控制元件包裝一個現有的 <input type='password'> 元素。

繼承的方法

此類繼承自以下類的方法:

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

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

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

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

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

  • java.lang.Object

PasswordTextBox 控制元件示例

此示例將引導您完成簡單的步驟,以演示如何在 GWT 中使用 PasswordTextBox 控制元件。按照以下步驟更新我們在GWT - 建立應用章節中建立的 GWT 應用:

步驟 說明
1 建立一個名為HelloWorld的專案,放在com.tutorialspoint包下,如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;
}

.gwt-PasswordTextBox {
   color: green; 
}

.gwt-PasswordTextBox-readonly {
   background-color: yellow;	
}

以下是修改後的 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>PasswordTextBox Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

讓我們來看一下 Java 檔案src/com.tutorialspoint/HelloWorld.java的內容,它將演示 PasswordTextBox 控制元件的使用。

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {
      //create password textboxes
      PasswordTextBox passwordTextBox1 = new PasswordTextBox(); 
      PasswordTextBox passwordTextBox2 = new PasswordTextBox();

      //add text to text box
      passwordTextBox2.setText("hell@W@rld");

      //set textbox as readonly
      passwordTextBox2.setReadOnly(true);

      // Add text boxes to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.add(passwordTextBox1);
      panel.add(passwordTextBox2);

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

完成所有更改後,讓我們像在GWT - 建立應用章節中一樣,在開發模式下編譯並執行應用程式。如果您的應用程式一切正常,則會產生以下結果:

GWT PasswordTextBox Widget
gwt_form_widgets.htm
廣告
© . All rights reserved.