目录

Button

介绍 (Introduction)

Button是一个控件组件,它具有标签并在按下时生成事件。 当按下并释放按钮时,AWT通过调用按钮上的processEvent向该按钮发送ActionEvent实例。 按钮的processEvent方法接收按钮的所有事件; 它通过调用自己的processActionEvent方法传递一个动作事件。 后一种方法将动作事件传递给已注册对此按钮生成的动作事件感兴趣的任何动作侦听器。

如果应用程序想要根据按下和释放的按钮执行某些操作,它应该通过调用按钮的addActionListener方法实现ActionListener并注册新的侦听器以从此按钮接收事件。 应用程序可以使用按钮的action命令作为消息传递协议。

类声明

以下是java.awt.Button类的声明:

public class Button
   extends Component
      implements Accessible

类构造函数

SN 构造函数和描述
1

Button()

构造一个带有空字符串的按钮作为其标签。

2

Button(String text)

构造一个具有指定标签的新按钮。

类方法

SN 方法和描述
1

void addActionListener(ActionListener l)

添加指定的动作侦听器以从此按钮接收动作事件。

2

void addNotify()

创建按钮的对等方。

3

AccessibleContext getAccessibleContext()

获取与此Button关联的AccessibleContext。

4

String getActionCommand()

返回此按钮触发的动作事件的命令名称。

5

ActionListener[] getActionListeners()

返回在此按钮上注册的所有动作侦听器的数组。

6

String getLabel()

获取此按钮的标签。

7

《T extends EventListener》 T[] getListeners(Class《T》 listenerType)

返回当前在此Button上注册为FooListeners的所有对象的数组。

8

protected String paramString()

返回表示此Button状态的字符串。

9

protected void processActionEvent(ActionEvent e)

处理在此按钮上发生的操作事件,方法是将它们分派给任何已注册的ActionListener对象。

10

protected void processEvent(AWTEvent e)

处理此按钮上的事件。

11

void removeActionListener(ActionListener l)

删除指定的操作侦听器,以便它不再从此按钮接收操作事件。

12

void setActionCommand(String command)

设置此按钮触发的动作事件的命令名称。

13

void setLabel(String label)

将按钮的标签设置为指定的字符串。

方法继承

该类继承以下类中的方法:

  • java.awt.Component

  • java.lang.Object

按钮示例

使用您选择的任何编辑器创建以下java程序,例如D:/ 》 AWT 》 com 》 iowiki 》 gui 》

AwtControlDemo.java
package com.iowiki.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 com\iowiki\gui\AwtControlDemo.java

如果没有错误,那意味着编译成功。 使用以下命令运行程序。

D:\AWT>java com.iowiki.gui.AwtControlDemo

验证以下输出

AWT按钮
↑回到顶部↑
WIKI教程 @2018