import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class Matching extends JFrame implements MouseListener,
MouseMotionListener {
private JLabel img[] = new JLabel[5];
private JLabel targets[] = new JLabel[5];
private Point pressPoint; // 鼠标按住时的坐标
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Matching frame = new Matching();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Matching() {
super();
getContentPane().setLayout(new BorderLayout());
setBounds(100, 100, 536, 401);
setTitle("简易配对游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel imagePanel = new JPanel();
imagePanel.setLayout(null);
imagePanel.setOpaque(false);
setGlassPane(imagePanel);
getGlassPane().setVisible(true);
ImageIcon icon[] = new ImageIcon[5];
icon[0] = new ImageIcon(getClass().getResource("kafei.png"));
icon[1] = new ImageIcon(getClass().getResource("xianshiqi.png"));
icon[2] = new ImageIcon(getClass().getResource("xiyiji.png"));
icon[3] = new ImageIcon(getClass().getResource("yifu.png"));
icon[4] = new ImageIcon(getClass().getResource("zixingche.png"));
final JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
for (int i = 0; i < 5; i++) {
img = new JLabel(icon); // 创建图像标签
img.setSize(50, 50); // 设置标签大小
img.setBorder(new LineBorder(Color.GRAY)); // 设置线性边框
int x = (int) (Math.random() * (getWidth() - 50)); // 生成随机坐标
int y = (int) (Math.random() * (getHeight() - 150));
img.setLocation(x, y); // 设置随机坐标
img.addMouseListener(this); // 为每个图像标签添加鼠标事件监听器
img.addMouseMotionListener(this);
imagePanel.add(img); // 添加图像标签到图像面板
targets = new JLabel(); // 创建匹配位置标签
targets.setOpaque(true); // 使标签不透明,以设置背景色
targets.setBackground(Color.ORANGE); // 设置标签背景色
targets.setHorizontalTextPosition(SwingConstants.CENTER); // 设置文本与图像水平居中
targets.setVerticalTextPosition(SwingConstants.BOTTOM); // 设置文本显示在图像下方
targets.setPreferredSize(new Dimension(80, 80)); // 设置标签首先大小
targets.setHorizontalAlignment(SwingConstants.CENTER); // 文字居中对齐
bottomPanel.add(targets); // 添加标签到底部面板
}
targets[0].setText("咖啡"); // 设置匹配位置的文本
targets[1].setText("显示器");
targets[2].setText("洗衣机");
targets[3].setText("衣服");
targets[4].setText("自行车");
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
pressPoint = e.getPoint(); // 保存拖放图片标签时的起始坐标
}
public void mouseReleased(MouseEvent e) {
if (check()){ // 如果配对正确
getGlassPane().setVisible(false);
for(int i=0;i<5;i++){ // 遍历所有匹配位置的标签
targets.setText("配对成功"); // 设置正确提示
targets.setIcon(img.getIcon()); // 设置匹配的图标
}
}
}
private boolean check() {
boolean result = true;
for (int i = 0; i < 5; i++) {
Point location = img.getLocationOnScreen(); // 获取每个图像标签的位置
Point seat = targets.getLocationOnScreen(); // 获取每个对应位置的坐标
targets.setBackground(Color.GREEN); // 设置匹配后的颜色
// 如果配对错误
if (location.x < seat.x || location.y < seat.y
|| location.x > seat.x+80 || location.y > seat.y+80) {
targets.setBackground(Color.ORANGE); // 回复对应位置的颜色
result= false; // 检测结果为false
}
}
return result; // 返回检测结果
}
/**
* 鼠标拖动组件时的事件处理方法
*/
public void mouseDragged(MouseEvent e) {
JLabel source = (JLabel) e.getSource(); // 获取事件源组件
Point imgPoint = source.getLocation(); // 获取组件坐标
Point point = e.getPoint(); // 获取鼠标坐标
source.setLocation(imgPoint.x + point.x - pressPoint.x, imgPoint.y
+ point.y - pressPoint.y); // 设置组件新坐标
}
public void mouseMoved(MouseEvent e) {
}
}