介绍

按钮是一个控制组件,按下时有一个标签,并生成一个事件。当按钮被按下和释放,AWT发送ActionEvent的一个实例的按钮,通过调用按钮上的processEvent。按钮的processEvent方法接收所有事件的按钮,它通过一个动作事件以及调用其自身的processActionEvent方法。后一种方法通过操作事件的动作的已注册侦听此按钮所产生的动作事件。

如果一个应用程序需要一个按钮被按下和释放的基础上执行一些动作,但要实现ActionListener并注册新的侦听器接收事件从这个按钮,调用按钮的addActionListener方法。应用程序可以利用按钮的动作命令的消息传递协议。

类的声明

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

publicclassButtonextendsComponentimplementsAccessible

类的构造函数

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[] getListeners(Class 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 > yiibai.com > gui >

AwtControlDemo.java

packagecom.yiibai.gui;importjava.awt.*;importjava.awt.event.*;publicclassAwtControlDemo{privateFramemainFrame;privateLabelheaderLabel;privateLabelstatusLabel;privatePanelcontrolPanel;publicAwtControlDemo(){prepareGUI();}publicstaticvoidmain(String[]args){AwtControlDemoawtControlDemo=newAwtControlDemo();awtControlDemo.showButtonDemo();}privatevoidprepareGUI(){mainFrame=newFrame("Java AWT Examples");mainFrame.setSize(400,400);mainFrame.setLayout(newGridLayout(3,1));mainFrame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventwindowEvent){System.exit(0);}});headerLabel=newLabel();headerLabel.setAlignment(Label.CENTER);statusLabel=newLabel();statusLabel.setAlignment(Label.CENTER);statusLabel.setSize(350,100);controlPanel=newPanel();controlPanel.setLayout(newFlowLayout());mainFrame.add(headerLabel);mainFrame.add(controlPanel);mainFrame.add(statusLabel);mainFrame.setVisible(true);}privatevoidshowButtonDemo(){headerLabel.setText("Control in action: Button");ButtonokButton=newButton("OK");ButtonsubmitButton=newButton("Submit");ButtoncancelButton=newButton("Cancel");okButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){statusLabel.setText("Ok Button clicked.");}});submitButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){statusLabel.setText("Submit Button clicked.");}});cancelButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){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

验证下面的输出

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

java awt button_AWT Button类相关推荐

  1. java.awt.color,AWT Color类

    颜色类规定在默认sRGB颜色空间或颜色的任意颜色空间中的颜色确定的颜色. 类的声明 以下是声明java.awt.Color类: publicclassColorextendsObjectimpleme ...

  2. java中的actionlistener_JAVA事件监听器之BUTTON类中的ADDACTIONLISTENER(ACTIONLISTENER L)方法...

    JAVA:事件监听器之Button类中的addActionListener(ActionListener l)方法 addActionListener public void addActionLis ...

  3. java awt table_java.awt.image 类 LookupTable - Java 中文参考手册

    java.lang.Object java.awt.image.LookupTable 直接已知子类:ByteLookupTable, ShortLookupTable public abstract ...

  4. java语言怎么建立窗口awt,java.awt.Frame类:创建窗口

    创建窗口是 AWT 编程的第一步,窗口是所有组件和面板的容器. 在 Java AWT 中,我们使用 Frame 类来创建窗口.Frame 中文字意是"框架",它的主要功能是建立窗口 ...

  5. java awt canvas_java.awt 类 Canvas - Java 中文参考手册

    java.lang.Object java.awt.Component java.awt.Canvas 所有已实现的接口:ImageObserver, MenuContainer, Serializa ...

  6. JAVA:事件监听器之Button类中的addActionListener(ActionListener l)方法

    addActionListener public void addActionListener(ActionListener l) 添加指定的动作侦听器,以接收发自此按钮的动作事件.当用户在此按钮上按 ...

  7. java中的actionlistener_JAVA:事件监听器之Button类中的addActionListener(ActionListener l)方法...

    1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 public class FrameDemo 5 { ...

  8. java.awt.font 宋体,SpringBoot项目集成字体工具类

    场景:采用JasperReport生成报表时,若将模版中的字体配置成宋体时.部署在不同的系统上时,可能会出现中文乱码的情况,也可以适用于其他任何需要单独配置字体的第三方库. 首先,你可以给部署的服务器 ...

  9. java awt区域_java的awt包中有没有表示区域的类或者方法,可以传递一个Rectangle

    展开全部 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; final cla ...

最新文章

  1. 2020 年,人工智能和深度学习未来的五大趋势
  2. FFmpeg windows软件开发环境搭建
  3. MySQL同步复制搭建方法指南详细步骤
  4. 011_CSS子元素选择器
  5. 在一个类型的继承体系中,成员的构造顺序
  6. Win10电脑如何设置环境变量
  7. 运动目标跟踪(十八)--阶段性总结
  8. c++构造函数分类说明
  9. 主线程 唤醒_JAVA多线程--线程阻塞与唤醒
  10. VueRouter进阶(1)-导航守卫
  11. 每天学点5G-5G NEF
  12. h5页面预览pdf文件_H5 页面如何查看 PDF 文件
  13. c语言system title,system() – C语言库函数
  14. 伯努利-欧拉装错信封问题
  15. java中小数位数的限制,Java中限制小数位数有关问题
  16. Qt深入浅出(十五)QTableView
  17. 什么是nuget?nuget包是如何管理
  18. JAVA基础––从类和对象开始
  19. App通过(后台返回apk链接)下载apk并且安装
  20. 百度地图部署流程(实际生产环境部署内网)

热门文章

  1. LINUX中printf与echo的区别
  2. Oracle 导出CSV、导出大数据
  3. 在windows下如何快速搭建web.py开发框架
  4. 字符编码的发展(ASCII、Unicode、utf-8)
  5. Http协议 详解(转载)
  6. iphone 字符串
  7. 设计模式-模板模式-个人理解
  8. VUE-生命周期钩子
  9. 【HTML】处理<br>换行符追加到前端换行无效的问题 --- html中渲染的字符串中包含HTML标签无效的处理方法,字符串中包含HTML标签被转义的问题 解决
  10. 解决At least one JAR was scanned for TLDs yet contained no TLDs. 问题