介绍

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

如果应用程序要基于按下和释放的按钮执行某些操作,则应实现ActionListener并注册新的侦听器,以通过调用按钮的addActionListener方法来接收来自此按钮的事件。应用程序可以将按钮的action命令用作消息传递协议。

类声明

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

  1. public class Button
  2. extends Component
  3. implements Accessible

类的构造函数

S.N.Constructor & Description
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.Method & Description
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

按钮示例

使用您选择的任何编辑器在 D:/ > AWT > com > tutorialspoint > gui >创建以下Java程序 AwtControlDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtControlDemo {
  7.  
  8. private Frame mainFrame;
  9. private Label headerLabel;
  10. private Label statusLabel;
  11. private Panel controlPanel;
  12.  
  13. public AwtControlDemo(){
  14. prepareGUI();
  15. }
  16.  
  17. public static void main(String[] args){
  18. AwtControlDemo awtControlDemo = new AwtControlDemo();
  19. awtControlDemo.showButtonDemo();
  20. }
  21.  
  22. private void prepareGUI(){
  23. mainFrame = new Frame("Java AWT Examples");
  24. mainFrame.setSize(400,400);
  25. mainFrame.setLayout(new GridLayout(3, 1));
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent windowEvent){
  28. System.exit(0);
  29. }
  30. });
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showButtonDemo(){
  47. headerLabel.setText("Control in action: Button");
  48.  
  49. Button okButton = new Button("OK");
  50. Button submitButton = new Button("Submit");
  51. Button cancelButton = new Button("Cancel");
  52.  
  53. okButton.addActionListener(new ActionListener() {
  54. public void actionPerformed(ActionEvent e) {
  55. statusLabel.setText("Ok Button clicked.");
  56. }
  57. });
  58.  
  59. submitButton.addActionListener(new ActionListener() {
  60. public void actionPerformed(ActionEvent e) {
  61. statusLabel.setText("Submit Button clicked.");
  62. }
  63. });
  64.  
  65. cancelButton.addActionListener(new ActionListener() {
  66. public void actionPerformed(ActionEvent e) {
  67. statusLabel.setText("Cancel Button clicked.");
  68. }
  69. });
  70.  
  71. controlPanel.add(okButton);
  72. controlPanel.add(submitButton);
  73. controlPanel.add(cancelButton);
  74.  
  75. mainFrame.setVisible(true);
  76. }
  77. }

使用命令提示符编译程序。转到 D:/ > AWT 并键入以下命令。

  1. D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java

如果没有错误,则表示编译成功。使用以下命令运行程序。

  1. D:\AWT>java com.tutorialspoint.gui.AwtControlDemo

验证以下输出

awt_button.jpg