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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值