一、java事件模型:包含事件源、事件监听器、事件处理程序。
1、事件源:发生事件的控件。
2、事件侦听器:监测事件是否发生。都是接口。包含:
3、事件处理程序:响应用户的操作,是生成事件时事件侦听器调用的方法。都是抽象方法。
4、事件类:表示所有事件的特征,在触发事件时,自动创建。包含:
1)ActionEvent:JButton,JCheckBox,JComboBox,JRadioButton都可以触发,该类包含的方法:
实现代码可参考:
//在界面的类直接实现事件监听程序,监听界面上所有的侦听ActionEvent
public class FirstPage extends JFrame implements ActionListener,MouseMotionListener{
JPanel jp;
JButton btn1,btn2;
public FirstPage(){
setVisible(true);
setSize(400,400);
jp=new JPanel();
btn1=new JButton("按钮1");
btn2=new JButton("按钮2");
//邦定事件处理程序,this代表自己的类
btn1.addActionListener(this);
btn2.addActionListener(this);
addMouseMotionListener(this);
add(jp);
jp.add(btn1);
jp.add(btn2);
}
public static void main(String[] args) {
new FirstPage();
}
//实现了监听的接口,就必须实现接口里面所有的抽象方法
@Override
public void actionPerformed(ActionEvent e) {
// ActionEvent 事件类 可以获取事件源
if(e.getSource().equals(btn1)){//getSource()返回事件源,判断事件源是否为btn1
System.out.println(e.getWhen());//打印事件发生的事件
System.out.println(e.getActionCommand());//返回事件源的text
System.out.println(e.getID());//返回事件类型的id
}
if(e.getSource().equals(btn2)){//getSource()返回事件源,判断事件源是否为btn2
}
}
//拖动鼠标
@Override
public void mouseDragged(MouseEvent e) {
}
//移动鼠标
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("鼠标的横坐标"+e.getX());
System.out.println("鼠标的纵坐标"+e.getY());
}
}
2)KeyEvent:键盘事件,输入控件。用法可参考代码:
public class KeyEeventDemo extends JFrame implements KeyListener{
JPanel jp;
JTextArea jta;
JButton btn1;
public KeyEeventDemo(){
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp=new JPanel();
jp.setLayout(null);
add(jp);
jta=new JTextArea(10,10);
jta.setBounds(50,100,100,100);
jta.addKeyListener(this);
jp.add(jta);
btn1=new JButton("hour");
btn1.setBounds(150,150,100,20);
jp.add(btn1);
}
public static void main(String[] args) {
new KeyEeventDemo();
}
@Override
public void keyTyped(KeyEvent e) {
// 敲击键盘
int speedY=20;
//JOptionPane.showMessageDialog(this, e.getKeyChar());//返回按键上的字符
if(e.getKeyChar()=='w')
{
//修改y值
btn1.setBounds(150,btn1.getY()+speedY,100,20);
}
}
@Override
public void keyPressed(KeyEvent e) {
// 摁下键盘
}
@Override
public void keyReleased(KeyEvent e) {
// 弹起键盘
}
}
3)MouseEvent:鼠标事件:鼠标单击/双击/滚动速表。4)FocusEvent:获得/失去焦点
5)ItemEvent:触发选项的事件,针对单选和复选框选中或取消时触发。用法可参考代码:
public class ItemLister extends JFrame implements ItemListener{
JPanel jp;
JCheckBox c1,c2,c3;
JLabel lb1;
public ItemLister(){
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp=new JPanel();
c1=new JCheckBox("取消显示");
c2=new JCheckBox("修改");
c3=new JCheckBox("删除");
lb1=new JLabel("隐藏显示");
//绑带事件
c1.addItemListener(this);
c2.addItemListener(this);
jp.add(c1);
jp.add(c2);
jp.add(c3);
jp.add(lb1);
add(jp);
}
public static void main(String[] args) {
new ItemLister();
}
//选项的状态发生变化时触发
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource().equals(c1)){
lb1.setVisible(false);;
}
}
/* @Override
public void itemStateChanged(ItemEvent e) {
// 获取选中或取消的的这个按钮
JOptionPane.showMessageDialog(this, e.getItem());//返回选项
JOptionPane.showMessageDialog(this, e.getSource());//返回触发的控件
}*/
}
6)WindowEvent:窗体事件。用法参考代码:
public class WindowsListenerDemo extends JFrame implements WindowListener{
public WindowsListenerDemo()
{
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener(this);
}
public static void main(String[] args) {
new WindowsListenerDemo();
}
@Override
public void windowOpened(WindowEvent e) {
JOptionPane.showMessageDialog(this, "界面初始化完毕");
}
@Override
public void windowClosing(WindowEvent e) {
int choice=JOptionPane.showConfirmDialog(this, "确定关闭?");
if(choice==0){
this.dispose();//关闭当前界面
}else{
JOptionPane.showMessageDialog(this, "5秒之后关闭界面");
try{
Thread.sleep(5000);//暂停5秒
}catch(InterruptedException e1){
e1.printStackTrace();
}
}
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
二、实现事件。实现事件处理2种方式:
1、第一种:实现侦听Listener接口,步骤如下:
1)确定事件源
1)根据事件的类型选择要实现的侦听接口。implements接口
3)根据不同的事件,实现对应的事件处理。实现接口里面的抽象方法
4)绑定事件处理程序。
2、第二种:实现适配器=事件源自带事件侦听,自带事件处理程序,是一个类,已经实现了事件监听接口的抽象方法,可以用继承方式实现。
用法可参考代码:
public class AdapterDemo extends JFrame{
JPanel jp;
JButton btn1,btn2;
public AdapterDemo(){
setVisible(true);
setSize(400,400);
jp=new JPanel();
btn1=new JButton("按钮1");
btn2=new JButton("按钮2");
addKeyListener(new KeyAdapter(){
});
add(jp);
jp.add(btn1);
jp.add(btn2);
}
public static void main(String[] args) {
new AdapterDemo();
}
}
class MyAdapter extends MouseAdapter{
}