您的位置:首页 > 其它

Awt事件处理机制

2013-07-22 22:40 399 查看
Awt事件处理机制:

我们知道,当我们创建frame的时候,出来的窗口右上角上的叉号,我们关闭的时候没有任何响应。这是因为,在awt中,所有的事件都是有特定的对象完成,窗口或者组件没有处理事件的功能。

当我们点击关闭时,触发了一个事件,这个事件或触发相应的事件监听,事件监听器调用相应的方法对事件做处理。

Eg:

关闭窗口

public class ButTest extends Frame{

public static void main(String[] args) {

ButTest b = new ButTest();

b.setSize(300,300);

b.setVisible(true);

b.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

}

public class ButTest extends Frame{

Button b = new Button("Click ");

TextField t = new TextField("",20);

int clickCount = 0;

public ButTest(){

super("事件");

setLayout(new FlowLayout());

setSize(200,200);

add(t);

add(b);

setVisible(true);

b.addActionListener(new ButtonListener());

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

clickCount++;

t.setText("count"+clickCount);

}

}

public static void main(String[] args) {

ButTest b = new ButTest();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: