事件:程序对某些操作的响应
例子:点击按钮,打印“Hello”
Step1:事件响应类必须能够听到相应的命令
点击按钮如果要响应,必须让响应类实现java.awt.event.ActionListener 接口
Step2:将事件响应的代码,放在接口中重写的函数内
Step3:绑定,确保按钮发出命令,响应对象能够执行
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Printer implements ActionListener{ //①实现接口public void actionPerformed(ActionEvent e){ //②事件响应代码System.out.println("Hello");}
}class EventTest1 extends JFrame{private JButton jbt = new JButton("按钮");public EventTest1(){Printer p = new Printer(); jbt.addActionListener(p); //③绑定,确保按钮发出命令,p能够执行this.add(jbt,BorderLayout.NORTH);this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new EventTest1();}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class EventTest2 extends JFrame implements ActionListener{private JButton jbt = new JButton("按钮");public EventTest2(){jbt.addActionListener(this); this.add(jbt,BorderLayout.NORTH);this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void actionPerformed(ActionEvent e){ System.out.println("Hello");}public static void main(String[] args) {new EventTest2();}
}
actionPerformed:我们可以看到第一段代码额外构建了一个函数Printer(),而第二个代码则运用用到了actionPerformed()
思考问题
问题1:点击按钮,需要响应类实现ActionListener,这是谁规定的?
答:Java中定义,不同的事件,由不同的XXXListener来监听。
问题2:列出什么样的事件,由什么样的Listener监听?
答案:后面再讲
问题3:事件响应函数中,ActionEvent参数是什么?
答案:表示命令发出时,封装的命令发出方的信息。
例:两个按钮,一个按钮点击,界面变红,另一个点击,界面变蓝
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class EventTest3 extends JFrame implements ActionListener{private JButton jbt1 = new JButton("变红"); private JButton jbt2 = new JButton("变蓝");private JPanel jpl = new JPanel();public EventTest3(){this.add(jbt1,BorderLayout.NORTH);this.add(jbt2,BorderLayout.SOUTH); this.add(jpl,BorderLayout.CENTER); jbt1.addActionListener(this); jbt2.addActionListener(this); this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void actionPerformed(ActionEvent e){ if(e.getSource()==jbt1){jpl.setBackground(Color.red);}else{jpl.setBackground(Color.blue); }}public static void main(String[] args) {new EventTest3();}
}
——————事件处理讲解完毕——————
①实现接口(监听器)②编写函数 ③绑定
这叫做:事件监听机制
什么样的事件,由什么样的Listener监听?
ActionListener(非常重要)
监听按钮点击,文本框内回车,菜单单击、其他支持单击响应的控件,以及一些拥有addActionListener函数的控件
ItemListener(比较重要)
监听选项变化时要响应的事件,如下拉菜单等
下拉菜单中有红绿蓝三个选项,选择时,界面自动变色
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class EventTest4 extends JFrame implements ItemListener {private JComboBox jcb = new JComboBox(); private JPanel jpl = new JPanel();public void itemStateChanged(ItemEvent e){if(jcb.getSelectedItem().equals("红")){jpl.setBackground(Color.red);}else if(jcb.getSelectedItem().equals("绿")){jpl.setBackground(Color.green); }else{jpl.setBackground(Color.blue); }}public EventTest4(){this.add(jcb,BorderLayout.NORTH); this.add(jpl,BorderLayout.CENTER); jcb.addItemListener(this); jcb.addItem("红"); jcb.addItem("绿"); jcb.addItem("蓝"); this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new EventTest4();}
}
MouseListener:监听鼠标操作(单击,双击,进入,离开等)(比较重要)
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
例1:鼠标进入按钮,按钮变红,退出,按钮变白
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class EventTest5 extends JFrame implements MouseListener{private JButton jbt = new JButton("按钮");public EventTest5(){jbt.addMouseListener(this); this.add(jbt,BorderLayout.NORTH);this.setSize(300,400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {jbt.setBackground(Color.red);}public void mouseExited(MouseEvent e) {jbt.setBackground(Color.white);}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public static void main(String[] args) {new EventTest5();}
}
例2:界面上有一个图片,鼠标进入,图片随机躲开
import javax.swing.*;
import java.awt.event.*;class EventTest6 extends JFrame implements MouseListener{private JLabel jlb = new JLabel();int X=50,Y=50;public EventTest6(){Icon icon = new ImageIcon("img.jpg");jlb.setIcon(icon);this.setLayout(null); this.add(jlb);jlb.setSize(icon.getIconWidth(),icon.getIconHeight());jlb.setLocation(X,Y);jlb.addMouseListener(this); this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {X = (int)(Math.random()*this.getWidth())-jlb.getWidth();Y = (int)(Math.random()*this.getHeight())-jlb.getHeight();jlb.setLocation(X,Y);}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public static void main(String[] args) {new EventTest6();}
}
MouseMotionListener(比较重要)
监听鼠标移动和拖动操作
例:鼠标在界面上移动,标题栏显示鼠标的当前坐标
import javax.swing.*;
import java.awt.event.*;class EventTest7 extends JFrame implements MouseMotionListener{public EventTest7(){this.addMouseMotionListener(this);this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void mouseDragged(MouseEvent e) {String str = e.getX() + "," + e.getY();this.setTitle(str);}public void mouseMoved(MouseEvent e) {}public static void main(String[] args) {new EventTest7();}
}
作业
作业:界面上有一个图片,鼠标可以将图片从一个地方拖动到另一个地方
作业:界面上有10个图片,鼠标可以将某个图片从一个地方拖动到另一个地方
KeyListener(比较重要)
监听键盘输入时要响应的事件(如俄罗斯方块)
例:使用键盘上的上下左右键,能控制界面上一个图片的上下左右移动
import javax.swing.*;
import java.awt.event.*;class EventTest8 extends JFrame implements KeyListener{private JLabel jlb = new JLabel();int X=50,Y=50;public EventTest8(){Icon icon = new ImageIcon("img.jpg");jlb.setIcon(icon);this.setLayout(null); this.add(jlb);jlb.setSize(icon.getIconWidth(),icon.getIconHeight());jlb.setLocation(X,Y);this.addKeyListener(this);this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);} public void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode();if(keyCode==KeyEvent.VK_UP){Y-=30;}else if(keyCode==KeyEvent.VK_DOWN){Y+=30;}else if(keyCode==KeyEvent.VK_LEFT){X-=30;}else if(keyCode==KeyEvent.VK_RIGHT){X+=30;}jlb.setLocation(X,Y);} public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {}public static void main(String[] args) {new EventTest8();}
}
注意:键盘事件一般被更大的容器先截获。
综合案例:界面一个图片掉下,鼠标进入,暂停,离开,继续掉
import javax.swing.*;
import java.awt.event.*;class EventTest9 extends JFrame implements Runnable,MouseListener {private JLabel jlb = new JLabel();int X=50,Y=50;boolean RUN = true;public EventTest9(){Icon icon = new ImageIcon("img.jpg");jlb.setIcon(icon);this.setLayout(null); this.add(jlb);jlb.setSize(icon.getIconWidth(),icon.getIconHeight());jlb.setLocation(X,Y);this.addMouseListener(this);this.setSize(400,600);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);new Thread(this).start();} public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {RUN = false;}public void mouseExited(MouseEvent e) {RUN = true;new Thread(this).start();}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {} public void run(){while(RUN){try{Thread.sleep(100); }catch(Exception e){}Y+=20; jlb.setLocation(X,Y);}}public static void main(String[] args) {new EventTest9();}
}