java joptionpane调整大小_计算机毕业设计中用Java实现简单的扫雷小程序

ef1ec64dc671ae1e23a464d48d0dfc19.png点击上方“蓝字”,关注我们.

初学Java的同学可以尝试自己操作下Java小程序

这样子才能提高自己的理解能力和编程水平

不用多说了,直接上代码吧!

具体代码操作如下:

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.*; public class saolei implements ActionListener { JFrame frame=new JFrame("扫雷游戏"); JButton reset=new JButton("重来"); Container container=new Container(); //游戏数据结构 final int row=20; final int col=20; final int leiCount=30; JButton [][] buttons=new JButton[row][col]; int [][] counts=new int[row][col]; final int LEICODE=10; // 构造函数 public saolei(){ //1、设置窗口 frame.setSize(900, 800); frame.setResizable(true);//是否可改变窗口大小 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); //2、添加重来按钮 addResetButton(); //添加按钮 addButtons(); //埋雷 addLei(); //添加雷的计算 calcNeiboLei(); frame.setVisible(true); } public void addResetButton(){ reset.setBackground(Color.green); reset.setOpaque(true); reset.addActionListener(this); frame.add(reset,BorderLayout.NORTH); } public void addLei(){ Random rand=new Random(); int randRow,randCol; for(int i=0;i randRow=rand.nextInt(row); randCol=rand.nextInt(col); if(counts[randRow][randCol]== LEICODE){ i--; }else{ counts[randRow][randCol]=LEICODE; //buttons[randRow][randCol].setText("*"); } } } public void addButtons(){ frame.add(container,BorderLayout.CENTER); container.setLayout(new GridLayout(row,col)); for(int i=0;i for(int j=0;j

JButton button=new JButton(); button.setBackground(Color.yellow); button.setOpaque(true); button.addActionListener(this); buttons[i][j]=button; container.add(button); } } } public void calcNeiboLei(){ int count; for(int i=0;i for(int j=0;j count=0; if(counts[i][j]==LEICODE) continue; if(i>0 && j>0 && counts[i-1][j-1]==LEICODE) count++; if(i>0&&counts[i-1][j]==LEICODE) count++; if(i>0 && j<19 && counts[i-1][j+1]==LEICODE) count++; if(j>0 && counts[i][j-1]==LEICODE) count++; if(j<19 && counts[i][j+1]==LEICODE) count++; if(i<19&&j>0&&counts[i+1][j-1]==LEICODE) count++; if(i<19&&counts[i+1][j]==LEICODE) count++; if(i<19&&j<19&&counts[i+1][j+1]==LEICODE) count++; counts[i][j]=count; //buttons[i][j].setText(counts[i][j]+""); } } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JButton button=(JButton)e.getSource(); if(button.equals(reset)){ for(int i=0;i for(int j=0;j buttons[i][j].setText(""); buttons[i][j].setEnabled(true); buttons[i][j].setBackground(Color.yellow); counts[i][j]=0; } } addLei(); calcNeiboLei(); }else{ int count=0; for(int i=0;i for(int j=0;j if(button.equals(buttons[i][j])){ count=counts[i][j]; if(count==LEICODE){ LoseGame(); }else{ openCell(i,j); checkWin(); } return; } } } }  } void checkWin(){ for(int i=0;i for(int j=0;j if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!"); } void openCell(int i,int j){ if(buttons[i][j].isEnabled()==false) return; buttons[i][j].setEnabled(false);  if(counts[i][j]==0){ if(i>0 && j>0 && counts[i-1][j-1]!=LEICODE) openCell(i-1, j-1); if(i>0&&counts[i-1][j]!=LEICODE) openCell(i-1, j); if(i>0 && j<19 && counts[i-1][j+1]!=LEICODE) openCell(i-1, j+1); if(j>0 && counts[i][j-1]!=LEICODE) openCell(i, j-1); if(j<19 && counts[i][j+1]!=LEICODE) openCell(i, j+1); if(i<19&&j>0&&counts[i+1][j-1]!=LEICODE) openCell(i+1, j-1); if(i<19&&counts[i+1][j]!=LEICODE) openCell(i+1, j); if(i<19&&j<19&&counts[i+1][j+1]!=LEICODE) openCell(i+1, j+1); buttons[i][j].setText(counts[i][j]+""); }else{ buttons[i][j].setText(counts[i][j]+""); } } void LoseGame(){ for(int i=0;i for(int j=0;j int count=counts[i][j]; if(count==LEICODE){ buttons[i][j].setText("X"); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); }else{ buttons[i][j].setText(count+""); buttons[i][j].setEnabled(false);   } } } } public static void main(String[] args) { saolei lei=new saolei(); }

}

2fa6a7e9c5750a8864f1c95bd118de41.png2fa6a7e9c5750a8864f1c95bd118de41.png

be8c30f80eb459f087e27a0f5b0a3c58.gif

更多请关注小编继续了解,免费赠送网页设计作业源代码哦!

如需要精品网页设计作业和毕业设计作品

联系QQ:2656895362进行咨询

99bad8c677fb5f4f656112914ddc1deb.png 18591b2ba6ab4a51d0dad3a15c4da306.png 网站地址: http://www.85work.com/ 扫码关注最新动态更多精彩,点击下方“
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值