AWT Button 按钮类
简介
Button 按钮是一个控制组件,它有一个标签,按下时会生成一个事件。按下并释放按钮时,AWT 会通过调用按钮上的 processEvent 向按钮发送一个 ActionEvent 实例。按钮的 processEvent 方法接收按钮的所有事件;它通过调用自己的 processActionEvent 方法传递动作事件。后一种方法将动作事件传递给已注册对此按钮生成的动作事件感兴趣的任何动作侦听器。
如果应用程序想要根据按下和释放按钮来执行某些动作,它应该实现 ActionListener 并注册新的侦听器以通过调用按钮的 addActionListener 方法从此按钮接收事件。应用程序可以使用按钮的操作命令作为消息传递协议。
类声明
以下是 java.awt.Button 类的声明:
public class Button extends Component implements Accessible
类构造函数
S.N. | 构造函数 &描述 |
---|---|
1 | Button() 构造一个带有空字符串标签的按钮。 |
2 | Button(String text) 构造一个带有指定标签的新按钮。 |
类方法
S.N. | 方法 &描述 |
---|---|
1 | void addActionListener(ActionListener l) 添加指定的操作侦听器以接收来自此按钮的操作事件。 |
2 | void addNotify() 创建按钮的对等体。 |
3 | AccessibleContext getAccessibleContext() 获取与此按钮关联的 AccessibleContext。 |
4 | String getActionCommand() 返回由此按钮触发的操作事件的命令名称按钮。 |
5 | ActionListener[] getActionListeners() 返回此按钮上注册的所有动作监听器的数组。 |
6 | String getLabel() 获取此按钮的标签。 |
7 | <T extends EventListener> T[] getListeners(Class<T> listenerType) 返回当前在此按钮上注册为 FooListeners 的所有对象的数组。 |
8 | protected String paramString() 返回表示此按钮状态的字符串。 |
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
Button 示例
使用您选择的任何编辑器在 D:/ > AWT > com > tutorialspoint > gui > 中创建以下 Java 程序
AwtControlDemo.javapackage com.tutorialspoint.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 utorialspoint\gui\AwtControlDemo.java
如果没有错误,则表示编译成功。使用以下命令运行程序。
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
验证以下输出
