gpt4 book ai didi

Java 扫雷 GUI

转载 作者:行者123 更新时间:2023-12-02 00:38:23 25 4
gpt4 key购买 nike

我必须制作一个扫雷 GUI,但我不知道如何使每个按钮都有自己的 mouseAdapter。当我单击某个按钮时,它会更改屏幕上的每个按钮,而不仅仅是我单击的按钮。我可以提供一些关于如何执行此操作的指示吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MinePanel extends JPanel
{
private final int ROWS = 10;
private final int COLUMNS = 10;
private int numOfMines;
private double probability;
private JLabel statusBar;
private int minesLeft;
private JButton Minefield[][];
private boolean hasMine = false;
private int curRow = 0;
private int curCol = 0;
private JButton cell;

public MinePanel()
{
setLayout(new GridLayout(10, 10));
Minefield = new JButton[ROWS][COLUMNS];
//menuStuff();
buildButtonField();
}

//I will eventually want to create a menu, but it's extra credit so I'm going to
//wait until I have a working program.
public JMenuBar menuStuff()
{
JMenuBar menuBar = new JMenuBar();
//add(menuBar);
JMenu file = new JMenu("File");
menuBar.add(file);
JMenu edit = new JMenu("Edit");
menuBar.add(edit);
return menuBar;
}

//Adds one to the total number of mines.
public void addMine()
{
numOfMines++;
}

//Removes one from the total number of mines.
public void removeMine()
{
numOfMines--;
}

//Assigns a JButton the value of true or false, which represents whether or not it
//it is a mine. Is this the correct way to do this?
public boolean setMines(JButton button)
{
probability = Math.random()*100;
if(probability >= 80)
{
hasMine = true;
addMine();
}
else
{
hasMine = false;
}
return hasMine;
}

//This is supposed to change the JButton that is clicked on to either a T
//or an F, eventually this will reveal what the value of the button is
//or place a flag down. I'm guessing I shouldn't have used a for loop, but I don't
//know how else to do it.
private class MouseHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
for(int r=0; r<ROWS; r++)
{
for(int c=0; c<COLUMNS; c++)
{
if(e.getButton() == 1)
{
Minefield[r][c].setText("T");
}
else if(e.getButton() == 3)
{
Minefield[r][c].setText("F");
}
}
}
}
}

//This builds the "Minefield" with a ROWS*COLUMNS amount of buttons.
//It works fine but I'm open to suggestions on a better way to do this.
public void buildButtonField()
{
for(int r = 0; r < ROWS; r++)
{
curRow++;
for(int c = 0; c < COLUMNS; c++)
{
Minefield[r][c] = new JButton((r+1) + ", " + (c+1));

setMines(Minefield[r][c]);

Minefield[r][c].addMouseListener(new MouseHandler());

add(Minefield[r][c]);
curCol++;
}
}
}
}

预先感谢你们提供的任何帮助!我要去上课,所以可能需要一些时间才能回复。

最佳答案

当鼠标事件发生时,您将遍历网格中的每个单元格。。首先,消除 MouseHandler 中的循环:

private class MouseHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
if(e.getButton() == 1)
{
Minefield[r][c].setText("T");
}
else if(e.getButton() == 3)
{
Minefield[r][c].setText("F");
}
}
}

然后,您可以向行和列的 MouseHandler 构造函数添加两个参数,以便每个鼠标处理程序知道它负责哪个单元格。

public class MouseHandler extends MouseAdapter
{
public int r, c; // instance variables
public MouseHandler(int r, int c)
{
this.r = r;
this.c = c;
}

... // above code here
}

创建对象可能如下所示:Minefield[r][c].addMouseListener(new MouseHandler(r,c));

您也可以在事件发生时查找鼠标坐标,以手动确定单击了哪个单元格,但这非常困惑。

关于Java 扫雷 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22028923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com