java - 在扫雷游戏中显示所有空间

标签 java swing jbutton mouselistener minesweeper

我用 jbuttons 做了一个扫雷游戏,我不知道如何在你输了之后再次访问所有按钮来显示最终游戏。我通过将 setText 方法与 jbuttons 一起使用来设置文本,但我不知道如何再次访问它们。如果有帮助,这就是我创建所有按钮的方式:

    for(int x = 0; x < rows ;x++)
    {
        for(int j = 0; j < columns; j++)
        {
            MineButton button = new MineButton(x,j);
            // adds a mouselistener for every button
            button.addMouseListener(new MouseHandler());
            this.add(button);
        }
    }   

我可以将其更改为一组 jbutton,但我不认为我可以通过它来显示它们。欢迎任何建议。

最佳答案

您应该创建一些数组,用于存储对所有按钮的引用。请看我的小例子,它应该可以帮助你理解它是如何工作的:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MineSweeper {

    public static void main(String[] args) {
        MainFrame window = new MainFrame();
        window.setVisible(true);
    }
}

/**
 * Main frame. Initialize window and adds panel with buttons and clear button on
 * the window.
 * */
class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private final Board board = new Board(10, 11);

    public MainFrame() {
        setLocation(400, 400);
        setLayout(new GridLayout(2, 1));
        add(board);
        add(createClearButton());
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private JButton createClearButton() {
        JButton button = new JButton();
        button.setText("Clear");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                board.clear();
            }
        });
        return button;
    }
}

/***
 * This class contains all buttons on one panel. We initialize all buttons in
 * constructor. We can use {@link Board#clear()} method for reveal all buttons.
 * */
class Board extends JPanel {

    private static final long serialVersionUID = 1L;

    private JButton[][] plate;
    private int numberOfRows;
    private int numberOfColumns;

    public Board(int numberOfRows, int numberOfColumns) {
        this.numberOfRows = numberOfRows;
        this.numberOfColumns = numberOfColumns;
        this.plate = new JButton[numberOfColumns][numberOfRows];
        setLayout(new GridLayout(numberOfRows, numberOfColumns));
        init();
    }

    private void init() {
        for (int x = 0; x < numberOfColumns; x++) {
            for (int y = 0; y < numberOfRows; y++) {
                JButton button = createNewJButton(x, y);
                plate[x][y] = button;
                add(button);
            }
        }
    }

    private JButton createNewJButton(int x, int y) {
        JButton button = new JButton();
        button.setText(x + ", " + y);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button = (JButton) e.getSource();
                button.setText("X");
            }
        });
        return button;
    }

    public void clear() {
        for (int x = 0; x < numberOfColumns; x++) {
            for (int y = 0; y < numberOfRows; y++) {
                plate[x][y].setText(x + ", " + y);
            }
        }
    }
}

关于java - 在扫雷游戏中显示所有空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15869016/

相关文章:

java - JFileChooser 返回 FileNotFoundException

java - 如何从 JPanel 触发 ActionEvent

java - 如何在调用另一个 JFrame 时删除一个 JFrame

java - 调整工具栏图标的大小

Java如何调用子类的方法

java - Android编码字符串并显示在textView中

java - Java 中 Apache FOP 的 XSL 错误

java - 如何从 ImageIcon 获取图像(obj)

java - 在 TeeChart Android 中将 AxisBreaks 与蜡烛结合使用

java - 在 Java 中将整数转换为罗马数字