- 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 - 橫向面板部件 (HorizontalPanel)
介紹
HorizontalPanel 部件表示一個面板,它將所有部件水平排列在一列中。
類宣告
以下是 com.google.gwt.user.client.ui.HorizontalPanel 類的宣告:
public class HorizontalPanel
extends CellPanel
implements HasAlignment, InsertPanel.ForIsWidget
類建構函式
| 序號 | 建構函式 & 描述 |
|---|---|
| 1 |
HorizontalPanel() 空水平面板的建構函式 |
類方法
| 序號 | 函式名稱 & 描述 |
|---|---|
| 1 |
void add(Widget w) 新增子部件。 |
| 2 |
HasHorizontalAlignment.HorizontalAlignmentConstant getHorizontalAlignment() 獲取水平對齊方式。 |
| 3 |
HasVerticalAlignment.VerticalAlignmentConstant getVerticalAlignment() 獲取垂直對齊方式。 |
| 4 |
void insert(IsWidget w, int beforeIndex) |
| 5 |
void insert(Widget w, int beforeIndex) 在指定索引之前插入子部件。 |
| 6 |
protected void onEnsureDebugId(java.lang.String baseID) 受影響的元素:-# = 給定索引處的單元格。 |
| 7 |
boolean remove(Widget w) 移除子部件。 |
| 8 |
void setHorizontalAlignment(HasHorizontalAlignment.HorizontalAlignmentConstant align) 設定新增到此面板的部件使用的預設水平對齊方式。 |
| 9 |
void setVerticalAlignment(HasVerticalAlignment.VerticalAlignmentConstant align) 設定新增到此面板的部件使用的預設垂直對齊方式。 |
繼承的方法
此類繼承自以下類的方法:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.ComplexPanel
com.google.gwt.user.client.ui.CellPanel
java.lang.Object
HorizontalPanel 部件示例
此示例將引導您完成簡單的步驟,以演示如何在 GWT 中使用 HorizontalPanel 部件。請按照以下步驟更新我們在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-CheckBox {
margin: 10px;
}
以下是修改後的 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>HorizontalPanel Widget Demonstration</h1>
<div id = "gwtContainer"></div>
</body>
</html>
讓我們來看一下 Java 檔案src/com.tutorialspoint/HelloWorld.java的內容,它將演示 HorizontalPanel 部件的使用。
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
// Create a horizontal panel
HorizontalPanel horizontalPanel = new HorizontalPanel();
// Add CheckBoxes to horizontal Panel
for(int i = 1; i <= 10; i++){
CheckBox checkBox = new CheckBox("Item" + i);
horizontalPanel.add(checkBox);
}
DecoratorPanel decoratorPanel = new DecoratorPanel();
decoratorPanel.setWidth("500");
decoratorPanel.add(horizontalPanel);
// Add the widgets to the root panel.
RootPanel.get().add(decoratorPanel);
}
}
完成所有更改後,讓我們像在GWT - 建立應用章節中一樣,在開發模式下編譯並執行應用程式。如果應用程式一切正常,則會產生以下結果: