定义ActionListener接口的实现类实现事件监听
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Test04 {
public static void main(String[] args) {
Frame frame=new Frame("我的窗体");
Button btn=new Button("点我");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
System.out.println(cmd);
Frame frm=new Frame("俺是被点击才弹出滴");
frm.setSize(100,100);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
});
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("***windowOpened");
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("***windowClosing");
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("***windowActivated");
}
});
frame.add(btn);
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class ButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
System.out.println(cmd);
Frame frm=new Frame("俺是被点击才弹出滴");
frm.setSize(100,100);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
}
为一个组件绑定多个监听器
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
public class Test05 {
Frame frame = new Frame("我的窗体");
Button btn = new Button("点我");
TextField txt=new TextField(10);
int count = 0;
public Test05(){
init();
}
public void init() {
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("***actionPerformed");
count++;
btn.setLabel(count+"");
}
});
btn.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("****mouseClicked");
}
});
txt.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
System.out.println("***focusLost");
TextField txtSource=(TextField) e.getSource();
txtSource.setBackground(Color.white);
}
@Override
public void focusGained(FocusEvent e) {
System.out.println("***focusGained");
TextField txtSource=(TextField) e.getSource();
txtSource.setBackground(Color.gray);
}
});
txt.addTextListener(new TextListener() {
@Override
public void textValueChanged(TextEvent e) {
System.out.println("文本框被更改"+e.paramString());
}
});
txt.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode()+" "+e.getKeyChar());
if(e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("哈哈,你按了回车,被发现了!");
}
}
});
frame.add("North",txt);
frame.add(btn);
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new Test05();
}
}
监听器实现方式
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test06 extends Frame implements ActionListener{
Button btn=new Button("点我");
public Test06(String title){
super(title);
init();
}
public void init(){
btn.addActionListener(new MyHandler2());
add(btn);
setSize(200,200);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("使用容器类方式");
}
class MyHandler2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("使用外部类方式");
}
}
public static void main(String[] args) {
new Test06("窗体");
}
}
class MyHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("使用外部类方式");
}
}