Search code examples
javaswingjbuttonmouselistenerminesweeper

Reveal all spaces in a game of Minesweeper


I made a game of minesweeper with jbuttons and I have no clue how to access all of the buttons again to reveal the final game after you lose. I set the text by using the setText method with jbuttons but i have no clue how to access them again.This is how I created all of the buttons if it helps:

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

I can change it into an array of jbuttons but I don't think that I can reveal them all through that. Any advice is welcome.


Solution

  • You should create some array, which will be store references to all buttons. Please see my little example, which should help you to understand how it works:

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