Gwt-togglebutton-widget
GWT-トグルボタンウィジェット
前書き
*ToggleButton* ウィジェットは、ユーザーがアップ状態とダウン状態を切り替えることができるスタイリッシュなステートフルボタンを表します。
クラス宣言
以下は com.google.gwt.user.client.ui.ToggleButton クラスの宣言です-
public class ToggleButton
extends CustomButton
CSSスタイルルール
次のデフォルトのCSSスタイルルールは、すべてのToggleButtonウィジェットに適用されます。 要件に応じて上書きできます。
.gwt-ToggleButton-up {}
.gwt-ToggleButton-down {}
.gwt-ToggleButton-up-hovering {}
.gwt-ToggleButton-down-hovering {}
.gwt-ToggleButton-up-disabled {}
.gwt-ToggleButton-down-disabled {}
クラスコンストラクター
Sr.No. | Constructor & Description |
---|---|
1 |
ToggleButton() ToggleButtonのコンストラクター。 |
2 |
ToggleButton(Image upImage) アップ状態の画像でトグルボタンを作成します。 |
3 |
ToggleButton(Image upImage, ClickListener listener) アップ状態の画像とclickListenerでToggleButtonを作成します。 |
4 |
ToggleButton(Image upImage, Image downImage) アップ状態の画像でトグルボタンを作成します。 |
5 |
ToggleButton(Image upImage, Image downImage, ClickListener listener) アップ状態の画像でトグルボタンを作成します。 |
6 |
ToggleButton(java.lang.String upText) アップ状態テキストでトグルボタンを作成します。 |
7 |
ToggleButton(java.lang.String upText, ClickListener listener) アップ状態のテキストとクリックリスナーを使用してトグルボタンを作成します。 |
8 |
ToggleButton(java.lang.String upText, java.lang.String downText) アップ状態とダウン状態のテキストを含むトグルボタンを作成します。 |
9 |
ToggleButton(java.lang.String upText, java.lang.String downText, ClickListener listener) アップ状態、ダウン状態テキスト、クリックリスナーを備えたToggleButtonを作成します。 |
クラスメソッド
Sr.No. | Function name & Description |
---|---|
1 |
boolean isDown() このボタンは押されていますか? |
2 |
protected void onClick() ユーザーがこのボタンをクリックし終わったときに呼び出されます。 |
3 |
void setDown(boolean down) このボタンが押されているかどうかを設定します。 |
継承されるメソッド
このクラスは、次のクラスからメソッドを継承します-
- 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.CustomWidget
- java.lang.Object
ToggleButtonウィジェットの例
この例では、GWTでのトグルボタンウィジェットの使用方法を示す簡単な手順を紹介します。 次の手順に従って、_GWTで作成したGWTアプリケーションを更新します-アプリケーションの作成_の章-
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.finddevguides as explained in the GWT - Create Application chapter. |
2 | Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorldl and HelloWorld.java as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to verify the result of the implemented logic. |
以下は、変更されたモジュール記述子 src/com.finddevguides/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.finddevguides.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-ToggleButton-up {
color:green;
}
.gwt-ToggleButton-down {
color:blue;
}
.gwt-ToggleButton-up-hovering {
color:pink;
}
.gwt-ToggleButton-down-hovering {
color:aqua;
}
.gwt-ToggleButton-up-disabled {
color:lime;
}
.gwt-ToggleButton-down-disabled {
color:maroon;
}
以下は、変更されたHTMLホストファイル war/HelloWorldl の内容です。
<html>
<head>
<title>Hello World</title>
<link rel = "stylesheet" href = "HelloWorld.css"/>
<script language = "javascript" src = "helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<h1>ToggleButton Widget Demonstration</h1>
<div id = "gwtContainer"></div>
</body>
</html>
ToggleButtonウィジェットの使用方法を示すJavaファイル src/com.finddevguides/HelloWorld.java の内容を見てみましょう。
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
//create toggle buttons
ToggleButton toggleButton = new ToggleButton("Click Me!");
ToggleButton toggleButton1 = new ToggleButton("Click Me!");
//disable a toggle button
toggleButton1.setEnabled(false);
//add a clickListener to the toggle button
toggleButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("Hello World!");
}
});
//Add toggle button to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.setSpacing(10);
panel.add(toggleButton);
panel.add(toggleButton1);
RootPanel.get("gwtContainer").add(panel);
}
}
すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-
*Click Me* ボタンをクリックすると、警告メッセージ* Hello World!*が表示されます。
ボタンテキストの色は、操作に応じて変わることがわかります。
- ボタンにカーソルを合わせると、色がピンクになります。
- ボタンを押すと、色が青になります。
- ボタンを離すと、ボタンは下がったままになります。