1.实现事件监听,总的来讲就是有三个步骤
1)事件源注册事件监听器 -- 事件源.addXXXListener( obj ) ,obj称为监听者
2 ) 实现监听器接口 --obj所属的类实现XXXListener接口
3)实现监听器接口中的抽象方法 --在obj所属的类中写监听器接口中的抽象方法
2.具体来讲有三个方法:
1)监听注册,实现接口,实现接口的方法
public class MyFrame1 extends Frame implements ActionListener{
private static final long serialVersionUID = 1L;
private Label lb1;
private Label lb2;
private TextField tf1;
private TextField tf2;
private Button btn1;
private Button btn2;
public MyFrame1(){
super("事件监听的三种方式:1");
setBounds(200,50,170,200);
setLayout( new FlowLayout() );
lb1 = new Label("name");
lb2 = new Label("psw");
tf1 = new TextField(15);
tf2 = new TextField(15);
btn1 = new Button("OK");
btn2 = new Button("exit");
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(btn1);
add(btn2);
//监听
btn1.addActionListener(this);
btn2.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//方法1
/*
if(e.getSource() == btn1){
JOptionPane.showMessageDialog(this, "登录!");
}else if(e.getSource() == btn2){
System.exit(0);
}
*/
//方法2
if(e.getActionCommand().equals("OK")){
JOptionPane.showMessageDialog(this, "登录!");
}else if (e.getActionCommand().equals("exit")) {
System.exit(0);
}
}
public static void main(String[] args) {
new MyFrame1();
}
}
2)使用匿名内部类,方法类似
public class MyFrame2 extends Frame{
private static final long serialVersionUID = 1L;
private Label lb1;
private Label lb2;
private TextField tf1;
private TextField tf2;
private Button btn1;
private Button btn2;
public MyFrame2(){
super("事件监听的三种方式:2");
setBounds(200,50,170,200);
setLayout( new FlowLayout() );
lb1 = new Label("tf1");
lb2 = new Label("tf2");
tf1 = new TextField(15);
tf2 = new TextField(15);
btn1 = new Button("move");
btn2 = new Button("exit");
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(btn1);
add(btn2);
//监听
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String string = tf1.getText();
tf2.setText(string);
tf1.setText("");
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
new MyFrame2();
}
}
3)使用内部类
public class MyFrame3 extends Frame{
private static final long serialVersionUID = 1L;
private Label lb1;
private Label lb2;
private TextField tf1;
private TextField tf2;
private Button btn1;
private Button btn2;
public MyFrame3(){
super("事件监听的三种方式:3");
setBounds(200,50,170,200);
setLayout( new FlowLayout() );
lb1 = new Label("tf1");
lb2 = new Label("tf2");
tf1 = new TextField(15);
tf2 = new TextField(15);
btn1 = new Button("move");
btn2 = new Button("exit");
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(btn1);
add(btn2);
//监听
btn1.addActionListener(new MyAction());
btn2.addActionListener(new MyAction());
setVisible(true);
}
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn1){
String string = tf1.getText();
tf2.setText(string);
tf1.setText("");
}else if(e.getSource() == btn2){
System.exit(0);
}
}
}
public static void main(String[] args) {
new MyFrame3();
}
}