java awt 按钮响应_Java AWT按钮
java awt 按鈕響應(yīng)
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類用于實(shí)現(xiàn)GUI按鈕。 只要單擊它,它就會(huì)帶有一個(gè)標(biāo)簽并生成一個(gè)事件。 如前幾節(jié)所述,它擴(kuò)展了Component類并實(shí)現(xiàn)了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.
每當(dāng)按下按鈕時(shí),它都會(huì)生成ActionEvent類的實(shí)例。 要在單擊按鈕時(shí)執(zhí)行某些功能,我們需要一個(gè)ActionListener 。 任何實(shí)現(xiàn)ActionListener接口的類都可以用于處理按鈕單擊 。 我們將在后面的部分中詳細(xì)研究如何處理事件。
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.
從代碼中可以看出,我們可以初始化帶有或不帶有標(biāo)簽的Button對(duì)象。 按鈕b1和b2已分別用文本“按鈕1”和“ B2”初始化。 我們?cè)趧?chuàng)建對(duì)象b3時(shí)使用了默認(rèn)構(gòu)造函數(shù)。 這就是為什么初始化按鈕時(shí)沒有任何文本的原因。
The setBounds() method of the button is used to set its location and size on the frame. Its signature is
按鈕的setBounds()方法用于設(shè)置其在框架上的位置和大小。 它的簽名是
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是左側(cè)的像素?cái)?shù), y是頂部的像素?cái)?shù)。 因此, x和y用于確定按鈕在框架上的位置。 接下來(lái)的兩個(gè)參數(shù)int width和int height用于設(shè)置按鈕的大小(以像素為單位)。
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()方法用于讀取調(diào)用按鈕的標(biāo)簽。 它以字符串形式返回按鈕標(biāo)簽的文本。 它的簽名是
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上調(diào)用的setLabel()方法用于重命名/設(shè)置按鈕的標(biāo)簽。 最初,創(chuàng)建b3時(shí)使用一個(gè)空字符串作為標(biāo)簽。 使用setLabel()方法將其標(biāo)簽更改為“ 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方法將所有按鈕添加到框架中(如上一節(jié)所述)。
翻譯自: https://www.includehelp.com/java/awt-button.aspx
java awt 按鈕響應(yīng)
總結(jié)
以上是生活随笔為你收集整理的java awt 按钮响应_Java AWT按钮的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 茱莉亚分形_茱莉亚的NaN Consta
- 下一篇: 单位矩阵的逆| 使用Python的线性代