Java API 分類導覽 - java.awt.Button removeActionListener()

Button 類別 (class) 的 removeActionListener() 方法 (method) 用來移除按鈕的事件處理。



修飾子與參數
public void removeActionListener(ActionListener l)


removeActionListener() 沒有回傳值 (return value) ,需要 ActionListener 型態 (type) 的物件 (object) 當參數 (paramenter) 。


舉例如下
import java.awt.*;
import java.awt.event.*;

public class ButtonDemo implements ActionListener {
    Frame frame;
    Button button1, button2, button3, button4, button5;
    
    public static void main(String[] args) {
        new ButtonDemo();
    }
    
    public ButtonDemo() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setLayout(new FlowLayout());
        
        button1 = new Button("b1");
        button2 = new Button("b2");
        button3 = new Button("b3");
        button4 = new Button("b4");
        button5 = new Button("b5");
        
        button1.setActionCommand("b1");
        button1.addActionListener(this);
        button2.setActionCommand("b2");
        button2.addActionListener(this);
        button3.setActionCommand("b3");
        button3.addActionListener(this);
        button4.setActionCommand("b4");
        button4.addActionListener(this);
        button5.setActionCommand("b5");
        button5.addActionListener(this);
        
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        
        frame.pack();
        frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd == "b1") {
            button1.setLabel("new 1");
        }
        if (cmd == "b2") {
            System.out.println("label: " + button2.getLabel());
        }
        if (cmd == "b3") {
            System.out.println("command: " + button3.getActionCommand());
        }
        if (cmd == "b4") {
            System.out.println("hello~~");
        }
        if (cmd == "b5") {
            button1.removeActionListener(this);
            button2.removeActionListener(this);
            button3.removeActionListener(this);
            button4.removeActionListener(this);
        }
    }
}

class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:ButtonDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例的 ButtonDemo 直接實作 ActionListener 介面
public class ButtonDemo implements ActionListener {


因此建立按鈕後,先呼叫 Button 的 setActionCommand() 設定按鈕的指令,然後呼叫 addActionListener() 設定事件處理, 並以 this 當參數
button1.setActionCommand("b1");
button1.addActionListener(this);
button2.setActionCommand("b2");
button2.addActionListener(this);
button3.setActionCommand("b3");
button3.addActionListener(this);
button4.setActionCommand("b4");
button4.addActionListener(this);
button5.setActionCommand("b5");
button5.addActionListener(this);


由於 ButtonDemo 實作 ActionListener 介面,因此要寫出 actionPerformed()
public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if (cmd == "b1") {
        button1.setLabel("new 1");
    }
    if (cmd == "b2") {
        System.out.println("label: " + button2.getLabel());
    }
    if (cmd == "b3") {
        System.out.println("command: " + button3.getActionCommand());
    }
    if (cmd == "b4") {
        System.out.println("hello~~");
    }
    if (cmd == "b5") {
        button1.removeActionListener(this);
        button2.removeActionListener(this);
        button3.removeActionListener(this);
        button4.removeActionListener(this);
    }
}


b1 按鈕呼叫 Button 的 setLabel() ,將按鈕顯示名稱改為 "new 1" , b2 按鈕呼叫 Button 的 getLabel() ,在命令列印出按鈕名稱, b3 按鈕呼叫 Button 的 getActionCommand() ,在命令列印出 b3 的指令, b4 在命令列印出 "hello~~" , b5 則是呼叫 Button 的 removeActionListener() ,解除 b1b2b3b4 四個按鈕的功能。


編譯後執行,結果如下



中英文術語對照
類別class
方法method
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
AWT 元件


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


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

沒有留言: