AWT Button類

介紹

按鈕是一個控制組件,按下時有一個標籤,並生成一個事件。當按鈕被按下和釋放,AWT發送ActionEvent的一個實例的按鈕,通過調用按鈕上的processEvent。按鈕的processEvent方法接收所有事件的按鈕,它通過一個動作事件以及調用其自身的processActionEvent方法。後一種方法通過操作事件的動作的已註冊偵聽此按鈕所產生的動作事件。

如果一個應用程序需要一個按鈕被按下和釋放的基礎上執行一些動作,但要實現ActionListener並註冊新的偵聽器接收事件從這個按鈕,調用按鈕的addActionListener方法。應用程序可以利用按鈕的動作命令的消息傳遞協議。

類的聲明

以下是聲明的 java.awt.Button類:

public class Button extends Component implements Accessible

類的構造函數

S.N.

構造函數與說明

1

Button()
Constructs a button with an empty string for its label.

2

Button(String text)
Constructs a new button with specified label.

類方法

S.N.

方法和說明

1

void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this button.

2

void addNotify()
Creates the peer of the button.

3

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Button.

4

String getActionCommand()
Returns the command name of the action event fired by this button.

5

ActionListener[] getActionListeners()
Returns an array of all the action listeners registered on this button.

6

String getLabel()
Gets the label of this button.

7

T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this Button.

8

protected String paramString()
Returns a string representing the state of this Button.

9

protected void processActionEvent(ActionEvent e)
Processes action events occurring on this button by dispatching them to any registered ActionListener objects.

10

protected void processEvent(AWTEvent e)
Processes events on this button.

11

void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this button.

12

void setActionCommand(String command)
Sets the command name for the action event fired by this button.

13

void setLabel(String label)
Sets the button's label to be the specified string.

繼承的方法

這個類繼承的方法從以下類:

  • java.awt.Component

  • java.lang.Object

按鈕實例

選擇使用編輯器創建以下java程序 D:/ > AWT > com > yiibai.com > gui >

AwtControlDemo.java

package com.yiibai.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showButtonDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showButtonDemo(){ headerLabel.setText("Control in action: Button"); Button okButton = new Button("OK"); Button submitButton = new Button("Submit"); Button cancelButton = new Button("Cancel"); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { statusLabel.setText("Ok Button clicked."); } }); submitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { statusLabel.setText("Submit Button clicked."); } }); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { statusLabel.setText("Cancel Button clicked."); } }); controlPanel.add(okButton); controlPanel.add(submitButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } }

編譯程序,使用命令提示符。到 D:/ > AWT 然後鍵入以下命令。

D:AWT>javac comyiibaiguiAwtControlDemo.java

如果沒有錯誤出現,這意味着編譯成功。使用下面的命令來運行程序。

D:AWT>java com.yiibai.gui.AwtControlDemo

驗證下面的輸出

AWT