首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java扫雷GUI

问Java扫雷GUI
EN

Stack Overflow用户
提问于 2014-02-26 00:01:58
回答 1查看 8.8K关注 0票数 2

我必须做一个扫雷GUI,但我不知道如何使每个按钮有它自己的mouseAdapter。当我点击一个按钮时,它会改变屏幕上的每个按钮,而不是我点击的那个按钮。能给我一些关于怎么做的建议吗?

代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MinePanel extends JPanel 
{
    private final int ROWS = 10;
    private final int COLUMNS = 10;
    private int numOfMines;
    private double probability;
    private JLabel statusBar;
    private int minesLeft;
    private JButton Minefield[][];
    private boolean hasMine = false;
    private int curRow = 0;
    private int curCol = 0;
    private JButton cell;

    public MinePanel() 
    {
        setLayout(new GridLayout(10, 10));
        Minefield = new JButton[ROWS][COLUMNS];
        //menuStuff();
        buildButtonField();
    }

    //I will eventually want to create a menu, but it's extra credit so I'm going to
    //wait until I have a working program.
    public JMenuBar menuStuff()
    {
        JMenuBar menuBar = new JMenuBar();
        //add(menuBar);
        JMenu file = new JMenu("File");
        menuBar.add(file);
        JMenu edit = new JMenu("Edit");
        menuBar.add(edit);
        return menuBar;
    }

    //Adds one to the total number of mines.
    public void addMine()
    {
        numOfMines++;
    }

    //Removes one from the total number of mines.
    public void removeMine()
    {
        numOfMines--;
    }

    //Assigns a JButton the value of true or false, which represents whether or not it
    //it is a mine. Is this the correct way to do this?
    public boolean setMines(JButton button)
    {
        probability  = Math.random()*100;
        if(probability >= 80)
        {
            hasMine = true;
            addMine();
        }
        else
        {
            hasMine = false;
        }
        return hasMine;
    }

    //This is supposed to change the JButton that is clicked on to either a T
    //or an F, eventually this will reveal what the value of the button is
    //or place a flag down. I'm guessing I shouldn't have used a for loop, but I don't 
    //know how else to do it.
    private class MouseHandler extends MouseAdapter
    {
        public void mouseClicked(MouseEvent e)
        {
            for(int r=0; r<ROWS; r++)
            {
                for(int c=0; c<COLUMNS; c++)
                {
                    if(e.getButton() == 1)
                    {
                        Minefield[r][c].setText("T");
                    }
                    else if(e.getButton() == 3)
                    {
                        Minefield[r][c].setText("F");
                    }
                }
            }
        }
    }

    //This builds the "Minefield" with a ROWS*COLUMNS amount of buttons.
    //It works fine but I'm open to suggestions on a better way to do this.
    public void buildButtonField()
    {
        for(int r = 0; r < ROWS; r++)
        {
            curRow++;
            for(int c = 0; c < COLUMNS; c++)
            {
                Minefield[r][c] = new JButton((r+1) + ", " + (c+1));

                setMines(Minefield[r][c]);

                Minefield[r][c].addMouseListener(new MouseHandler());

                add(Minefield[r][c]);
                curCol++;
            }
        }
    }
}

提前感谢你们的帮助!我要去上课,所以我需要一点时间来回应。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-26 00:08:07

当发生鼠标事件时,您将遍历网格中的每个单元格。首先,去掉MouseHandler中的循环:

代码语言:javascript
复制
private class MouseHandler extends MouseAdapter
{
    public void mouseClicked(MouseEvent e)
    {
        if(e.getButton() == 1)
        {
            Minefield[r][c].setText("T");
        }
        else if(e.getButton() == 3)
        {
            Minefield[r][c].setText("F");
        }
    }
}

然后,可以为行和列向MouseHandler构造函数添加两个参数,以便每个鼠标处理程序都知道它负责哪个单元格。

代码语言:javascript
复制
public class MouseHandler extends MouseAdapter
{
    public int r, c; // instance variables
    public MouseHandler(int r, int c)
    {
        this.r = r;
        this.c = c;
    }

    ... // above code here
}

创建对象可能如下所示:Minefield[r][c].addMouseListener(new MouseHandler(r,c));

您也可以在事件引发时找到鼠标坐标,以手动确定单击了哪个单元格,但这非常混乱。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22028923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档