Java API 分類導覽 - java.awt.Button

Button 類別 (class) 用來建立按鈕。



修飾子
public class Button


父類別
java.awt.Component


實作介面
ImageObserver
MenuContainer
Serializable
Accessible


建構子
Button()
Button(String label)


常用方法
名稱敘述
addActionListener()設定按鈕的事件處理
getActionCommand()取得按鈕的指令
getLabel()取得按鈕的標籤
removeActionListener()移除按鈕的事件處理
setActionCommand()設定按鈕的指令
setLabel()設定按鈕的標籤


舉例如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.awt.*;
import java.awt.event.*;
 
public class ButtonDemo2 {
    Frame frame;
    Button button1, button2, button3, button4, button5;
     
    public static void main(String[] args) {
        new ButtonDemo2();
    }
     
    public ButtonDemo2() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setLayout(new FlowLayout());
         
        button1 = new Button("b1");
        button2 = new Button();
        button3 = new Button("b3");
        button4 = new Button();
        button5 = new Button("b5");
         
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
         
        frame.pack();
        frame.setVisible(true);
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:ButtonDemo2.java
    功能:示範 Java 程式
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例建立五個按鈕,其中三個利用帶有 label 參數的建構子 (constructor) ,另外兩個則利用沒有參數的建構子
17
18
19
20
21
button1 = new Button("b1");
button2 = new Button();
button3 = new Button("b3");
button4 = new Button();
button5 = new Button("b5");


編譯後執行,結果如下



中英文術語對照
類別class
建構子constructor
方法method


您可以繼續參考
AWT 元件


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Button.html

沒有留言: