修飾子與參數
public void remove(int index)
public void remove(Component comp)
remove() 有兩種參數版本,都沒有回傳值 (return value) ,參數 (paramenter) comp 為 Component 的物件 (object) , index 為索引值。
舉例如下
import java.awt.*;
import java.awt.event.*;
public class removeDemo implements ActionListener {
Frame frame;
Button button1, button2, button3, button4, button5;
public static void main(String[] args) {
new removeDemo();
}
public removeDemo() {
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") {
frame.remove(button1);
}
if (cmd == "b2") {
frame.remove(button2);
}
if (cmd == "b3") {
frame.remove(button3);
}
if (cmd == "b4") {
frame.remove(button4);
}
if (cmd == "b5") {
frame.remove(button5);
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:removeDemo.java
功能:示範 Java 程式
作者:張凱慶
時間:西元 2012 年 3 月 */此例建立 frame 之後,依次建立五個按鈕
Button button1 = new Button("b1");
Button button2 = new Button("b2");
Button button3 = new Button("b3");
Button button4 = new Button("b4");
Button button5 = new Button("b5");點擊特定按鈕,相關事件處理就會呼叫 Container 的 remove() ,從 frame 中移除掉該按鈕的參考變數
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd == "b1") {
frame.remove(button1);
}
if (cmd == "b2") {
frame.remove(button2);
}
if (cmd == "b3") {
frame.remove(button3);
}
if (cmd == "b4") {
frame.remove(button4);
}
if (cmd == "b5") {
frame.remove(button5);
}
}編譯後執行,點擊 b2 與 b4 結果如下

| 中英文術語對照 | |
|---|---|
| 類別 | class |
| 方法 | method |
| 回傳值 | return value |
| 型態 | type |
| 物件 | object |
| 參數 | parameter |
您可以繼續參考
AWT 元件
相關目錄
Java API 分類導覽
Java 教材
首頁
參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Container.html
沒有留言:
張貼留言