日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java awt 按钮响应_Java AWT按钮

發布時間:2025/3/11 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

輸出量

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是頂部的像素數。 因此, x和y用于確定按鈕在框架上的位置。 接下來的兩個參數int width和int 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按钮的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。