目录
5.5.1.ActionEvent 与 ActionListener
5.5.2.ItemEvent 与 ItemListener
5.5.4.MouseEvent 与 MouseListener/MouseMotionListener
5.5.5.ListSelectionEvent与ListSelectionListener
5.5.6.ChangeEvent 与 ChangeListener
5.5.7.FocusEvent 与 FocusListener
事件:Java语言将每一个键盘或鼠标的操作定义为一个“事件”。

事件处理的基本思路如下:

| 事件名称 | 监听器 | 主要用途 |
| WindowEvent | WindowListener | 窗口发生变化,如关闭 |
| ActionEvent | ActionListener | 产生动作,如单击按钮 |
| ItemEvent | ItemListener | 项目变化,如复选框 |
| ListSelectionEvent | ListSelectionListener | 选择列表中的项目时 |
| ChangeEvent | ChangeListener | 状态改变,如进度条 |
| FocusEvent | FocusListener | 焦点获得或失去 |
| MouseEvent | MouseListener | 鼠标点击、进入或离开 |
| MouseEvent | MouseMotionListener | 鼠标拖动或移动 |
| KeyEvent | KeyListener | 按键产生时 |
| MenuEvent | MenuListener | 菜单选择 |
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- public class MainClass{
- public static void main(String[] args){
- new ActionListener_1();
- }
- }
-
- class ActionListener_1 extends JFrame{
- private JPanel panel = new JPanel();
- private JButton btn1 = new JButton("粉色");
- private JButton btn2 = new JButton("黄色");
- private JButton btn3 = new JButton("绿色");
- public ActionListener_1(){
- super("ActionListener");
- btn1.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- panel.setBackground(Color.pink); // 设置面板背景颜色
- }
- });
- btn2.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- panel.setBackground(Color.yellow);
- }
- });
- btn3.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- panel.setBackground(Color.green);
- }
- });
- panel.add(btn1);
- panel.add(btn2);
- panel.add(btn3);
- this.add(panel);
- this.setSize(300,300);
- this.setLocation(200,250);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }


- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
-
- public class MainClass {
- public static void main(String[] args){
- MyJFrame mf = new MyJFrame();
- }
- }
- class MyJFrame extends JFrame {
- private JPanel panel;
- private JButton button;
- private JRadioButton btnPink,btnOrag,btnGren;
- private ButtonGroup buttonGroup;
- public MyJFrame(){
- super("itemListener");
- button = new JButton("请为我选择一种背景颜色");
- panel = new JPanel();
- btnPink = new JRadioButton("粉色");
- btnOrag = new JRadioButton("橙色");
- btnGren = new JRadioButton("青色");
- buttonGroup = new ButtonGroup();
- btnPink.addItemListener(new ItemListener() {
- @Override
- public void itemStateChanged(ItemEvent e) {
- button.setBackground(Color.PINK);
- button.setText("你的背景色变为了粉色");
- }
- });
- btnOrag.addItemListener(new ItemListener() {
- @Override
- public void itemStateChanged(ItemEvent e) {
- button.setBackground(Color.ORANGE);
- button.setText("你的背景色变为了橙色");
- }
- });
- btnGren.addItemListener(new ItemListener() {
- @Override
- public void itemStateChanged(ItemEvent e) {
- button.setBackground(Color.cyan);
- button.setText("你的背景色变为了青色");
- }
- });
- buttonGroup.add(btnPink);
- buttonGroup.add(btnOrag);
- buttonGroup.add(btnGren);
- panel.add(btnPink);
- panel.add(btnOrag);
- panel.add(btnGren);
- this.add(button);
- this.add(panel, BorderLayout.SOUTH);
- this.setLocation(100,200);
- this.setSize(400,300);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }
-


当组件上发生击键时产生该事件
KeyListener接口中的方法:
KeyEvent中的常用方法:
注:KeyEvent中定义了表示键的常量,如 VK_1
- import javax.swing.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
-
- public class MainClass{
- public static void main(String[] args) {
- new KeyListener_2();
- }
- }
- class KeyListener_2 extends JFrame{
- private JPanel panel = new JPanel();
- private JButton button = new JButton("走动");
- public KeyListener_2(){
- super("KeyListener");
- button.addKeyListener(new KeyListener(){
- public void keyPressed(KeyEvent e) {
- int keyCode = e.getKeyCode();
- int x = button.getX();
- int y = button.getY();
- if(keyCode == KeyEvent.VK_RIGHT){
- button.setLocation(x+20,y);
- }else if(keyCode == KeyEvent.VK_LEFT){
- button.setLocation(x-20,y);
- }else if(keyCode == KeyEvent.VK_UP){
- button.setLocation(x,y-20);
- }else if(keyCode == KeyEvent.VK_DOWN){
- button.setLocation(x,y+20);
- }
- }
- public void keyReleased(KeyEvent e) {
-
- }
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
-
- });
- panel.add(button);
- add(panel);
- setLocation(300,250);
- setSize(300,300);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- }


5.5.4.1MouseListener
当在组件上进行鼠标基本操作时产生该事件
MouseListener接口中的方法:
MouseEvent中的常用方法:
5.5.4.2.MouseMotionListener
当在组件上进行鼠标拖动或移动时产生该事件
MouseMotionListener接口中的方法:
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
-
- public class MainClass{
- public static void main(String[] args) {
- new MouseEvent_1();
- }
- }
- class MouseEvent_1 extends JFrame {
- private JPanel panel;
- // 鼠标上一次的坐标
- int pre_x = -1;
- int pre_y = -1;
- // 鼠标当前坐标
- int x;
- int y;
- public MouseEvent_1(){
- super("MouseListener——MouseMotionListener");
- panel = new JPanel();
- panel.addMouseMotionListener(new MouseMotionListener() {
- // 鼠标拖拽事件
- public void mouseDragged(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- // 重画,调用repaint方法触发paint
- MouseEvent_1.this.repaint();
- }
-
- // 鼠标移动事件
- public void mouseMoved(MouseEvent e) {
-
- }
- });
-
-
-
- panel.addMouseListener(new MouseListener() {
- // 鼠标点击事件
- public void mouseClicked(MouseEvent e) {
-
- }
-
- // 鼠标按下事件
- public void mousePressed(MouseEvent e) {
- // 如果是右键
- if(e.getButton() == MouseEvent.BUTTON3){
- // 擦除原来的轨迹
- MouseEvent_1.this.panel.repaint();
- }
- }
-
- // 鼠标松开事件
- public void mouseReleased(MouseEvent e) {
- // 松开后恢复历史坐标
- pre_x = -1;
- pre_y = -1;
- }
-
- // 鼠标移入事件
- public void mouseEntered(MouseEvent e) {
-
- }
-
- // 鼠标移出事件
- public void mouseExited(MouseEvent e) {
-
- }
- });
- add(panel);
- setSize(500,350);
- setLocation(350,270);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
-
- public void paint(Graphics graphics){
- // 设置画笔的颜色
- graphics.setColor(Color.red);
-
- // 历史坐标 > 0
- if(pre_x > 0 && pre_y > 0){
- // 开始画一条线
- graphics.drawLine(pre_x,pre_y,x,y);
- }
-
- // 保存当前鼠标坐标,作为上一次的鼠标坐标
- pre_x = x;
- pre_y = y;
- }
- }

当组件获得或失去输入焦点时产生该事件
FocusListener接口中的方法:
void focusGained(FocusEvent);
void focusLost(FocusEvent);
FocusEvent中的常用方法:
Component getOppositeComponent();
只实现接口所需要处理的方法——通过覆盖
对于接口中的其它方法: 系统会提供默认的方法(方法体为空)
事件适配器类与事件监听器接口的区别?

-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
-
- public class MainClass{
- public static void main(String[] args) {
- new MouseEvent_2();
- }
- }
- class MouseEvent_2 extends JFrame {
- private JPanel panel;
- int pre_x = -1;
- int pre_y = -1;
- int x;
- int y;
- public MouseEvent_2(){
- super("MouseAdapter——MouseMotionAdapter");
- panel = new JPanel();
-
- panel.addMouseMotionListener(new MouseMotionAdapter() {
- // 只需实现需处理的方法
- public void mouseDragged(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- MouseEvent_2.this.repaint();
- }
- });
-
-
- panel.addMouseListener(new MouseAdapter() {
- // 只需实现需处理的方法
- public void mousePressed(MouseEvent e){
- if(e.getButton() == MouseEvent.BUTTON3){
- MouseEvent_2.this.panel.repaint();
- }
- }
- public void mouseReleased(MouseEvent e){
- pre_x = -1;
- pre_y = -1;
- }
- });
- add(panel);
- setSize(300,400);
- setLocation(300,250);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public void paint(Graphics graphics){
- graphics.setColor(Color.red);
- if(pre_x>0 && pre_y>0){
- graphics.drawLine(pre_x,pre_y,x,y);
- }
- pre_x = x;
- pre_y = y;
- }
- }

(27条消息) Swing UI——容器(一)_Stuttering Guy的博客-CSDN博客
https://blog.csdn.net/Mr_Morgans/article/details/125109643?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125109643%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=l9JMT(27条消息) Swing UI——基本组件(二)_Stuttering Guy的博客-CSDN博客
https://blog.csdn.net/Mr_Morgans/article/details/125110881?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125110881%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=Kzbcx(27条消息) Swing UI——高级组件(三)_Stuttering Guy的博客-CSDN博客
https://blog.csdn.net/Mr_Morgans/article/details/125115383?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125115383%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=81Bbq(27条消息) Swing UI——布局管理器(四)_Stuttering Guy的博客-CSDN博客
https://blog.csdn.net/Mr_Morgans/article/details/125115409?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125115409%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=GZyFf