- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在之前得到良好的帮助后,我已经完成了我的扫雷游戏。然而,有一个问题我想不通。游戏本身运行良好,但是,我可以选择让用户更改难度设置(初级、中级、高级),这让我感到悲伤。用户从 JMenuBar 中选择“游戏”,然后从下拉菜单中选择“更改难度”。这会调用 selectDifficulty() 方法,该方法会打开一个 JOptionMessageBox,该 JOptionMessageBox 使用 JRadioButtons 让用户选择 4 个选项之一(第 4 个选项是自定义选项 - 但到目前为止我还没有实现)。当用户第一次更改难度模式时,该对话框似乎工作正常。然而,当用户第二次或更多次尝试改变难度时,游戏会执行该设置,然后再次出现对话框并提示用户重新选择难度。有时这种情况会重复两次,然后消失,有时会重复 4 或 5 次。我将不胜感激任何帮助,因为我不知道为什么会发生这种情况。
此外,我也希望获得一些关于如何改进难度设置更改的建议。按照目前的情况,代码删除了 JPanel 和雷区(由按钮组成),并使用新的难度设置重新实例化按钮/Japanels。有没有办法提高效率?
有一个逻辑类和一个 GUI 玻璃。上述两个问题都存在于 GUI 类中。
private void selectDifficulty() {
group.add(b1);
group.add(b2);
group.add(b3);
group.add(b4);
b1.setSelected(true);
Object[] array = {new JLabel("Select Difficulty"),b1,b2,b3,b4};
JOptionPane.showMessageDialog(null, array);
if (b1.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 1;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 1;
}else {
rw = 20;
rh = 20;
mode = 1;
}
start(9,9);
}else if (b2.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 2;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 2;
}else {
rw = 20;
rh = 20;
mode = 2;
}
start(15,15);
}else if (b3.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 3;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 3;
}else {
rw = 20;
rh = 20;
mode = 3;
}
start(20,20);
}
}
完整代码:图形用户界面类
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class MineSweeperGUI extends JFrame implements MouseListener {
MinesweeperLogic logicClass = new MinesweeperLogic();
JButton newGameButton = new JButton ("New Game");
JMenuItem optionsButton = new JMenuItem ("Change Difficulty");
JRadioButton b1 = new JRadioButton ("Beginner: 9 X 9 Grid (10 Mines)");
JRadioButton b2 = new JRadioButton ("Intermediate: 15 X 15 Grid (36 Mines)");
JRadioButton b3 = new JRadioButton ("Advanced: 20 X 20 Grid (80 Mines)");
JRadioButton b4 = new JRadioButton ("Custom");
ButtonGroup group = new ButtonGroup();
JMenuItem aboutButton = new JMenuItem ("About Minesweeper");
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
int w, h, rw = 0, rh = 0, mode = 1;
public void MineSweeper(int width, int height) {
//setupI();
w = width;
h = height;
logicClass.startNewGame(w, h);
GridLayout layout = new GridLayout (w, h);
p2.setLayout(new BorderLayout());
p.setLayout(layout);
p2.add(p, BorderLayout.CENTER);
for(int x = 0 ; x < w ; x++) {
for(int y = 0 ; y < h ; y++) {
logicClass.label[x][y] = new Button();
logicClass.label[x][y].setPreferredSize(new Dimension(20,20));
logicClass.label[x][y].setBackground(new Color(33,58,156));
logicClass.label[x][y].addMouseListener (this);
p.add(logicClass.label[x][y]);
}
}
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("Game");
JMenu m2 = new JMenu("Help");
m.add(optionsButton);
m2.add(aboutButton);
mb.add(m);
mb.add(m2);
p2.add(p3, BorderLayout.PAGE_START);
newGameButton.setPreferredSize (new Dimension(87, 20));
newGameButton.setFont(new Font("sansserif",Font.BOLD,11));
newGameButton.setForeground(Color.black);
newGameButton.setBackground(new Color(235,52,52));
Border thickBorder = new LineBorder(Color.black, 2);
newGameButton.setBorder(thickBorder);
p3.add(newGameButton);
newGameButton.addMouseListener(this);
optionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
selectDifficulty();
}
});
this.setJMenuBar(mb);
this.add(p2);
this.pack();
this.setVisible(true);
}
private void start(int width, int height){
p2.remove(p);
for(int x = 0 ; x < rw ; x++) {
for(int y = 0 ; y < rh ; y++) {
p.remove(logicClass.label[x][y]);
}
}
p2.remove(p3);
p3.remove(newGameButton);
this.remove(p2);
MineSweeper(width, height);
}
private void lose() {
JOptionPane.showMessageDialog(this, "GAME OVER - YOU LOSE! Starting New Game", "Game Over", JOptionPane.INFORMATION_MESSAGE);
newGame();
}
private void win() {
JOptionPane.showMessageDialog(this, "YOU WIN! Starting New Game", "CONGRATULATIONS", JOptionPane.INFORMATION_MESSAGE);
newGame();
}
private void newGame() {
if(mode==1){
rw = 9;
rh = 9;
start(9,9);
}else if(mode==2){
rw = 15;
rh = 15;
start(15,15);
}else{
rw = 20;
rh = 20;
start(20,20);
}
}
public void mouseClicked(MouseEvent e) {
if(e.getSource() == newGameButton) {
if(e.getButton() == e.BUTTON1) {
newGame();
}
}
for (int x = 0 ; x < w; x++) {
for (int y = 0 ; y < h; y++) {
if(e.getSource() == logicClass.label[x][y]) {
if(e.getButton() == e.BUTTON1) {
if(logicClass.openCell(x, y) == false) {
lose();
} else {
for (int q = 0; q < w; q++) {
for (int j = 0; j < h; j++) {
if (logicClass.label[q][j].getBackground()==Color.green) {
win();
}
}
}
}
}else if(e.getButton() == e.BUTTON3) {
logicClass.markCell(x, y);
}
}
}
}
}
private void selectDifficulty() {
group.add(b1);
group.add(b2);
group.add(b3);
group.add(b4);
b1.setSelected(true);
Object[] array = {new JLabel("Select Difficulty"),b1,b2,b3,b4};
JOptionPane.showMessageDialog(null, array);
if (b1.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 1;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 1;
}else {
rw = 20;
rh = 20;
mode = 1;
}
start(9,9);
}else if (b2.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 2;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 2;
}else {
rw = 20;
rh = 20;
mode = 2;
}
start(15,15);
}else if (b3.isSelected()) {
if (mode == 1){
rw = 9;
rh = 9;
mode = 3;
}else if(mode == 2){
rw = 15;
rh = 15;
mode = 3;
}else {
rw = 20;
rh = 20;
mode = 3;
}
start(20,20);
}
}
public static void main(String[]args) {
MineSweeperGUI guiClass = new MineSweeperGUI();
guiClass.MineSweeper(9,9);
}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
逻辑类: 导入 java.awt.* ;
public class MinesweeperLogic {
public int width, height;
private int w, h, maxBombs;
private boolean mine[][];
private boolean flag[][];
private boolean isClicked[][];
private boolean isZero[][];
private boolean marked;
MineSweeperGUI guiClass;
Button[][] label;
private int surBombs;
private String temp;
private double mineRatio;
public void startNewGame(int width, int height) {
w = width;
h = height;
label = new Button[w][h];
mine = new boolean[w][h];
flag = new boolean[w][h];
isClicked = new boolean[w][h];
isZero = new boolean[w][h];
if ( (w*h) <= 81) {
mineRatio = 0.13;
}else if ( (w*h) <= 255) {
mineRatio = 0.16;
}else {
mineRatio = 0.2;
}
maxBombs = (int) Math.floor (w * h * mineRatio);
for (int i = 0; i < maxBombs; i++) {
int x = (int) (Math.random() * w);
int y = (int) (Math.random() * h);
if (mine[x][y] == false) {
mine[x][y] = true;
isClicked[x][y] = false;
flag[x][y] = false;
isZero[x][y] = false;
} else {
i--;
}
}
}
int getWidth() {
return w;
}
int getHeight() {
return h;
}
boolean openCell(int x, int y) {
isClicked[x][y] = true;
if (mine[x][y] == true && flag[x][y] == false) {
lose();
return false;
} else if (getValue(x, y) > 0 && flag[x][y] == false) {
temp = Integer.toString (getValue(x, y));
label[x][y].setLabel (temp);
label[x][y].setBackground (new Color (111,184,252));
checkWin();
return true;
} else if (getValue(x, y) == 0 && flag[x][y] == false) {
for (int q = x-1; q <= x+1; q++) {
if(q < 0 || q >= w) {
continue;
}
for (int i = y-1; i <= y+1; i++) {
if(i < 0 || i >= h || flag[q][i] == true) { // makes sure that it wont have an error for buttons next to the wall
continue;
}
label[q][i].setBackground(new Color (111,184,252));
if (getValue(q, i) != 0) { // opens surrounding cells that have mines around them (max 8 cells)
temp = Integer.toString (getValue(q, i));
label[q][i].setLabel (temp);
isClicked[q][i] = true;
} else {
for (int k = x-1; k <= x+1; k++) {
if(k < 0 || k >= w) {
continue;
}
for (int m = y-1; m <= y+1; m++) {
if (m < 0 || m >= h || flag[k][m] == true) { // makes sure that it wont have an error for buttons next to the wall
continue;
}
if (isClicked[k][m] == false && getValue(k, m) == 0) { // recursively continues to open all surrounding cells with no mines around them
openCell(k, m);
}
}
}
}
}
}
checkWin();
return true;
} else {
return true;
}
}
boolean markCell(int x, int y) {
if (flag[x][y] == true) {
flag[x][y] = false;
label[x][y].setLabel ("");
label[x][y].setForeground (Color.black);
label[x][y].setFont (new Font (null, Font.PLAIN, 12));
return false;
}
if (isClicked[x][y] == false && flag[x][y] == false) {
flag[x][y] = true;
label[x][y].setFont (new Font ("sansserif", Font.BOLD, 14));
label[x][y].setLabel ("<|");
label[x][y].setForeground (Color.red);
}
if (mine[x][y] == true) {
return true;
} else {
return false;
}
}
boolean isOpen(int x, int y) {
if (isClicked[x][y] == false) {
return false;
} else {
return true;
}
}
boolean isMarked(int x, int y) {
if (flag[x][y] == true) {
return true;
} else {
return false;
}
}
int getValue(int x, int y) {
if (mine[x][y] == true) {
return -1;
} else {
return neighborBombs(x, y);
}
}
private int neighborBombs(int x, int y) { // checks surrounding 8 squares for number of bombs (it does include itself, but has already been checked for a bomb so it won't matter)
surBombs = 0;
for (int q = x-1; q <= x+1; q++) {
if (q < 0 || q >= w) {
continue;
}
for (int i = y-1; i <= y+1; i++) {
if (i < 0 || i >= h) { // makes sure that it wont have an error for buttons next to the wall
continue;
}
if (mine[q][i] == true) {
surBombs++;
}
}
}
return surBombs;
}
private void lose() {
for (int q = 0; q < w; q++) {
for (int j = 0; j < h; j++) {
if (mine[q][j] == true) {
if (label[q][j].getLabel().equals ("<|")) {
label[q][j].setBackground (Color.green);
} else {
label[q][j].setBackground (Color.red);
label[q][j].setLabel ("*");
label[q][j].setForeground (Color.black);
}
}
}
}
}
private void checkWin() {
int count = 0;
int count2 = 0;
for (int i = 0; i < w; i++){
for (int j = 0; j < h; j++) {
if (isClicked[i][j] == true){
count++;
}
if (mine[i][j] == true){
count2++;
}
}
}
if ( (count + count2) == (w * h) ){
win();
}
}
private void win() {
for (int q = 0; q < w; q++) {
for (int j = 0; j < h; j++) {
if (mine[q][j] == true) {
label[q][j].setBackground (Color.green);
label[q][j].setForeground (Color.red);
label[q][j].setFont (new Font ("sansserif", Font.BOLD, 14));
label[q][j].setLabel ("<|");
}
}
}
}
}
最佳答案
您在 start
的末尾调用 MineSweeper(width, height)
(应该是小写,因为它是一个方法),您在 selectDifficulty< 中调用它
.
MineSweeper
会创建新的所有组件。 optionsButton
不是新创建的,因此您向其添加另一个 ActionListener
。当您按下按钮时,每个 ActionListener
都会被调用,因此 selectDifficulty
被调用的次数越来越多。
最简单的解决方法是将此 ActionListener
添加到 MineSweeperGUI
的构造函数中,但您应该重构源代码,尽可能只创建一次所有组件。至少整个菜单应该转到构造函数。
关于java - 单击“确定”按钮后再次出现消息对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2005757/
我正在使用 Selenium Web 驱动程序 3.0,并且想要从打开的两个对话框(一个在后台,第二个在前台)的 Activity 对话框中单击“确定”按钮。如何从 html 下面的父 div 单击前
actions: [ FlatButton( onPressed: () {
我有一个问题有点超出我的范围(我真的很高兴我是 Beta)涉及重复项(所以 GROUP BY, HAVING, COUNT),通过将解决方案保留在 SQLite 附带的标准函数中而变得更加复杂。我正在
使用DBI是否可以确定SELECT语句的已执行语句句柄是否返回任何行而不从中获取行? IE。就像是: use DBI; ... my $sth = $dbh->prepare("SELECT ..."
是否可以为“确定”和“关闭”按钮指定回调函数? 如果是JQuery Modal,则可以在初始化时使用按钮字典指定回调函数。 Semantic-ui模态是否提供类似的功能?按下确定后,我该如何寻求其他逻
我想阅读警报中的消息。 示例:如果警报显示“错误的电子邮件地址”。怎么读呢?意味着我想将该消息存储在字符串中。 如何在“警报”中单击“确定”...?? 如何使用 Selenium 来做到这一点? 最佳
我有一个删除按钮: 我试图首先查明是否已选择一个网站,如果已选择一个网站,我需要确定是否已选择一个或多个列表项,如果是,则继续删除这些项目。 我的 if 语句不断返回“您必须首先选择您的列表”,即使它
部分出于好奇——我们想知道在我们的应用程序中发生了什么——部分是因为我们需要在我们的代码中找到一些潜在的问题,我喜欢在我们的网络应用程序运行时跟踪一些一般值。这尤其包括某些对象图的分配内存。 我们的应
我将 SweetAlert 与 Symfony 结合使用,我希望用户在完成删除操作之前进行确认。 发生的情况是,当用户单击删除按钮时,SweetAlert 会弹出,然后立即消失,并且该项目被删除。 在
我们有一个应用程序可以生成不包括字母 O 的随机基数 35 [0-9A-Z]。我正在寻找一种解决方案来查找包含任何淫秽英语单词的代码,而无需搜索包含 10,000 个条目的列表每个生成的代码。每秒生成
这是我做的: #include #include int betweenArray(int a, int b){ int *arr,i,range; range = b - a +
我知道如何创建 警报和确认框,但我不知道如何做的是实际单击“确定”。我有一个弹出确认框的页面。 我想使用 Java Script 插件单击“确定”。基本上,我希望我的代码单击页面上的链接,然后在出现提
代码: swal('Your ORDER has been placed Successfully!!!'); window.location="index.php"; 甜蜜警报工
>>> import re >>> s = "These are the words in a sentence" >>> regex = re.compile('are|words') >>> [m
使用确定的理想散列函数给出随机期望线性时间算法两个数组 A[1..n] 和 B[1..n] 是否不相交,即 A 的元素是否也是 B 的元素。 谁能告诉我如何做到这一点,甚至如何开始考虑它? 最佳答案
我在计算机科学课上有这段代码: int input=15; while (input < n ) { input = input *3;} 这段代码有 log3(n/15) 次循环的上限。我们怎样才能
我有一个允许 2 位玩家玩 TicTacToe 的程序。在每个玩家移动之后,它应该在那个点显示棋盘并返回一个名为 Status 的枚举,显示玩家是否应该继续,如果玩家赢了,还是平局。但是,该算法要么返
给定一个 y 值数组,例如 [-3400, -1000, 500, 1200, 3790],我如何确定“好的”Y 轴标签并将它们放置在网格上? ^ ---(6,000)-|---
假设我有一个检查用户登录的 SQL 语句: SELECT * FROM users WHERE username='test@example.com', password='abc123', expi
teradata中有返回表中哪一列被定义为主索引的命令吗?我没有制作一些我正在处理的表,也没有尝试优化我对这些表的连接。谢谢! 最佳答案 有dbc.IndicesV,其中IndexNumber=1表示
我是一名优秀的程序员,十分优秀!