GWT - 單選按鈕部件



介紹

RadioButton 部件代表一個互斥選擇的單選按鈕。

類宣告

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

public class RadioButton
   extends CheckBox

CSS樣式規則

以下預設 CSS 樣式規則將應用於所有 RadioButton 部件。您可以根據您的需求覆蓋它。

.gwt-RadioButton {} 

類建構函式

序號 建構函式和說明
1

RadioButton(java.lang.String name)

建立一個與特定組名關聯的新單選按鈕。

2

RadioButton(java.lang.String name,java.lang.String label)

建立一個與特定組關聯的新單選按鈕,並使用給定的 HTML 標籤進行初始化。

3

RadioButton(java.lang.String name,java.lang.String label, boolean asHTML)

建立一個與特定組關聯的新單選按鈕,並使用給定的標籤(可選地作為 HTML)進行初始化。

類方法

序號 函式名和說明
1

void setName(java.lang.String name)

更改此單選按鈕的組名。

繼承的方法

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

  • 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.CheckBox

  • java.lang.Object

RadioButton 部件示例

此示例將引導您完成簡單的步驟,以演示在 GWT 中使用 RadioButton 部件的方法。按照以下步驟更新我們在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-RadioButton { 
   color:green;   
}

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

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

package com.tutorialspoint.client;

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

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {

       // Create some radio buttons, all in one group 'radioGroup'.
       RadioButton radioButton1 = new RadioButton("radioGroup", "First");
       RadioButton radioButton2 = new RadioButton("radioGroup", "Second");
       RadioButton radioButton3 = new RadioButton("radioGroup", "Third");

       // Check 'First' by default.
       radioButton1.setValue(true);

      //disable 'Second' radio button
      radioButton2.setEnabled(false);

      // Add toggle button to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);            
      panel.add(radioButton1);
      panel.add(radioButton2);
      panel.add(radioButton3);

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

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

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