You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现JLabel鼠标释放事件检测及扫雷空白区块自动展开?

扫雷游戏开发问题(基于JLabel实现)

我是Java新手,正在独立开发扫雷游戏,为了挑战自己,选择用JLabel而非JButton实现。目前需要实现两个功能:

  • 检测JLabel上的鼠标释放(mouseReleased)事件
  • 点击空白区块时,自动展开周围所有空白区块

原本打算用MouseAdapter,通过MouseEvent.getSource()或getButton()实现,但现在卡住了。

项目分两个包:

  • Graphics:负责UI显示
  • Logics:存储数据与游戏逻辑

Square类代码(继承JLabel实现MouseListener)

package Graphics;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Square extends JLabel implements MouseListener {   
    
    private static final long serialVersionUID = 1L;
    
    public int squareWidth = 50, squareLength = 50;
    Color foreGround;
    
    Color grass1 = new Color(170, 215, 80);
    Color grass2 = new Color(165, 210, 75);
    Color dirt1 = new Color(230, 195, 160);
    Color dirt2 = new Color(215, 185, 155);

    public Square(int x, int y, Color fg, Color bg) {
        foreGround = fg;
        this.setForeground(bg);
        this.setFont(new Font("ArcadeClassic", Font.BOLD, 40));
        this.setHorizontalAlignment(JLabel.CENTER);
        this.setPreferredSize(getPreferredSize());
        this.setBounds(x, y, squareWidth, squareLength);
        this.setBackground(bg);
        this.setOpaque(true);
        this.addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {}
    @Override
    public void mousePressed(MouseEvent e) {}
    @Override
    public void mouseReleased(MouseEvent e) {
        this.setForeground(foreGround);
        if(this.getBackground().getRed() == 170) this.setBackground(dirt1);
        if(this.getBackground().getRed() == 165) this.setBackground(dirt2);
        System.out.println("" + this.getX() + " - "  + this.getY());
    }
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}
    
}

GamePanel类代码(继承JPanel初始化游戏区块)

package Graphics;

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

import Logics.Field;

public class GamePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    
    Color grass1 = new Color(170, 215, 80);
    Color grass2 = new Color(165, 210, 75);
    Color dirt1 = new Color(230, 195, 160);
    Color dirt2 = new Color(215, 185, 155);
    
    int row = 5, col = 5;
    Square square[][] = new Square[row][col];
    Field field = new Field(row, col);
    
    public GamePanel() {
        this.setBackground(new Color(75, 120, 45));     
        field.createBomb(3);
        field.printValue();     
        int x = 20, y = 5;
        int len = 0;
        byte step = 0;      
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                Color temp = null;
                switch(field.getValue(i, j)) {
                    case 1: {
                        temp = Color.blue; break;}
                    case 2: {
                        temp = new Color(75, 120, 45); break;}
                    case 3: {
                        temp = Color.red; break;}
                    case 4: {
                        temp = new Color(125, 30, 165); break;}
                    case 5: {
                        temp = new Color(255, 145, 0); break;}
                    case 6: {
                        temp = new Color(0, 150, 170); break;}}
                if(step == 0) {
                    square[i][j] = new Square(x, y, temp, grass1);
                    step = 1;}
                else {
                    square[i][j] = new Square(x, y, temp, grass2);
                    step = 0;}
                this.add(square[i][j]);
                x += square[i][j].squareWidth;
                len = square[i][j].squareLength;}
            y += len;
            x = 20;}
        this.setSquareText();
        this.setLayout(null);
    }
    
    public void setSquareText() {
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(field.getValue(i, j) == 0) square[i][j].setText("");
                else if(field.getValue(i, j) == -1) square[i][j].setText("x");
                else square[i][j].setText(Integer.toString(field.getValue(i, j)));
            }
        }
    }
    
}

需要的话可以提供Logics包代码。


内容的提问来源于stack exchange,提问作者c o m a

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 10:55:20