监听器:
>事件源
>事件
>监听器:监听事件源,等待事件发生,执行方法
import javax.swing.JButton;
import javax.swing.JFrame;
public class Demo1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 200);
frame.setLocation(200, 200);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn = new JButton("确定");
frame.add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("hello");
}
});
frame.setVisible(true);
}
}
btn是事件源,evt是事件,ActionListener是监听器,ActionListener监听btn,当evt发生,执行actionPerfomed方法进行处理。