Java~事件处理机制(窗口事件、鼠标事件、键盘事件、动作事件)实现抽奖小界面

事件处理机制专门用于响应用户的操作,比如,想要响应用户的单击鼠标、按下键盘等操作,就需要使用AWT的事件处理机制。在学习如何使用AWT事件处理机制之前,首先向读者介绍几个比较重要的概念,具体如下所示

  • 事件对象(Event):封装了GUI组件上发生的特定事件(通常就是用户的一次操作)。
  • 事件源(组件):事件发生的场所,通常就是产生事件的组件。
  • 监听器(Listener):负责监听事件源上发生的事件,并对各种事件做出相应处理的对象(对象中包含事件处理器)。
  • 事件处理器:监听器对象对接收的事件对象进行相应处理的方法。

它们彼此之间有着非常紧密的联系。接下来用一个图例来描述事件处理的工作流程,如图所示。
在这里插入图片描述
事件适配器:
在程序中,如果想实现事件的监听机制,首先需要定义一个实现了事件监听器接口的类,例如Window类型的窗口需要实现WindowListener。接着通过addWindowListener()方法为事件源注册事件监听器对象,当事件源上发生事件时,便会触发事件监听器对象,由事件监听器调用相应的方法来处理相应的事件
实现WindowListener接口后,需要实现接口中定义的7个方法,然而在程序中需要用到的只有windowClosing()一个方法,其他六个方法都是空实现,没有发挥任何作用,这样代码的编写明显是一种多余但又必需的工作。针对这样的问题,JDK提供了一些适配器类,它们是监听器接口的默认实现类,这些实现类中实现了接口的所有方法,但方法中没有任何代码,程序可以通过继承适配器类来达到实现监听器接口的目的。
但在实际开发中,为了代码的简洁,经常通过匿名内部类来创建事件的监听器对象,针对所发生的事件进行处理

窗口事件:
大部分GUI应用程序都需要使用Window窗体对象作为最外层的容器,可以说窗体对象是所有GUI应用程序的基础,应用程序中通常都是将其他组件直接或者间接地置于窗体中。
当对窗体进行操作时,比如窗体的打开、关闭、激活、停用等,这些动作都属于窗体事件,JDK中提供了一个类WindowEvent用于表示这些窗体事件。在应用程序中,当对窗体事件进行处理时,首先需要定义一个实现了WindowListener接口的类作为窗体监听器,然后通过addWindowListener()方法将窗体对象与窗体监听器绑定。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class WindowEventTest extends JFrame implements WindowListener
{

	
	public WindowEventTest()
	{
		this.setSize(300, 300);
		this.setTitle("窗体事件测试");
		this.addWindowListener(this);
		this.setVisible(true);
	}

	@Override
	public void windowActivated(WindowEvent e) {
		System.out.println("窗体获得焦点");	
	}

	@Override
	public void windowClosed(WindowEvent e) {
		System.out.println("窗体已经关闭");			
	}

	@Override
	public void windowClosing(WindowEvent e) {
		System.out.println("窗体正在关闭");	
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		System.out.println("窗体失去焦点");	
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		System.out.println("窗体最大化");		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		System.out.println("窗体最小化");	
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		System.out.println("窗体已经打开");	
	}
	
	
}

WindowAdapter

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class WindowEventTest1 extends JFrame
{
	public WindowEventTest1()
	{
		this.setSize(300, 300);
		this.setTitle("窗体事件测试");
		this.addWindowListener(new WindowDeal());
		this.setVisible(true);
	}
}

class WindowDeal extends WindowAdapter {	
	public void windowIconified(WindowEvent e) 
	{
		System.out.println("窗体图标化");
	}
}

鼠标事件:
在图形用户界面中,用户会经常使用鼠标来进行选择、切换界面等操作,这些操作被定义为鼠标事件,其中包括鼠标按下、鼠标松开、鼠标单击等。JDK中提供了一个MouseEvent类用于表示鼠标事件,几乎所有的组件都可以产生鼠标事件。处理鼠标事件时,首先需要通过实现MouseListener接口定义监听器(也可以通过继承适配器MouseAdapter类来实现),然后调用addMouseListener()方法将监听器绑定到事件源对象。

class BeginBtMouseDeal extends MouseAdapter {
    private LottoGames event;

    public BeginBtMouseDeal(LottoGames event) {
        this.event = event;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getClickCount() == 2) {
            event.beginBt.setText("奖励都假的");
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        event.beginBt.setText("双击发现秘密");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        event.beginBt.setText("欢迎光临");
    }
}

键盘事件
键盘操作也是最常用的用户交互方式,例如键盘按下、释放等,这些操作被定义为键盘事件。JDK中提供了一个KeyEvent类表示键盘事件,处理KeyEvent事件的监听器对象需要实现KeyListener接口或者继承KeyAdapter类。

class colorBtKeyDeal extends KeyAdapter {
    private LottoGames event;

    public colorBtKeyDeal(LottoGames event) {
        this.event = event;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyChar() == 'c') {
            event.getContentPane().setBackground(Color.CYAN);
        }
        if(e.getKeyChar() == 'g') {
            event.getContentPane().setBackground(Color.green);
        }
        if(e.getKeyChar() == 'b') {
            event.getContentPane().setBackground(Color.blue);
        }
        if(e.getKeyChar() == 'w') {
            event.getContentPane().setBackground(Color.white);
        }
    }
}

动作事件:
动作事件与前面三种事件有所不同,它不代表某个具体的动作,只是表示一个动作发生了。例如,在关闭一个文件时,可以通过键盘关闭,也可以通过鼠标关闭。在这里读者不需要关心使用哪种方式对文件进行关闭,只要是对关闭按钮进行操作,即触发了动作事件。
在Java中,动作事件用ActionEvent类表示,处理ActionEvent事件的监听器对象需要实现ActionListener接口。监听器对象在监听动作时,不会像鼠标事件一样处理鼠标的移动和单击的细节,而是去处理类似于“按钮按下”这样“有意义”的事件。

class ButtonsActionDeal implements ActionListener {
    private LottoGames event;
    private List<String> list;

    public ButtonsActionDeal(LottoGames event) {
        this.event = event;
        this.list = new ArrayList<>();
        String[] strings = new String[] { "空", "一百万", "空", "空", "一千万", "空", "一个亿", "空"};
        list.addAll(Arrays.asList(strings));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton bt = (JButton)e.getSource();
        Random ran = new Random();
         int num = ran.nextInt(list.size());
        String str = bt.getText();
        for (int i = 0; i < event.buttons.length; i++) {
            if(event.buttons[i].getText().equals(str)) {
                event.buttons[i].setText(list.get(num));
                list.remove(num);
                break;
            }
        }
    }
}

实现:

  • PS
  1. 本人不善搭配,颜色形状可能会丑,但主要为了练习事件处理
  2. 某个位置每次奖项都会不同。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class LottoGames extends JFrame {
    JButton colorBt;
    JButton beginBt;
    JButton[] buttons;
    JPanel panel;
    boolean key;
    public LottoGames() {
        this.setSize(330,550);
        this.setTitle("Welcome to LottoGame");
        this.setLayout(null);
        this.setResizable(false);
        this.getContentPane().setBackground(Color.yellow);
        init();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void init() {
        colorBt = new JButton("键盘输入换颜色:c浅蓝 g绿色 b蓝色 w白色");
        colorBt.setSize(300,30);
        colorBt.setLocation(10,10);
        colorBt.setContentAreaFilled(false);
        colorBt.setBorderPainted(false);
        colorBtKeyDeal dealKey = new colorBtKeyDeal(this);
        colorBt.addKeyListener(dealKey);
        this.add(colorBt);
        beginBt = new JButton("欢迎光临");
        beginBt.setSize(200,80);
        beginBt.setLocation(60,100);
        beginBt.setFont(new Font("", 1, 25));
        beginBt.setBackground(Color.lightGray);
        beginBt.setBorderPainted(false);
        BeginBtMouseDeal dealMou = new BeginBtMouseDeal(this);
        beginBt.addMouseListener(dealMou);
        this.add(beginBt);
        beginGames();

    }

    public void beginGames() {
        panel = new JPanel();
        panel.setSize(300,300);
        panel.setLocation(10,210);
        panel.setLayout(new GridLayout(3,3));
        panel.setBackground(Color.cyan);
        buttons = new JButton[9];
        String[] buttonsNames = new String[] {"祝", "你", "一", "生", "好", "运", "发", "大", "财"};
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(buttonsNames[i]);
            buttons[i].setFont(new Font("", 4, 20));
            buttons[i].setBackground(Color.red);
            ButtonsActionDeal buttonsDeal = new ButtonsActionDeal(this);
            buttons[i].addActionListener(buttonsDeal);
            panel.add(buttons[i]);
        }
        this.add(panel);
    }
}

class colorBtKeyDeal extends KeyAdapter {
    private LottoGames event;

    public colorBtKeyDeal(LottoGames event) {
        this.event = event;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyChar() == 'c') {
            event.getContentPane().setBackground(Color.CYAN);
        }
        if(e.getKeyChar() == 'g') {
            event.getContentPane().setBackground(Color.green);
        }
        if(e.getKeyChar() == 'b') {
            event.getContentPane().setBackground(Color.blue);
        }
        if(e.getKeyChar() == 'w') {
            event.getContentPane().setBackground(Color.white);
        }
    }
}

class BeginBtMouseDeal extends MouseAdapter {
    private LottoGames event;

    public BeginBtMouseDeal(LottoGames event) {
        this.event = event;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getClickCount() == 2) {
            event.beginBt.setText("奖励都假的");
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        event.beginBt.setText("双击发现秘密");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        event.beginBt.setText("欢迎光临");
    }
}

class ButtonsActionDeal implements ActionListener {
    private LottoGames event;
    private List<String> list;

    public ButtonsActionDeal(LottoGames event) {
        this.event = event;
        this.list = new ArrayList<>();
        String[] strings = new String[] { "空", "一百万", "空", "空", "一千万", "空", "一个亿", "空"};
        list.addAll(Arrays.asList(strings));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton bt = (JButton)e.getSource();
        Random ran = new Random();
         int num = ran.nextInt(list.size());
        String str = bt.getText();
        for (int i = 0; i < event.buttons.length; i++) {
            if(event.buttons[i].getText().equals(str)) {
                event.buttons[i].setText(list.get(num));
                list.remove(num);
                break;
            }
        }
    }
}

运行结果图:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/105942200