编了一个扫雷游戏,点击按钮后,无法周围雷数

wufangli 2010-04-19 12:08:33
public class Jiemian extends JFrame implements ActionListener,MouseListener {
public Icon icon_bomb = new ImageIcon("blue.gif"); //踩雷
public Icon icon_bomb_big = new ImageIcon("blue.gif"); //踩雷标记
public Icon icon_flag = new ImageIcon("blue.gif"); //雷标记
public Icon icon_question = new ImageIcon("blue.gif"); //疑惑是否有雷
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// 取得屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
public Panel MenuPamel = new Panel();
public JButton start = new JButton(" 开始 ");
public JLabel nowBomb,setBomb;
public JTextField text;
public int BlockNum,BombNum;
public Bomb[][] bombButton;
public Panel mainPanel = new Panel();

public Jiemian() {

BlockNum = 400;
BombNum = 50;
nowBomb = new JLabel("当前雷数"+" "+BombNum+"");
setBomb= new JLabel("设置地雷数");
text=new JTextField("10 ",3);
this.setSize(820, 820);
this.setLocation((width - 820) / 2, (height - 820) / 2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);

Container c=getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel,"North");


start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
BombNum = Integer.parseInt(text.getText().trim());
if(BombNum >= 10 && BombNum < 100 )
{ replay();}
else
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(null,"您设置的地雷数太多了,请重设!","错误",2);
}

}
} );
// this.add(setBomb);
mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );
bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++)
{
bombButton[ i ]=new Bomb[ (int)Math.sqrt(BlockNum) ];
}
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++ )
{
bombButton[ i ][ j ]=new Bomb(i,j);
bombButton[ i ][ j ].setForeground( Color.gray);
bombButton[ i ][ j ].addActionListener(this);
bombButton[ i ][ j ].addMouseListener(this);
}
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++ )
mainPanel.add(bombButton[ i ][ j ]);
c.add(mainPanel,"Center");
this.setVisible(true);
startBomb();
}
...全文
225 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wufangli 2010-04-19
  • 打赏
  • 举报
回复
有哪位能帮我看看代码,功能都能实现,就是点击方块后,显示周围雷数的数字显示为“”,经测试数据确实赋值给了按钮的text了,就是在游戏中无法显示。
行舟 2010-04-19
  • 打赏
  • 举报
回复
给你贴个有用的
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyFirstGame
{
private JFrame f;
private Container con;
private JPanel pUp, pDown;
private JMenuItem item1;
private JMenuItem item2;
private JMenuItem exitItem;
private JButton btn;
private MyButton[][] btns;
private JLabel l1;
private JLabel l2;
private Timer timer;
private int sum;
private int rows;
private int cols;
private int temp;
private boolean flag;

public MyFirstGame(){

f = new JFrame("");
con = f.getContentPane();
pUp = new JPanel();
pDown = new JPanel();
con.add(pUp, BorderLayout.NORTH);
con.add(pDown);

//处理Game菜单!!
JMenuBar bar=new JMenuBar();
JMenu menu=new JMenu("Game");
item1=new JRadioButtonMenuItem("Normal");
item2=new JRadioButtonMenuItem("Hard");
exitItem=new JMenuItem("Exit");

MenuActionListener listener = new MenuActionListener();
item1.addActionListener(listener);
item2.addActionListener(listener);
exitItem.addActionListener(listener);

menu.addSeparator();
menu.add(item1);
menu.add(item2);
menu.addSeparator();
menu.add(exitItem);

bar.add(menu);

f.setJMenuBar(bar);

ButtonGroup bg=new ButtonGroup();
bg.add(item1);
bg.add(item2);

reset(9, 9, 10);


timer = new Timer(1000, new TimerActionListener());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

private void reset(int rows, int cols, int sum){
this.rows = rows;
this.cols = cols;
this.sum = sum;
temp = rows*cols-sum;
flag = false;

if(sum==10)
item1.setSelected(true);
if(sum==40)
item2.setSelected(true);


pUp.removeAll();
pUp.setLayout(new GridLayout(1, 3));
l1 = new JLabel(sum + "");
pUp.add(l1);
btn = new JButton("Restart");
btn.addActionListener(new StartActionListener());
pUp.add(btn);

l2 = new JLabel("0");
pUp.add(l2);


pDown.removeAll();
pDown.setLayout(new GridLayout(rows, cols));
btns = new MyButton[rows][cols];

initBtns();

f.pack();
f.validate();
}

private void initBtns(){
double temp = (double)sum/(rows*cols);
int num1 = sum;
int num2 = rows*cols;
for(int i=0; i<btns.length; i++){
for(int j=0; j<btns[i].length; j++){
double random = Math.random();
if(num1==0){
btns[i][j] = new MyButton("ㅂ", i, j);
num2--;
}else if(num1==num2){
btns[i][j] = new MyButton("ㅂ", false, true, i, j);
num1--;
num2--;
}else if(random>temp){
btns[i][j] = new MyButton("ㅂ", i, j);
num2--;
}else if(random<=temp){
btns[i][j] = new MyButton("ㅂ", false, true, i, j);
num1--;
num2--;
}
pDown.add(btns[i][j]);
btns[i][j].addMouseListener(new NormalMouseListener());
}
}
}


class TimerActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
l2.setText(Integer.parseInt(l2.getText())+1+"");
}
};

class MenuActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source=e.getSource();
//get information of the button that be push
if(source==item1)
{
item1.setSelected(true);
//the method setSelected:set up the menu is selected or not!
reset(9,9,10);
}
else if(source==item2)
{
item2.setSelected(true);
reset(16,16,40);
}
else
{
System.exit(0);
}
}
};


class StartActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(item1.isSelected()==true)
reset(9, 9, 10);
else if(item2.isSelected()==true)
reset(16, 16, 40);
}
};


class NormalMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
if(!flag){
timer.start();
flag = true;
}

MyButton btn = (MyButton)e.getSource();

if(btn.isEnabled()==false)
return;

if(e.getButton()==MouseEvent.BUTTON1/*鼠标左键*/){
if(btn.getText().equals("*"))
l1.setText(Integer.parseInt(l1.getText())+1 + "");

if(btn.getB2()==true){
l2.setText("Game over ,You lose!!!!");
showAll();
}
else{
showLeave(btn.getRow(), btn.getCol());
}
}else if(e.getButton()==MouseEvent.BUTTON3/*鼠标右键*/){
if(btn.getText().equals("*")){
btn.setText("ㅂ");
l1.setText(Integer.parseInt(l1.getText())+1 + "");
}else{
btn.setText("*");
l1.setText(Integer.parseInt(l1.getText())-1 + "");
}
}
f.validate();
}
//显示所有MyButton信息
private void showAll()
{
timer.stop();
for(int i=0; i<btns.length; i++){
for(int j=0; j<btns[i].length; j++){
if(btns[i][j].getB2()==true)
btns[i][j].setText("*");
else{
int num = check(i, j);
if(num==0)
btns[i][j].setText("");
else
btns[i][j].setText(""+num);
}
btns[i][j].setEnabled(false);
}
}
}

//显示点击MyButton应显示的信息!
private void showLeave(int row, int col)
{

int num = check(row, col);

btns[row][col].setB1(true);
if(--temp == 0)
{
l2.setText("you win take " + l2.getText() + "seconds!");
showAll();
}
btns[row][col].setEnabled(false);

if(num>0)
btns[row][col].setText(""+num);
else{
btns[row][col].setText("");

if(row-1>=0 && col-1>=0 && btns[row-1][col-1].getB1()==false){
showLeave(row-1,col-1);
}
if(row-1>=0 && btns[row-1][col].getB1()==false){
showLeave(row-1, col);
}
if(row-1>=0 && col+1<cols && btns[row-1][col+1].getB1()==false){
showLeave(row-1, col+1);
}
if(col-1>=0 && btns[row][col-1].getB1()==false){
showLeave(row, col-1);
}
if(col+1<cols && btns[row][col+1].getB1()==false){
showLeave(row, col+1);
}
if(row+1<rows && col-1>=0 && btns[row+1][col-1].getB1()==false){
showLeave(row+1, col-1);
}
if(row+1<rows && btns[row+1][col].getB1()==false){
showLeave(row+1, col);
}
if(row+1<rows && col+1<cols && btns[row+1][col+1].getB1()==false){
showLeave(row+1, col+1);
}
}
}
//检查周围地雷数量
private int check(int row, int col)
{
int count=0;
if(row-1>=0 && col-1>=0 && btns[row-1][col-1].getB2()==true)
count++;
if(row-1>=0 && btns[row-1][col].getB2()==true)
count++;
if(row-1>=0 && col+1<cols && btns[row-1][col+1].getB2()==true)
count++;
if(col-1>=0 && btns[row][col-1].getB2()==true)
count++;
if(col+1<cols && btns[row][col+1].getB2()==true)
count++;
if(row+1<rows && col-1>=0 && btns[row+1][col-1].getB2()==true)
count++;
if(row+1<rows && btns[row+1][col].getB2()==true)
count++;
if(row+1<rows && col+1<cols && btns[row+1][col+1].getB2()==true)
count++;
return count;
}
};

public static void main(String[] args)
{
new MyFirstGame();
}
}
/************************************************************************************/
import javax.swing.*;

public class MyButton extends JButton
{
private boolean b1;
private boolean b2;
private int row;
private int col;

public MyButton(String text, boolean b1, boolean b2, int row, int col)
{
setText(text);
this.b1 = b1;
this.b2 = b2;
this.col = col;
this.row = row;
}
public MyButton(int row, int col)
{
this("", false, false, row, col);
}
public MyButton(String text, int row, int col)
{
this(text, false, false, row, col);
}

public void setB1(boolean b1)
{
this.b1=b1;
}

public void setB2(boolean b2)
{
this.b2=b2;
}

public boolean getB1()
{
return b1;
}

public boolean getB2()
{
return b2;
}

public int getRow(){
return row;
}

public int getCol(){
return col;
}

}

WKY_198642 2010-04-19
  • 打赏
  • 举报
回复
帮忙顶下
wufangli 2010-04-19
  • 打赏
  • 举报
回复
import javax.swing.JButton;

public class Bomb extends JButton {

public int num_x,num_y; //第几号方块
public int BombRoundCount; //周围雷数
public boolean isBomb; //是否为雷
public boolean isClicked; //是否被点击
public int BombFlag; //探雷标记
public boolean isRight; //是否点击右键

public Bomb(int x,int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
}

}
wufangli 2010-04-19
  • 打赏
  • 举报
回复
public void actionPerformed(ActionEvent e)
{

CountRoundBomb();//计算方块周围雷数

if(((Bomb)e.getSource()).isBomb==false && ((Bomb)e.getSource()).isClicked == false)
{
// ((Bomb)e.getSource()).setText(( ((Bomb)e.getSource()).BombRoundCount )+"");
String ffh;
ffh =String.valueOf (( ((Bomb)e.getSource()).BombRoundCount ));
((Bomb)e.getSource()).setText(ffh);
// String ddd=((Bomb)e.getSource()).getText();
((Bomb)e.getSource()).isClicked=true;
((Bomb)e.getSource()).setIcon(null);
System.out.print(ffh);
((Bomb)e.getSource()).setEnabled(false);

if((((Bomb)e.getSource()).BombRoundCount) == 0)
isNull(bombButton,(Bomb)e.getSource());
isWin();
}
if (((Bomb)e.getSource()).isBomb == true)
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);
replay();



}
}
public void isWin()
{
int findBomb=0; //找到的地雷数
for(int i = 0;i < (int)Math.sqrt(BlockNum) ; i++)

for(int j = 0;j < (int)Math.sqrt(BlockNum ); j++)
{
if(bombButton[ i ][ j ].isBomb == true && bombButton[ i ][ j ].isRight == true)
findBomb++;
}
if( findBomb == Integer.parseInt(text.getText().trim()) )
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",2);
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

Bomb bombSource = (Bomb)e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
String ff= bombSource.getname();
if((right == true)){if(ff==""){
bombSource.setname("!!");

System.out.print(ff);
}else{ bombSource.setname("");}
};
}

public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}
public void mousePressed(MouseEvent e) {

}

}
wufangli 2010-04-19
  • 打赏
  • 举报
回复
public void replay()
{
BombNum=Integer.parseInt(text.getText().trim());
nowBomb.setText("当前雷数"+" "+BombNum+"");
for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++)
for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++)
{
bombButton[ i ][ j ].isBomb=false;
bombButton[ i ][ j ].isClicked=false;
bombButton[ i ][ j ].setEnabled(true);
bombButton[ i ][ j ].setText("");
bombButton[ i ][ j ].setIcon(null);
}
startBomb();
}
public void startBomb()
{

for(int i=0;i<BombNum;i++)
{
int x =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));
int y =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));

if(bombButton[ x ][ y ].isBomb==true)
i--;//再重新来一次.呵呵
else
bombButton[ x ][ y ].isBomb=true ;
}
}
public void CountRoundBomb()
{
for (int i = 0; i < (int)Math.sqrt(BlockNum); i++) {
for (int j = 0; j < (int)Math.sqrt(BlockNum); j++) {
int count = 0;
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[ i ][ j ].isBomb != true) {
if ( (i - 1 >= 0) && (j - 1 >= 0)) {
if (bombButton[i - 1][j - 1].isBomb == true) {
count += 1; //检测左上方空格是否是地雷
}
}
if ( (i - 1 >= 0)) {
if (bombButton[i - 1][ j ].isBomb == true) {
count += 1; //检测上方空格是否为地雷
}
}
if ( (i - 1 >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i - 1][j + 1] .isBomb == true) {
count += 1; //检测右上方是否为地雷
}
}
if ( (j - 1 >= 0)) {
if (bombButton[ i ][j - 1] .isBomb == true) {
count += 1; //检测左边是否为地雷
}
}
if ( (i >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[ i ][j + 1].isBomb == true) {
count += 1; //右边
}
}
if ( (j - 1 >= 0) && (i + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j - 1].isBomb == true) {
count += 1; //左下
}
}
if ( (i + 1 <= (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][ j ].isBomb == true) {
count += 1; //下
}
if ( (j + 1 <= (int)Math.sqrt(BlockNum)-1) && (i + 1 <= Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j + 1].isBomb == true) {
count += 1; //右下
}
}
bombButton[ i ][ j ].BombRoundCount = count;
}
}
}
}
}
public void isNull(Bomb[][] bombButton,Bomb ClickecButton)
{
int i,j;
i=ClickecButton.num_x;
j=ClickecButton.num_y;

if (ClickecButton.isBomb==true) {

}
else {

if ( (i - 1 >= 0) && (j - 1 >= 0)) { //检测左上方空格是否是空
if (bombButton[i - 1][j - 1].isBomb == false && bombButton[i - 1][j - 1].isClicked == false && bombButton[i - 1][j - 1].isRight == false) {
bombButton[i - 1][j - 1].setText((bombButton[i - 1][j - 1].BombRoundCount)+"");
bombButton[i - 1][j - 1].setEnabled(false);
bombButton[i - 1][j - 1].isClicked=true;
}
}

if ( (i - 1 >= 0)) { //检测上方空格是否为空
if (bombButton[i - 1][ j ] .isBomb == false && bombButton[i - 1][ j ].isClicked == false && bombButton[i - 1][ j ].isRight == false) {
bombButton[i - 1][ j ].setText((bombButton[i - 1][ j ].BombRoundCount)+"");
bombButton[i - 1][ j ].setEnabled(false);
bombButton[i - 1][ j ].isClicked=true;
}
}
if ( (i - 1 >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右上方是否为空
if (bombButton[i - 1][j + 1] .isBomb == false && bombButton[i - 1][j + 1].isClicked == false && bombButton[i - 1][j + 1].isRight == false) {
bombButton[i - 1][j + 1].setText((bombButton[i - 1][j + 1].BombRoundCount)+"");
bombButton[i - 1][j + 1].setEnabled(false);
bombButton[i - 1][j + 1].isClicked=true;
}

}
if ( (j - 1 >= 0)) { //检测左边是否为空
if (bombButton[ i ][j - 1].isBomb == false && bombButton[ i ][j - 1].isClicked == false && bombButton[ i ][j - 1].isRight == false) {
bombButton[ i ][j - 1].setText((bombButton[ i ][j - 1].BombRoundCount)+"");
bombButton[ i ][j - 1].setEnabled(false);
bombButton[ i ][j - 1].isClicked=true;
}

}
if ( (i >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右边空格是否是空
if (bombButton[ i ][j + 1].isBomb == false && bombButton[ i ][j + 1].isClicked == false && bombButton[ i ][j + 1].isRight == false) {
bombButton[ i ][j + 1].setText((bombButton[ i ][j + 1].BombRoundCount)+"");
bombButton[ i ][j + 1].setEnabled(false);
bombButton[ i ][j + 1].isClicked=true;
}
}
if ( (j - 1 >= 0) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测左下空格是否是空
if (bombButton[i + 1][j - 1].isBomb == false && bombButton[i + 1][j - 1].isClicked == false && bombButton[i + 1][j - 1].isRight == false) {
bombButton[i + 1][j - 1].setText((bombButton[i + 1][j - 1].BombRoundCount)+"");
bombButton[i + 1][j - 1].setEnabled(false);
bombButton[i + 1][j - 1].isClicked=true;
}
}
if ( (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测下边空格是否是空
if (bombButton[i + 1][ j ].isBomb == false && bombButton[i + 1][ j ].isClicked == false && bombButton[i + 1][ j ].isRight == false) {
bombButton[i + 1][ j ].setText((bombButton[i + 1][ j ].BombRoundCount)+"");
bombButton[i + 1][ j ].setEnabled(false);
bombButton[i + 1][ j ].isClicked=true;
}
}
if ( (j + 1 <= ((int)Math.sqrt(BlockNum)-1) ) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) { //检测右下边空格是否是空
if (bombButton[i + 1][j + 1].isBomb == false && bombButton[i + 1][j + 1].isClicked == false && bombButton[i + 1][j + 1].isRight == false) {
bombButton[i + 1][j + 1].setText((bombButton[i + 1][j + 1].BombRoundCount)+"");
bombButton[i + 1][j + 1].setEnabled(false);
bombButton[i + 1][j + 1].isClicked=true;
}
}
if ( (i - 1 >= 0) && (j - 1 >= 0))//检测左上
isNull(bombButton,bombButton[i - 1][j - 1]);
if ( (i - 1 >= 0))
isNull( bombButton,bombButton[i - 1][ j ]);//检测上方
if ( (i - 1 >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1))
isNull( bombButton,bombButton[i - 1][j + 1]);//检测右上
if ( (j - 1 >= 0))
isNull(bombButton,bombButton[i][j - 1]);//检测左边
if ( (i >= 0) && (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i][j + 1]);//检测右边
if ( (j - 1 >= 0) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i + 1][j - 1]); //检测左下
if ( (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) //检测下
isNull(bombButton,bombButton[i + 1][ j ]);
if ( (j + 1 <= ((int)Math.sqrt(BlockNum)-1)) && (i + 1 <= ((int)Math.sqrt(BlockNum)-1)) ) //检测右下
isNull(bombButton,bombButton[i + 1][j + 1]);

}
}
wufangli 2010-04-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sjkof 的回复:]
引用 6 楼 wufangli 的回复:
有哪位能帮我看看代码,功能都能实现,就是点击方块后,显示周围雷数的数字显示为“”,经测试数据确实赋值给了按钮的text了,就是在游戏中无法显示。


jbutton的大小是不是不够,要大小足够才能显示
[/Quote]真让你说对了 我把frame稍微放大后,就都能显示出来了,哎弄了一晚上,都没看出来,一直以为是语法问题。
sjkof 2010-04-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wufangli 的回复:]
有哪位能帮我看看代码,功能都能实现,就是点击方块后,显示周围雷数的数字显示为“”,经测试数据确实赋值给了按钮的text了,就是在游戏中无法显示。
[/Quote]

jbutton的大小是不是不够,要大小足够才能显示
sjkof 2010-04-19
  • 打赏
  • 举报
回复
死循环了..., 检测函数有问题.
比如,检测一个button之后,会去检测它左边的button,然后它左边的又会检测它右边的(即自己)

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧