-2

Im a big rookie in Java ( and pretty much every language). I am trying to make a Minesweeper game for an assignment and I am still in the beta stages. I am stuck however on how to display the mines as a "0" once the button is pressed. I created the Jbuttons using a 2d array, but I cant seem to put the "0" on the specific button I want.

Here is my code ( please excuse the ugliness/ probable inefficiency) I really am clueless when it comes to GUI's.

import java.awt.*;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Grid extends JPanel {
    // Initializes rows,columns and mines
    int rows = 10;
    int cols = 10;
    int i, j = 0;
    int mines = 10;
    boolean[][] setmine = new boolean[rows][cols];
    boolean[][] clickable = new boolean[rows][cols];
    private JToggleButton squares[][], squares2[][];

    // Contructor for creating a grid(with default size 400x400
    public Grid() {
        this.setSize(600, 600);
        this.setLayout(new GridLayout(rows, cols));
        squares = new JToggleButton[rows][cols];
        buildButtons();
    }

    private void buildButtons() {
        // loops are used for creating the "buttons" on the grid.
        int MinesNeeded = 10;
        // builds buttons
        // ----------------------------------------
        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                squares[i][j] = new JToggleButton();
                // squares[i][j].setEnabled(false);
                squares[i][j].setSize(600, 600);

                // --------------------------------------------------
                // This part randomises the mines
                // -----------------------------------------------------
                while (MinesNeeded > 0) {

                    int x = (int) Math.floor(Math.random() * rows);
                    int y = (int) Math.floor(Math.random() * cols);
                    if (!setmine[x][y]) {
                        setmine[x][y] = true;

                        MinesNeeded--;
                    }
                }
                // ----------------------------------------------------------------------------

                this.add(squares[i][j]);

                if (setmine[i][j] == true) {

                    squares[i][j].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                            // this is the problem
                            squares[i][j].setText("0");
                        }
                    });
                }
            }
        }
    }

    public static void main(String[] args) {
        Grid g = new Grid();
        JFrame frame = new JFrame("Minesweeper");

        frame.add(g);
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Brian
  • 17,079
  • 6
  • 43
  • 66
Seeker
  • 262
  • 1
  • 5
  • 17

1 Answers1

0

My suggestion is not to display a JButton for the number, but instead have your mine cell use a CardttLayout so that it will initially show a JButton, but then when pressed show a JLabel. I have an example of this on stackoverflow that I'll search for and get back to you with a link.

Here's the link: Minesweeper Action Events

And here's a link that explains some of my code in a bit more detail: Need help with my minesweeper program?

If you run the program, you'll see an initial screen like so:

MineSweeper1

After pressing an empty square, you'd see clearance of all empty squares:

MineSweeper2

And after hitting a bomb, you'd see all bombs show up:

MineSweeper3

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373