按鈕是一個控制元件,按下時有一個標籤,並生成一個事件。當按鈕被按下和釋放,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 extends EventListener> T[] getListeners(Class<T> 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 > tw511.com > gui >
AwtControlDemo.javapackage 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
驗證下面的輸出