-2

私は Java (およびほぼすべての言語) の新人です。課題でマインスイーパ ゲームを作ろうとしていますが、まだベータ段階です。ただし、ボタンを押すと地雷を「0」として表示する方法に行き詰まっています。2D 配列を使用して Jbuttons を作成しましたが、必要な特定のボタンに「0」を付けることができないようです。

これが私のコードです(醜さ/おそらく非効率性を許してください)GUIに関しては、私は本当に無知です。

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

1 に答える 1

0

私の提案は、番号のJButtonを表示するのではなく、代わりに、最初にJButtonを表示するように、鉱山セルにCardttLayoutを使用させますが、押すとJLabelを表示します。私はstackoverflowでこれの例を持っています。これを検索して、リンクを付けて返信します。

リンクは次のとおりです。マインスイーパアクションイベント

そして、ここに私のコードのいくつかをもう少し詳しく説明するリンクがあります:私のminesweeperプログラムの助けが必要ですか?

プログラムを実行すると、次のような初期画面が表示されます。

MineSweeper1

空の正方形を押すと、すべての空の正方形のクリアランスが表示されます。

MineSweeper2

そして爆弾を打った後、あなたはすべての爆弾が現れるのを見るでしょう:

MineSweeper3

于 2012-09-11T22:20:55.373 に答える