【问题标题】:Flagging Buttons for Minesweeper GUI Game扫雷 GUI 游戏的标记按钮
【发布时间】:2016-09-26 10:21:40
【问题描述】:

我在这段代码上工作了几个小时,终于接近完成。一旦我弄清楚如何添加通过右键单击标记地雷的功能,我将添加一个地雷计数器和一个计时器。我了解到 JButton 无法识别右键单击,因此我必须使用 MouseEvent。从那里,我迷路了。有人可以在正确的方向上推动我吗?

这是我目前拥有的:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class MineSweeper implements ActionListener {

    JFrame frame = new JFrame("Mine Sweeper");
    JButton reset = new JButton("Reset");
    JButton[][] buttons = new JButton[20][20];//button array
    JButton[] flags = new JButton[20];
    int[][] counts = new int[20][20];
    Container grid = new Container(); //grid for buttons
    int MineNum = 50;//number of mines
    final int MINE = 10;//int value to identify a mine



    public MineSweeper() {
        frame.setSize(500, 500);
        frame.setLayout(new BorderLayout());
        frame.add(reset, BorderLayout.NORTH);
        reset.addActionListener(this);
        //Grid of Buttons
        grid.setLayout(new GridLayout(20,20));
        for (int a = 0; a<buttons.length; a++) {
            for (int b = 0; b < buttons[0].length; b++) {
                buttons[a][b] = new JButton("▒");
                buttons[a][b].addActionListener(this);
                buttons[a][b].addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        if (e.getButton() == MouseEvent.BUTTON3) {
                            mineFlagger(true);
                        }
                    }
                });
                grid.add(buttons[a][b]);
            }
        }
        frame.add(grid, BorderLayout.CENTER);
        makeRandomMines();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void mineFlagger (boolean flag){
        for (int i = 0; i < buttons.length; i++) {
            for (int j = 0; j < buttons[0].length; j++) {
                if(flag == true) {
                    buttons[i][j].setText("F");
                }
            }
        }
    }

    public static void main(String[] args) {
        new MineSweeper();
    }

    public void makeRandomMines() {
        //initializes list of random pairs
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int x = 0; x < counts.length; x++) {
            for (int y = 0; y < counts[0].length; y++) {
                list.add(x * 100 + y);//changed y to x
            }
        }
        //resets the counts in case reset button is pressed & picks random mines
        counts = new int[20][20];
        for (int i = 0; i < MineNum; i++) {
            int choice = (int) (Math.random() * list.size());
            counts[list.get(choice) / 100][list.get(choice) % 100] = MINE;
            list.remove(choice);
        }
        //neighbor counts(how many mines are touching this square.)
        for (int x = 0; x < counts.length; x++) {
            for (int y = 0; y < counts[0].length; y++) {
                int neighborCount = 0;
                if (counts[x][y] != MINE) {
                    if(x > 0 && y > 0 && counts[x - 1][y - 1] == MINE) { //a mine is up & left
                        neighborCount++;
                    }
                    if(y > 0 && counts[x][y - 1] == MINE) { //a mine is up
                        neighborCount++;
                    }
                    if(x < counts.length - 1 && y > 0 && counts[x + 1][y - 1] == MINE) { // a mine is left
                        neighborCount++;
                    }
                    if(x > 0 && counts[x - 1][y] == MINE) { //left
                        neighborCount++;
                    }
                    if(x < counts.length - 1 && counts[x + 1][y] == MINE) { //mine is right
                        neighborCount++;
                    }
                    if(x > 0 && y < counts[0].length - 1 && counts[x - 1][y + 1] == MINE) { //mine is down
                        neighborCount++;
                    }
                    if(y < counts[0].length - 1 && counts[x][y + 1]== MINE) {//mine is up right
                        neighborCount++;
                    }
                    if(x < counts[0].length - 1 && y < counts[0].length - 1 && counts[x + 1][y + 1]== MINE) {//mine is down left
                        neighborCount++;
                    }
                    counts[x][y] = neighborCount;
                }
            }
        }
    }

    public void lostGame() {
        for (int x = 0; x < buttons.length; x++) {
            for (int y = 0; y < buttons[0].length; y++) {
                if (buttons[x][y].isEnabled()) {
                    if (counts[x][y] != MINE) {
                        buttons[x][y].setText(counts[x][y] + "");
                        buttons[x][y].setEnabled(false);
                    }
                    else {
                        buttons[x][y].setText("✪");
                        buttons[x][y].setEnabled(false);
                    }
                }
            }
        }
        JOptionPane.showMessageDialog(null, "You Lose!\n" + "You clicked on a mine!", "BOOM!", JOptionPane.INFORMATION_MESSAGE);
    }

    public void checkWin() {
        boolean winner = true;

        for (int x = 0; x < counts.length; x++) {
            for (int y = 0; y < counts[0].length; y++) {
                if (counts[x][y] != MINE && buttons[x][y].isEnabled()) {
                    winner = false;
                }
            }
        }
        if (winner == true) {
            JOptionPane.showMessageDialog(frame, "You win!");
        }
    }

    public void zeroCleaner(ArrayList<Integer> toClear) {
        if (toClear.size() == 0) {
            return;
        }
        else {
            int x = toClear.get(0) / 100;
            int y = toClear.get(0) % 100;
            toClear.remove(0);
            if(x > 0 && y > 0 && buttons[x-1][y-1].isEnabled()) { //up and left
                buttons[x - 1][y - 1].setText(counts[x-1][y-1] + "");
                buttons[x - 1][y - 1].setEnabled(false);
                if (counts[x - 1][y - 1] == 0) {
                    toClear.add((x-1) * 100 + (y-1));
                }
            }
            if (y > 0 && buttons[x][y-1].isEnabled()) { // up
                buttons[x][y - 1].setText(counts[x][y-1] + "");
                buttons[x][y - 1].setEnabled(false);
                if (counts[x][y - 1] == 0) {
                    toClear.add(x * 100+(y - 1));
                }
            }
            if (x < counts.length - 1 && y > 0 && buttons[x+1][y-1].isEnabled()) { //up right
                buttons[x + 1][y - 1].setText(counts[x+1][y-1] + "");
                buttons[x + 1][y - 1].setEnabled(false);
                if (counts[x + 1][y - 1] == 0) {
                    toClear.add((x + 1)*100+(y - 1));
                }
            }
            if(x > 0 && y > 0 && buttons[x-1][y].isEnabled()) { //left
                buttons[x - 1][y].setText(counts[x-1][y] + "");
                buttons[x - 1][y].setEnabled(false);
                if (counts[x-1][y] == 0) {
                    toClear.add((x-1)*100+y);
                }
            }
            if (x < counts.length - 1 && buttons[x+1][y].isEnabled()) { //right
                buttons[x + 1][y].setText(counts[x+1][y] + "");
                buttons[x + 1][y].setEnabled(false);
                if (counts[x + 1][y] == 0) {
                    toClear.add((x + 1)*100+y);
                }
            }
            if(x > 0 && y < counts[0].length - 1 && buttons[x-1][y+1].isEnabled()) { //down and left
                buttons[x - 1][y + 1].setText(counts[x-1][y+1] + "");
                buttons[x - 1][y + 1].setEnabled(false);
                if (counts[x-1][y+1] == 0) {
                    toClear.add((x-1)*100+(y+1));
                }
            }
            if (y < counts[0].length - 1 && buttons[x][y+1].isEnabled()) { // down
                buttons[x][y + 1].setText(counts[x][y+1] + "");
                buttons[x][y + 1].setEnabled(false);
                if (counts[x][y + 1] == 0) {
                    toClear.add(x * 100+(y + 1));
                }
            }
            if (x < counts.length - 1 && y < counts[0].length - 1 && buttons[x+1][y+1].isEnabled()) { //down right
                buttons[x + 1][y + 1].setText(counts[x+1][y+1] + "");
                buttons[x + 1][y + 1].setEnabled(false);
                if (counts[x + 1][y + 1] == 0) {
                    toClear.add((x + 1)*100+(y + 1));
                }
            }
            zeroCleaner(toClear);
        }
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if(event.getSource().equals(reset)) {
         //Resets the playing field
            for (int x = 0; x < buttons.length; x++) {
                for (int y = 0; y < buttons[0].length; y++) {
                    buttons[x][y].setEnabled(true);
                    buttons[x][y].setText("▒");
                }
            }
            makeRandomMines();
        }
        else {
            for (int x = 0; x < buttons.length ; x++) {
                for (int y = 0; y < buttons[0].length; y++) {
                    if(event.getSource().equals(buttons[x][y])) {
                        if (counts[x][y]== MINE) {
                            lostGame();
                        }
                        else if(counts[x][y] == 0) {
                            buttons[x][y].setText(counts[x][y]+ "");
                            buttons[x][y].setEnabled(false);
                            ArrayList<Integer> toClear = new ArrayList<>();
                            toClear.add(x*100+y);
                            zeroCleaner(toClear);
                            checkWin();
                        }
                        else {
                            buttons[x][y].setText(counts[x][y]+ "");
                            buttons[x][y].setEnabled(false);
                            checkWin();
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 我不明白你的问题。您已经有了检查按下哪个按钮的代码:e.getButton() == MouseEvent.BUTTON3
  • @Jhonny007 MouseEvent.BUTTON3 是模棱两可的。 Button3 可能会因鼠标/操作系统等而有所不同......此外,他在询问如何将检测右键单击与它悬停的按钮相关联。因为正如他所说,按钮无法直接检测到右键单击。

标签: java user-interface mouseevent minesweeper


【解决方案1】:

这应该可以解决您的问题:

for (int a = 0; a<buttons.length; a++) {
    for (int b = 0; b < buttons[0].length; b++) {
        buttons[a][b] = new JButton("▒");
        buttons[a][b].addActionListener(this);
        final int finalB = b;
        final int finalA = a;
        buttons[a][b].addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    mineFlagger(true, finalA, finalB);
                }
            }
        });
        grid.add(buttons[a][b]);
    }
}


public void mineFlagger (boolean flag, int x, int y){
    buttons[x][y].setText("F");
}

编辑: 用于切换标志(延迟实现)

if (SwingUtilities.isRightMouseButton(e)) {
    mineFlagger(finalA, finalB);
}

public void mineFlagger(int x, int y) {
    if(buttons[x][y].getText().equals("F")) {
        buttons[x][y].setText("▒");
    } else if (buttons[x][y].getText().equals("▒")){
        buttons[x][y].setText("F");
    }
}

【讨论】:

  • 这成功了!谢谢!我怎样才能让它在再次右键单击时恢复正常?
  • 我也为这个问题添加了一个解决方案,尽管这不是一个好的方法。
【解决方案2】:

如果您导入“javax.swing.SwingUtilities”,您可以使用实用程序方法:

SwingUtilities.isRightMouseButton(MouseEvent anEvent) 

然后当按下鼠标右键时,获取鼠标的位置,找到该位置的按钮,然后使用该事件将特定按钮上的图片更改为标志的图片。

你可以使用:

MouseInfo.getPointerInfo().getLocation()

获取鼠标相对于屏幕的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2023-03-04
    • 2012-11-15
    • 2021-02-28
    • 1970-01-01
    • 2014-03-28
    • 2014-08-11
    相关资源
    最近更新 更多