java awt 按钮响应_Java AWT按钮

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

输出量

Java AWT Button

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 按钮响应

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540527.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qgis在地图上画导航线_在Laravel中的航线

qgis在地图上画导航线For further process we need to know something about it, 为了进一步处理,我们需要了解一些有关它的信息, The route is a core part in Laravel because it maps the controller for sending a request which is automatically …

Logistic回归和SVM的异同

这个问题在最近面试的时候被问了几次,让谈一下Logistic回归(以下简称LR)和SVM的异同。由于之前没有对比分析过,而且不知道从哪个角度去分析,一时语塞,只能不知为不知。 现在对这二者做一个对比分析&#xf…

构建安全网络 比格云全系云产品30天内5折购

一年之计在于春,每年的三、四月,都是个人创业最佳的起步阶段,也是企业采购最火热的时期。为了降低用户的上云成本,让大家能无门槛享受到优质高性能的云服务,比格云从3月16日起,将上线“充值30天内&#xff…

数据结构 基础知识

一。逻辑结构: 是指数据对象中数据 素之间的相互关系。 其实这也是我 今后最需要关注的问题 逻辑结构分为以 四种1. 集合结构 2.线性结构 3.数形结构 4,图形结构 二。物理结构: 1,顺序存储结,2 2. 链式存储结构 一,时间复杂…

ruby 变量类中范围_Ruby中的类

ruby 变量类中范围Ruby类 (Ruby Classes) In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object o…

以云计算的名义 驻云科技牵手阿里云

本文讲的是以云计算的名义 驻云科技牵手阿里云一次三个公司的牵手 可能会改变无数企业的命运 2017年4月17日,对于很多人来说可能只是个平常的工作日,但是对于国内无数的企业来说却可能是个会改变企业命运的日。驻云科技联合国内云服务提供商阿里云及国外…

浏览器端已支持 ES6 规范(包括 export import)

当然,是几个比较优秀的浏览器,既然是优秀的浏览器,大家肯定知道是那几款啦,我就不列举了,我用的是 chrome。 对 script 声明 type 为 module 后就可以享受 es6 规范所带来的模块快感了。 基础语法既然是全支持&#xf…

一文读懂深度学习框架下的目标检测(附数据集)

从简单的图像分类到3D位置估算,在机器视觉领域里从来都不乏有趣的问题。其中我们最感兴趣的问题之一就是目标检测。 如同其他的机器视觉问题一样,目标检测目前为止还没有公认最好的解决方法。在了解目标检测之前,让我们先快速地了解一下这个领…

设计一个应用程序,以在C#中的按钮单击事件上在MessageBox中显示TextBox中的文本...

Here, we took two controls on windows form that are TextBox and Button, named txtInput and btnShow respectively. We have to write C# code to display TextBox’s text in the MessageBox on Button Click. 在这里,我们在Windows窗体上使用了两个控件&…

Oracle优化器:星型转换(Star Query Transformation )

Oracle优化器:星型转换(Star Query Transformation )Star query是一个事实表(fact table)和一些维度表(dimension)的join。每个维度表都跟事实表通过主外键join,且每个维度表之间不j…

JavaScript | 声明数组并使用数组索引分配元素的代码

Declare an array, assign elements by indexes and print all elements in JavaScript. 声明一个数组&#xff0c;通过索引分配元素&#xff0c;并打印JavaScript中的所有元素。 Code: 码&#xff1a; <html><head><script>var fruits [];fruits[0]"…

Kubernetes基础组件概述

本文讲的是Kubernetes基础组件概述【编者的话】最近总有同学问Kubernetes中的各个组件的相关问题&#xff0c;其实这些概念内容在官方文档中都有&#xff0c;奈何我们有些同学可能英文不好&#xff0c;又或者懒得去看&#xff0c;又或者没有找到&#xff0c;今天有时间就专门写…

c语言将链表写入二进制文件_通过逐级遍历将二进制树转换为单链表的C程序

c语言将链表写入二进制文件Problem statement: Write a C program to convert a binary tree into a single linked list by traversing level-wise. 问题陈述&#xff1a;编写一个C程序&#xff0c;通过逐级遍历将二进制树转换为单个链表 。 Example: 例&#xff1a; The ab…

洛谷 P2689 东南西北【模拟/搜索】

题目描述 给出起点和终点的坐标及接下来T个时刻的风向(东南西北)&#xff0c;每次可以选择顺风偏移1个单位或者停在原地。求到达终点的最少时间。 如果无法偏移至终点&#xff0c;输出“-1”。 输入输出格式 输入格式&#xff1a; 第一行两个正整数x1,y1&#xff0c;表示小明所…

单链表遍历_单链表及其遍历实现的基本操作

单链表遍历单链表 (Single linked list) Single linked list contains a number of nodes where each node has a data field and a pointer to next node. The link of the last node is to NULL, indicates end of list. 单个链表包含许多节点&#xff0c;其中每个节点都有一…

8086简单的指令流水线_在8086微处理器中执行流水线的指令和概念的步骤

8086简单的指令流水线Any computer or machine works according to some instructions. These instructions are responsible for all the work that the machine does. But how does a machine work to understand and execute that instruction? 任何计算机或机器都按照某些…

node.js 爬虫入门总结

node.js爬虫 前端同学可能向来对爬虫不是很感冒&#xff0c;觉得爬虫需要用偏后端的语言&#xff0c;诸如 php &#xff0c; python 等。当然这是在 nodejs 前了&#xff0c;nodejs 的出现&#xff0c;使得 Javascript 也可以用来写爬虫了。由于 nodejs 强大的异步特性&#xf…

将八进制数制转换为二进制,十进制和十六进制数制

1)将八进制数制转换为二进制数制 (1) Conversion of Octal Number System to Binary Number System) To convert octal numbers into binary numbers, we can use the relationship between octal and binary numbers. 要将八进制数转换为二进制数&#xff0c;我们可以使用八进…

想提高用户访问的响应速度和成功率还不赶快学习CDN

2019独角兽企业重金招聘Python工程师标准>>> 课程介绍 CDN可以将源站内容分发至最接近用户的节点&#xff0c;使用户可就近取得所需内容&#xff0c;提高用户访问的响应速度和成功率。解决因分布、带宽、服务器性能带来的访问延迟问题&#xff0c;适用于站点加速、点…

Python 核心编程(第二版)——条件和循环

Python 中的 if 子句由三部分组成: 关键字本身&#xff0c;用于判断结果真假的条件表达式&#xff0c; 以及当表达式为真或者非零时执行的代码块。if 语句的语法如下: if expression: expr_true_suite 单个 if 语句可以通过使用布尔操作符 and , or 和 not实现多重判断条件或…