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();
}
}
我们知道,当我们创建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();
}
}
相关文章推荐
- core java 8~9(GUI & AWT事件处理机制)
- Java学习笔记(AWT 之 事件处理机制)
- core java 8~9(GUI & AWT事件处理机制)
- AWT 处理事件机制概要
- AWT事件处理机制
- AWT事件处理机制学习
- AWT事件处理机制学习
- 分析源码,探究AWT事件处理机制
- Awt事件处理机制——太原维信科技提供http://www.bjweixin.com/
- iOS 事件处理机制与图像渲染过程
- iOS 事件处理机制与图像渲染过程(转)
- Java中的事件处理与异常处理机制-解析
- Qt的事件处理机制分析
- 分析MIDP低级事件处理机制
- cocos2d-x Touch事件处理机制
- JAVA委托事件处理机制
- java事件处理机制(自定义事件)j
- 从源码角度分析android事件分发处理机制
- 事件处理机制之Gestures(手势)
- javascript事件处理机制---事件监听、捕获和冒泡