按鈕是一個控制組件,按下時有一個標(biāo)簽,并生成一個事件。當(dāng)按鈕被按下和釋放,AWT發(fā)送ActionEvent的一個實例的按鈕,通過調(diào)用按鈕上的processEvent。按鈕的processEvent方法接收所有事件的按鈕,它通過一個動作事件以及調(diào)用其自身的processActionEvent方法。后一種方法通過操作事件的動作的已注冊偵聽此按鈕所產(chǎn)生的動作事件。
如果一個應(yīng)用程序需要一個按鈕被按下和釋放的基礎(chǔ)上執(zhí)行一些動作,但要實現(xiàn)ActionListener并注冊新的偵聽器接收事件從這個按鈕,調(diào)用按鈕的addActionListener方法。應(yīng)用程序可以利用按鈕的動作命令的消息傳遞協(xié)議。
以下是聲明的 java.awt.Button類:
public class Button extends Component implements Accessible
S.N. | 構(gòu)造函數(shù)與說明 |
---|---|
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
選擇使用編輯器創(chuàng)建以下java程序 D:/ > AWT > com > yiibai.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"); 上一篇:MouseListener 接口下一篇:AWT 布局(Layouts)