Java 按下Jbutton后设置Jbutton上的文本
我是Java(以及几乎所有语言)的新手。我正在尝试为任务制作一个扫雷游戏,我还在测试阶段。然而,我被困在如何在按下按钮后将地雷显示为“0”的问题上。我使用2d数组创建了Jbuttons,但我似乎无法将“0”放在我想要的特定按钮上 这是我的代码(请原谅我的丑陋/可能的低效),对于GUI,我真的很无知Java 按下Jbutton后设置Jbutton上的文本,java,user-interface,jbutton,actionlistener,minesweeper,Java,User Interface,Jbutton,Actionlistener,Minesweeper,我是Java(以及几乎所有语言)的新手。我正在尝试为任务制作一个扫雷游戏,我还在测试阶段。然而,我被困在如何在按下按钮后将地雷显示为“0”的问题上。我使用2d数组创建了Jbuttons,但我似乎无法将“0”放在我想要的特定按钮上 这是我的代码(请原谅我的丑陋/可能的低效),对于GUI,我真的很无知 import java.awt.*; import javax.swing.*; import java.awt.GridLayout; import java.awt.event.*; import
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);
}
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.GridLayout;
导入java.awt.event.*;
导入java.awt.geom.*;
导入javax.swing.event.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JOptionPane;
导入javax.swing.JFrame;
公共类网格扩展JPanel{
//初始化行、列和列
int行=10;
int cols=10;
int i,j=0;
int=10;
boolean[][]setmine=新的boolean[rows][cols];
布尔[][]可点击=新布尔[行][cols];
专用JToggleButton方块[],方块2[];
//用于创建网格的构造函数(默认尺寸为400x400
公共电网(){
这个。设置大小(600600);
这个.setLayout(新的GridLayout(行、列));
正方形=新的JToggleButton[行][cols];
buildButtons();
}
私有void buildButtons(){
//循环用于在网格上创建“按钮”。
int minesneed=10;
//构建按钮
// ----------------------------------------
对于(i=0;i0){
int x=(int)Math.floor(Math.random()*行);
int y=(int)Math.floor(Math.random()*cols);
如果(!setmine[x][y]){
setmine[x][y]=真;
我需要——;
}
}
// ----------------------------------------------------------------------------
加上(平方[i][j]);
if(setmine[i][j]==true){
squares[i][j].addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae){
//这就是问题所在
正方形[i][j].setText(“0”);
}
});
}
}
}
}
公共静态void main(字符串[]args){
网格g=新网格();
JFrame=新JFrame(“扫雷舰”);
框架。添加(g);
框架。设置尺寸(600600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我的建议是不要为号码显示一个JButton,而是让您的mine单元格使用CardttLayout,这样它最初会显示一个JButton,但当按下时会显示一个JLabel。我在stackoverflow上有一个这样的例子,我将搜索并通过链接返回给您
以下是链接:
这里有一个链接更详细地解释了我的一些代码:
如果运行该程序,您将看到如下初始屏幕:
按下空白方块后,您将看到所有空白方块的间隙:
在击中炸弹后,你会看到所有的炸弹出现: