| componentAdded | コンポーネントが追加された際に発生する |
|---|---|
| componentRemoved | コンポーネントが削除された際に発生する |
ContainerAdapterクラス:ContainerListenerインターフェースを実装済みのクラスです。
以下のコードはcomponentAddedイベントとcomponentRemovedイベントの使用例です。
package MyPackage1;
import java.awt.*;
import java.awt.event.*;
public class ContainerEventSample {
/*
* エントリーポイント
*/
public static void main(String args[]){
new ContainerEventSample();
}
Frame frm;
Button btn1;
TextField txt1;
Label lbl1;
/*
* コンストラクタ
*/
public ContainerEventSample(){
frm =new Frame("ContainerEventSample");
frm.setSize(300,300);
frm.setLayout(new GridLayout(3,1));
frm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
frm.addContainerListener(new MyContainerAdapter());
lbl1 = new Label();
frm.add(lbl1);
btn1= new Button("add");
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (txt1 == null ){
txt1 = new TextField();
txt1.setBackground(Color.blue);
}
if (txt1.getParent() == null){
frm.add(txt1);
btn1.setLabel("remove");
}else {
frm.remove(txt1);
btn1.setLabel("add");
}
frm.validate();
}
});
frm.add(btn1);
frm.setVisible(true);
}
private class MyContainerAdapter extends ContainerAdapter {
public void componentAdded(ContainerEvent e) {
lbl1.setText("componentAdded");
}
public void componentRemoved(ContainerEvent e) {
lbl1.setText("componentRemoved");
}
}
}
0 件のコメント:
コメントを投稿