java awt 按钮响应

The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface.

Button类用于实现GUI按钮。 只要单击它,它就会带有一个标签并生成一个事件。 如前几节所述,它扩展了Component类并实现了Accessible接口。

Whenever a button is pushed, it generates an instance of the ActionEvent class. To perform some functionality on a button click, we need an ActionListener. Any class implementing the ActionListener interface can be used to handle a button click. We will study in detail how to handle events in later sections.

每当按下按钮时,它都会生成ActionEvent类的实例。 要在单击按钮时执行某些功能,我们需要一个ActionListener 。 任何实现ActionListener接口的类都可以用于处理按钮单击 。 我们将在后面的部分中详细研究如何处理事件。

Consider the following code -

考虑以下代码-

import java.awt.*;
public class CreateButton{CreateButton()
{Frame f = new Frame();
Button b1 = new Button("Button 1");
Button b2 = new Button("B2");
Button b3 = new Button();
b1.setBounds(50,50,100,50);
b2.setBounds(50,100,100,50);
b3.setBounds(150,50,100,100);
f.setLayout(null);
f.setSize(300,300);
f.setVisible(true);
f.add(b1);
f.add(b2);
f.add(b3);
if(b1.getLabel() == "Button 1")
b3.setLabel("B3");
}
public static void main(String []args){CreateButton b = new CreateButton();
}
}

Output

输出量

As seen in the code, we can initialize a Button object with or without a label. Buttons b1 and b2 have been initialized with the text "Button 1" and "B2" respectively. We have used the default constructor while creating object b3. That is why the button is initialized without any text on it.

从代码中可以看出,我们可以初始化带有或不带有标签的Button对象。 按钮b1b2已分别用文本“按钮1”“ B2”初始化。 我们在创建对象b3时使用了默认构造函数。 这就是为什么初始化按钮时没有任何文本的原因。

The setBounds() method of the button is used to set its location and size on the frame. Its signature is

按钮的setBounds()方法用于设置其在框架上的位置和大小。 它的签名是

    public void setBounds(int x, int y, int width, int height).

x here is the number of pixels from the left and y is the number of pixels from the top. Thus, x and y are used to decide the position of the button on the frame. The next two parameters, int width, and int height are used to set the size of the button, in pixels.

x是左侧的像素数, y是顶部的像素数。 因此, xy用于确定按钮在框架上的位置。 接下来的两个参数int widthint height用于设置按钮的大小(以像素为单位)。

The getLabel() method is used to read the label of the button it is called on. It returns the text of the button label as a String. Its signature is

getLabel()方法用于读取调用按钮的标签。 它以字符串形式返回按钮标签的文本。 它的签名是

    public String getLabel().

The setLabel() method that is called on button b3 in the code, is used to rename/set the label of a button. Initially, b3 was created with an empty string as the label. Its label is changed to "B3" using the setLabel() method. Its signature is

在代码中的按钮b3上调用的setLabel()方法用于重命名/设置按钮的标签。 最初,创建b3时使用一个空字符串作为标签。 使用setLabel()方法将其标签更改为“ B3” 。 它的签名是

    public void setLabel(String text)

All the buttons are added in the frame using the add method of the Frame class (as discussed in previous sections).

使用Frame类的add方法将所有按钮添加到框架中(如上一节所述)。

翻译自: https://www.includehelp.com/java/awt-button.aspx

java awt 按钮响应

java awt 按钮响应_Java AWT按钮相关推荐

  1. java button click事件_java处理按钮点击事件的方法

    java处理按钮点击事件的方法 发布时间:2020-09-17 07:17:02 来源:脚本之家 阅读:70 作者:jingxian 不同的事件源可以产生不同类别的事件.例如,按钮可以发送一个Acti ...

  2. java按钮位置_java窗口按钮位置设置

    java窗口按钮位置设置 代码如下: package Day08; import java.awt.BorderLayout; import javax.swing.JButton; import j ...

  3. java 点击改变_java 单击按钮改变背景颜色

    怎样实现单击按钮改变成相应颜色!importjava.awt.*;importjava.awt.event.*;publicclassButtonColorimplementsActionListen ...

  4. java 按钮 事件_Java给按钮添加事件

    展开全部 赞一个, 提前学习, 做好预先 , 是个好习惯.java图形界面主要62616964757a686964616fe4b893e5b19e31333365636666有AWT, SWING, ...

  5. java textfield 文字居中_java.awt.TextField和TextArea:文本框和文字区控件

    在 Java AWT 内有两个与文字输入有关的类 TextField(文本框控件)和 TextArea(文本区控件),它们之间的差异在于: TextField 文本框只能输入单行文本: TextAre ...

  6. java.awt中文乱码_Java AWT窗体中文乱码问题解决-百度经验

    JavaAWT中文乱码问题: 代码如下: package com.awt.frame; import java.awt.Color; import java.awt.FileDialog; impor ...

  7. java 按钮 事件_Java 添加按钮点击事件

    展开全部 xml文件代码如下: android:id="@+id/button1" android:layout_width="wrap_content" an ...

  8. java -jar 未响应_Java 方法性能监控和统计工具 MyPerf4J

    一个针对高并发.低延迟应用设计的高性能 Java 性能监控和统计工具. 特性 高性能: 单线程支持每秒 1000 万次 响应时间的记录,每次记录只花费 73 纳秒 无侵入: 采用 JavaAgent ...

  9. 浏览器运行java后未响应_java 无法在浏览器中运行?

    用netbeans选择 run in browser然后点运行,浏览器提示JavaFX application could not launch due to system configuration ...

最新文章

  1. leetcode 225 用队列实现栈(JS)
  2. 全球顶会论文作者,28天手把手带你复现顶会论文
  3. command line
  4. CSS伪类的三种写法
  5. python 创建工具包_使用Python工具建立网站
  6. 群签名和环签名的区别_苹果企业签名和苹果超级签名的区别
  7. 四分位数(Quartiles)、十分位数(Deciles)和百分位数(Percentiles
  8. MySql 性能调优策略
  9. VBA中,正则表达式的语法介绍
  10. 亿晟科技人脸识别门禁系统方案整体解决办法
  11. 信号完整性可能遇见的五类问题
  12. Linux boot 时 USB 的初始化过程分析2
  13. Arduino造轮子—FlashSRAM优化代码
  14. 搜狗输入法模糊音设置 (非自定义短语设置)
  15. matlab滤波有几种形式,几种经典常用的滤波算法
  16. Android 8.0 开机动画,RK3326 android10.0(Q) 开机logo+开关机动画替换
  17. ISO26262解析(十二)——HARA分析
  18. SpringBoot+Vue项目线上教学平台
  19. 计算机维护宝典,分分钟教你“修电脑”的宝典,值得收藏
  20. NLP论文解读:无需模板且高效的语言微调模型(下)

热门文章

  1. 34988 Happy Reversal(二进制去取反)
  2. 计算机是如何启动的?从未上电到操作系统启动
  3. 店铺咨询系统c语言,课内资源
  4. CTF web题 wp:
  5. easyui中onchange事件_React中类似Vue的“模板语法”
  6. c语言贪吃蛇_C语言贪吃蛇完整代码
  7. 影响索引的mysql函数_mysql索引对排序的影响实例分析
  8. 就业技术书文件表格_公路工程全套资料—开工施工检验等表格范本,及监理内业常用资料...
  9. oracle迁移父子数据
  10. 编写第二个Spring程序——AOP实现