Gwt-button-widget

提供:Dev Guides
移動先:案内検索

GWT-ボタンウィジェット

前書き

*Button* ウィジェットは、標準のプッシュボタンを表します。

クラス宣言

以下は com.google.gwt.user.client.ui.Button クラスの宣言です-

public class Button
   extends ButtonBase

CSSスタイルルール

次のデフォルトのCSSスタイルルールがすべてのボタンウィジェットに適用されます。 要件に応じて上書きできます。

.gwt-Button { }

クラスコンストラクター

Sr.No. Constructor & Description
1

Button()

キャプションのないボタンを作成します。

2

protected Button(Element element)

このコンストラクタは、既存の要素を明示的に使用するためにサブクラスによって使用される場合があります。

3

Button(java.lang.String html)

指定されたHTMLキャプションでボタンを作成します。

4

Button(java.lang.String html, ClickListener listener)

指定されたHTMLキャプションとクリックリスナーでボタンを作成します。

クラスメソッド

Sr.No. Function name & Description
1

click()

ユーザーがボタンをクリックするのと同等のプログラム。

2

static Button wrap(Element element)

既存の<a>要素をラップするボタンウィジェットを作成します。

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • 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アプリケーションを更新します-アプリケーションの作成_の章-

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-Button {
   color:red;
}

.gwt-Green-Button {
   color:green;
}

.gwt-Blue-Button {
   color:blue;
}

以下は、変更された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>Button Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

ボタンウィジェットの使用法を示すJavaファイル src/com.finddevguides/HelloWorld.java の内容を見てみましょう。

package com.finddevguides.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);
   }
}

すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-

GWTボタンウィジェット

*Click Me* ボタンをクリックすると、警告メッセージ* Hello World!*が表示されます。