本文概述
- Java事件类和侦听器接口
- 执行事件处理的步骤
- 报名方法
更改对象的状态称为事件。例如, 单击按钮, 拖动鼠标等。java.awt.event包提供了许多事件类和用于事件处理的Listener接口。 |
活动类 | 侦听器接口 |
---|---|
ActionEvent | ActionListener |
MouseEvent | MouseListener和MouseMotionListener |
MouseWheelEvent | MouseWheelListener |
KeyEvent | KeyListener |
ItemEvent | ItemListener |
TextEvent | TextListener |
AdjustmentEvent | AdjustmentListener |
WindowEvent | WindowListener |
ComponentEvent | ComponentListener |
ContainerEvent | ContainerListener |
FocusEvent | FocusListener |
- 向侦听器注册组件
- 按钮public void addActionListener(ActionListener a){}
- MenuItem公共无效addActionListener(ActionListener a){}
- TextField public void addActionListener(ActionListener a){} public void addTextListener(TextListener a){}
- TextArea公共无效addTextListener(TextListener a){}
- 复选框public void addItemListener(ItemListener a){}
- 选择public void addItemListener(ItemListener a){}
- 列出public void addActionListener(ActionListener a){} public void addItemListener(ItemListener a){}
我们可以将事件处理代码放入以下位置之一:
- 班内
- 其他班
- 匿名班
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){//create components
tf=new TextField();
tf.setBounds(60, 50, 170, 20);
Button b=new Button("click me");
b.setBounds(100, 120, 80, 30);
//register listener
b.addActionListener(this);
//passing current instance//add components and set size, layout and visibility
add(b);
add(tf);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}
public void setBounds(int xaxis, int yaxis, int width, int height); 在上面的示例中使用了, 用于设置组件的位置, 它可以是按钮, 文本字段等。
文章图片
2)通过外部类处理Java事件
import java.awt.*;
import java.awt.event.*;
class AEvent2 extends Frame{
TextField tf;
AEvent2(){
//create components
tf=new TextField();
tf.setBounds(60, 50, 170, 20);
Button b=new Button("click me");
b.setBounds(100, 120, 80, 30);
//register listener
Outer o=new Outer(this);
b.addActionListener(o);
//passing outer class instance
//add components and set size, layout and visibility
add(b);
add(tf);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent2();
}
}
import java.awt.event.*;
class Outer implements ActionListener{
AEvent2 obj;
Outer(AEvent2 obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent e){
obj.tf.setText("welcome");
}
}
3)通过匿名类处理Java事件
import java.awt.*;
import java.awt.event.*;
class AEvent3 extends Frame{
TextField tf;
AEvent3(){
tf=new TextField();
tf.setBounds(60, 50, 170, 20);
Button b=new Button("click me");
b.setBounds(50, 120, 80, 30);
b.addActionListener(new ActionListener(){
public void actionPerformed(){
tf.setText("hello");
}
});
add(b);
add(tf);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent3();
}
}
推荐阅读
- Java ActionListener接口
- Java ZoneOffset类
- Java ZoneId类
- Java ZonedDateTime类
- Java YearMonth类
- Java年份
- Java TimeZone类
- 安装系统 硬盘重装系统图文详细教程
- ghost版win7旗舰版安装办法