【问题标题】:Reveal all spaces in a game of Minesweeper显示扫雷游戏中的所有空间
【发布时间】:2013-04-07 23:18:31
【问题描述】:

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

    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);
        }
    }   

我可以将其更改为 jbuttons 数组,但我不认为我可以将它们全部显示出来。欢迎任何建议。

【问题讨论】:

  • 按照您的建议保留一个 jbuttons 数组(或其他数据结构,例如 arraylist),然后您可以迭代/枚举它们以执行您想要的任何操作。
  • "I can change it into an array of jbuttons but I don't think that I can reveal them all through that...." -- 确实可以,而且效果很好。
  • 你将如何初始化它?你会在数组的每个部分都创建一个新按钮吗?
  • MineButton 到底是做什么的?它是否存储按钮是否为炸弹或周围有多少炸弹的信息?还是有另一个数组可以处理这个问题?其次,你为什么不使用“MineButton[][]buttons = new MineButtons[width][height];”创建一个 MineButtons 数组并从循环中初始化该数组中的每个按钮?
  • 试试看!但是这种应用程序是为使用数组而设计的。例如:minesweeper-action-events

标签: java swing jbutton mouselistener minesweeper


【解决方案1】:

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

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);
            }
        }
    }
}

【讨论】:

  • @Hovercraft Full Of Eels,好点子。谢谢你的建议。我已经改变了。新名称 - MainFrame。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多