JavaTM Platform
Standard Ed. 6

java.awt
クラス Button

java.lang.Object
  上位を拡張 java.awt.Component
      上位を拡張 java.awt.Button
すべての実装されたインタフェース:
ImageObserver, MenuContainer, Serializable, Accessible

public class Button
extends Component
implements Accessible

このクラスはラベル付きボタンを生成します。アプリケーションは、ボタンが押されたときにアクションが実行されるようにすることができます。このイメージは、Solaris オペレーティングシステムで表示される Quit ボタンの 3 つのビューを表します。

次のコンテキストで図を説明する

1 番目のビューは通常の状態のボタンを示します。2 番目のビューは入力フォーカスがあるときのボタンを示します。ボタンの輪郭は暗くなり、アクティブなオブジェクトであることをユーザーに示します。3 番目のビューは、ユーザーがボタンの上でマウスをクリックし、アクションの実行を要求したときのボタンを示します。

マウスを使ってボタンをクリックするジェスチャーは、マウスがボタン上で押されて離されたときに送信される ActionEvent のインスタンスに関連付けられています。いつボタンが押されたかの通知は必要であるが、いつ離されたかの通知は必要でない場合、アプリケーションは、独立した動作として processMouseEvent を特殊化することも、addMouseListener を呼び出すことによりマウスイベントのリスナーとして自身を登録することもできます。これらのメソッドは両方とも、すべてのコンポーネントの抽象スーパークラスである Component によって定義されます。

ボタンが押されて離されると、AWT はボタンの processEvent を呼び出すことにより、ボタンに ActionEvent のインスタンスを送ります。ボタンの processEvent メソッドはそのボタンのすべてのイベントを受け取ります。 ボタンは自身の processActionEvent メソッドを呼び出すことによってアクションイベントを渡します。processActionEvent メソッドはこのボタンによって生成されるアクションイベントの通知を対象として登録されているアクションリスナーにアクションイベントを渡します。

押されて離されるボタンに基づいてアクションを実行する場合、アプリケーションは、ActionListener を実装し、ボタンの addActionListener メソッドを呼び出すことにより、このボタンからイベントを受け取るように新しいリスナーを登録する必要があります。アプリケーションはボタンのアクションコマンドをメッセージプロトコルとして使用します。

導入されたバージョン:
JDK1.0
関連項目:
ActionEvent, ActionListener, Component.processMouseEvent(java.awt.event.MouseEvent), Component.addMouseListener(java.awt.event.MouseListener), 直列化された形式

入れ子のクラスの概要
protected  class Button.AccessibleAWTButton
          このクラスは Button クラス用のユーザー補助機能のサポートを実装しています。
 
クラス java.awt.Component から継承された入れ子のクラス/インタフェース
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
フィールドの概要
 
クラス java.awt.Component から継承されたフィールド
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
インタフェース java.awt.image.ImageObserver から継承されたフィールド
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
コンストラクタの概要
Button()
          そのラベルの空の文字列を持つボタンを構築します。
Button(String label)
          指定されたラベルを持つボタンを構築します。
 
メソッドの概要
 void addActionListener(ActionListener l)
          このボタンからアクションイベントを受け取るために、指定されたアクションリスナーを追加します。
 void addNotify()
          ボタンのピアを構築します。
 AccessibleContext getAccessibleContext()
          この Button に関連付けられる AccessibleContext を取得します。
 String getActionCommand()
          このボタンでトリガーされるアクションイベントのコマンド名を返します。
 ActionListener[] getActionListeners()
          このボタンに登録されているすべてのアクションリスナーの配列を返します。
 String getLabel()
          ボタンのラベルを返します。
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          この ButtonFooListener として現在登録されているすべてのオブジェクトの配列を返します。
protected  String paramString()
          この Button の状態を表す文字列を返します。
protected  void processActionEvent(ActionEvent e)
          このボタンで発生するアクションイベントを、登録されている ActionListener オブジェクトにディスパッチすることにより、アクションイベントを処理します。
protected  void processEvent(AWTEvent e)
          このボタンに関するイベントを処理します。
 void removeActionListener(ActionListener l)
          このボタンからアクションイベントを受け取らないように、指定されたアクションリスナーを削除します。
 void setActionCommand(String command)
          このボタンによってトリガーされるアクションイベントのコマンド名を設定します。
 void setLabel(String label)
          ボタンのラベルを、指定された文字列に設定します。
 
クラス java.awt.Component から継承されたメソッド
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
 
クラス java.lang.Object から継承されたメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

コンストラクタの詳細

Button

public Button()
       throws HeadlessException
そのラベルの空の文字列を持つボタンを構築します。

例外:
HeadlessException - GraphicsEnvironment.isHeadless() が true を返す場合
関連項目:
GraphicsEnvironment.isHeadless()

Button

public Button(String label)
       throws HeadlessException
指定されたラベルを持つボタンを構築します。

パラメータ:
label - ボタンの文字列ラベル。ラベルがない場合は null
例外:
HeadlessException - GraphicsEnvironment.isHeadless() が true を返す場合
関連項目:
GraphicsEnvironment.isHeadless()
メソッドの詳細

addNotify

public void addNotify()
ボタンのピアを構築します。ボタンのピアは、アプリケーションが機能を変更せずにボタンの外観を変更できるようにします。

オーバーライド:
クラス Component 内の addNotify
関連項目:
Toolkit.createButton(java.awt.Button), Component.getToolkit()

getLabel

public String getLabel()
ボタンのラベルを返します。

戻り値:
ボタンのラベル。ボタンにラベルがない場合は null
関連項目:
setLabel(java.lang.String)

setLabel

public void setLabel(String label)
ボタンのラベルを、指定された文字列に設定します。

パラメータ:
label - 新しいラベル。ボタンにラベルがない場合は null
関連項目:
getLabel()

setActionCommand

public void setActionCommand(String command)
このボタンによってトリガーされるアクションイベントのコマンド名を設定します。デフォルトでは、このアクションコマンドはボタンのラベルと一致するように設定されます。

パラメータ:
command - ボタンのアクションコマンド設定に使用される文字列文字列が null の場合、アクションコマンドはボタンのラベルと一致するように設定される
導入されたバージョン:
JDK1.1
関連項目:
ActionEvent

getActionCommand

public String getActionCommand()
このボタンでトリガーされるアクションイベントのコマンド名を返します。コマンド名が null (デフォルト) の場合、このメソッドはボタンのラベルを返します。


addActionListener

public void addActionListener(ActionListener l)
このボタンからアクションイベントを受け取るために、指定されたアクションリスナーを追加します。アクションイベントは、ユーザーがこのボタンの上でマウスボタンを押したり、離したりしたときに発生します。l が null の場合、例外はスローされず、処理も実行されません。 

AWT スレッドモデルの詳細については、「AWT Threading Issues」を参照してください。

パラメータ:
l - アクションリスナー
導入されたバージョン:
JDK1.1
関連項目:
removeActionListener(java.awt.event.ActionListener), getActionListeners(), ActionListener

removeActionListener

public void removeActionListener(ActionListener l)
このボタンからアクションイベントを受け取らないように、指定されたアクションリスナーを削除します。アクションイベントは、ユーザーがこのボタンの上でマウスボタンを押したり、離したりしたときに発生します。l が null の場合、例外はスローされず、処理も実行されません。 

AWT スレッドモデルの詳細については、「AWT Threading Issues」を参照してください。

パラメータ:
l - アクションリスナー
導入されたバージョン:
JDK1.1
関連項目:
addActionListener(java.awt.event.ActionListener), getActionListeners(), ActionListener

getActionListeners

public ActionListener[] getActionListeners()
このボタンに登録されているすべてのアクションリスナーの配列を返します。

戻り値:
このボタンのすべての ActionListener、または現在アクションリスナーが登録されていない場合は空の配列
導入されたバージョン:
1.4
関連項目:
addActionListener(java.awt.event.ActionListener), removeActionListener(java.awt.event.ActionListener), ActionListener

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
この ButtonFooListener として現在登録されているすべてのオブジェクトの配列を返します。FooListener は、addFooListener メソッドを使用して登録します。

FooListener.class といったクラスリテラルを使用して、listenerType 引数を指定できます。たとえば、このアクションリスナーに対する Button b の照会は次のコードで行います。  

ActionListener[] als = (ActionListener[])(b.getListeners(ActionListener.class));
このようなリスナーがない場合は空の配列を返します。

オーバーライド:
クラス Component 内の getListeners
パラメータ:
listenerType - 要求されるリスナーの型。java.util.EventListener の下位インタフェースを指定
戻り値:
このボタンに FooListener として登録されているすべてのオブジェクトの配列。またはそのようなリスナーが追加されていない場合は空の配列
例外:
ClassCastException - listenerTypejava.util.EventListener を実装するクラスまたはインタフェースを指定しない場合
導入されたバージョン:
1.3
関連項目:
getActionListeners()

processEvent

protected void processEvent(AWTEvent e)
このボタンに関するイベントを処理します。イベントが ActionEvent のインスタンスである場合、このメソッドは processActionEvent メソッドを呼び出します。そうでない場合はスーパークラスの processEvent を呼び出します。 

イベントパラメータが null の場合の動作は定義されていないため例外がスローされます。

オーバーライド:
クラス Component 内の processEvent
パラメータ:
e - イベント
導入されたバージョン:
JDK1.1
関連項目:
ActionEvent, processActionEvent(java.awt.event.ActionEvent)

processActionEvent

protected void processActionEvent(ActionEvent e)
このボタンで発生するアクションイベントを、登録されている ActionListener オブジェクトにディスパッチすることにより、アクションイベントを処理します。

このボタンに対してアクションイベントが使用可能でない場合、このメソッドは呼び出されません。アクションイベントは、次のどちらかの場合に使用可能になります。  

 

イベントパラメータが null の場合の動作は定義されていないため例外がスローされます。

パラメータ:
e - アクションイベント
導入されたバージョン:
JDK1.1
関連項目:
ActionListener, addActionListener(java.awt.event.ActionListener), Component.enableEvents(long)

paramString

protected String paramString()
この Button の状態を表す文字列を返します。このメソッドはデバッグ専用であり、返される文字列の内容および形式は実装によって異なります。返される文字列は空の場合がありますが、null にはなりません。

オーバーライド:
クラス Component 内の paramString
戻り値:
このボタンのパラメータ文字列

getAccessibleContext

public AccessibleContext getAccessibleContext()
この Button に関連付けられる AccessibleContext を取得します。AccessibleContext はボタンのフォームを AccessibleAWTButton から取得します。必要な場合は新しい AccessibleAWTButton インスタンスを生成します。

定義:
インタフェース Accessible 内の getAccessibleContext
オーバーライド:
クラス Component 内の getAccessibleContext
戻り値:
この ButtonAccessibleContext として機能する AccessibleAWTButton
導入されたバージョン:
1.3

JavaTM Platform
Standard Ed. 6

バグの報告と機能のリクエスト
さらに詳しい API リファレンスおよび開発者ドキュメントについては、Java SE 開発者用ドキュメントを参照してください。開発者向けの詳細な解説、概念の概要、用語の定義、バグの回避策、およびコード実例が含まれています。

Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Documentation Redistribution Policy も参照してください。