- GWT 教程
- GWT - 首頁
- GWT - 概述
- GWT - 環境搭建
- GWT - 應用
- GWT - 建立應用
- GWT - 部署應用
- GWT - 使用 CSS 樣式
- GWT - 基本元件
- GWT - 表單元件
- GWT - 複雜元件
- GWT - 佈局面板
- GWT - 事件處理
- GWT - 自定義元件
- GWT - UIBinder
- GWT - RPC 通訊
- GWT - JUnit 整合
- GWT - 除錯應用
- GWT - 國際化
- GWT - History 類
- GWT - 書籤支援
- GWT - 日誌框架
- GWT 有用資源
- GWT - 常見問題解答
- GWT - 快速指南
- GWT - 有用資源
- GWT - 討論
GWT - 按鈕元件
簡介
按鈕元件代表一個標準的按鈕。
類宣告
以下是com.google.gwt.user.client.ui.Button類的宣告:
public class Button extends ButtonBase
CSS樣式規則
以下預設CSS樣式規則將應用於所有按鈕元件。您可以根據需要覆蓋它。
.gwt-Button { }
類建構函式
| 序號 | 建構函式及說明 |
|---|---|
| 1 |
Button() 建立一個沒有標題的按鈕。 |
| 2 |
protected Button(Element element) 子類可以使用此建構函式顯式使用現有元素。 |
| 3 |
Button(java.lang.String html) 建立一個具有給定HTML標題的按鈕。 |
| 4 |
Button(java.lang.String html, ClickListener listener) 建立一個具有給定HTML標題和點選監聽器的按鈕。 |
類方法
| 序號 | 函式名稱及說明 |
|---|---|
| 1 |
click() 使用者點選按鈕的程式設計等效項。 |
| 2 |
static Button wrap(Element element) 建立一個包裝現有<a>元素的Button元件。 |
繼承的方法
此類繼承自以下類的方法:
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.ButtonBase
java.lang.Object
按鈕元件示例
此示例將引導您完成簡單的步驟,以演示如何在GWT中使用按鈕元件。請按照以下步驟更新我們在GWT - 建立應用章節中建立的GWT應用程式:
| 步驟 | 說明 |
|---|---|
| 1 | 按照GWT - 建立應用章節中的說明,建立一個名為HelloWorld的專案,放在com.tutorialspoint包下。 |
| 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;
}
.gwt-Button {
color:red;
}
.gwt-Green-Button {
color:green;
}
.gwt-Blue-Button {
color:blue;
}
以下是修改後的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>Button Widget Demonstration</h1>
<div id = "gwtContainer"></div>
</body>
</html>
讓我們看看Java檔案src/com.tutorialspoint/HelloWorld.java的內容,它將演示按鈕元件的使用。
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.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
//create buttons
Button redButton = new Button("Red");
Button greenButton = new Button("Green");
Button blueButton = new Button("Blue");
// use UIObject methods to set button properties.
redButton.setWidth("100px");
greenButton.setWidth("100px");
blueButton.setWidth("100px");
greenButton.addStyleName("gwt-Green-Button");
blueButton.addStyleName("gwt-Blue-Button");
//add a clickListener to the button
redButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("Red Button clicked!");
}
});
//add a clickListener to the button
greenButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("Green Button clicked!");
}
});
//add a clickListener to the button
blueButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("Blue Button clicked!");
}
});
// Add button to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.setSpacing(10);
panel.add(redButton);
panel.add(greenButton);
panel.add(blueButton);
RootPanel.get("gwtContainer").add(panel);
}
}
完成所有更改後,讓我們像在GWT - 建立應用章節中一樣,在開發模式下編譯並執行應用程式。如果您的應用程式一切正常,則會產生以下結果:
當您點選點選我按鈕時,它將顯示一個警告訊息Hello World!