有没有java大神帮我看看这个代码,给我加加注释呀,真心求助

昔青今燕 2018-11-29 01:46:40
package 大实验; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random;    import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;    public class SaoLei implements MouseListener,ActionListener{  JPanel p=new JPanel();  JFrame frame = new JFrame("扫雷");  @SuppressWarnings("rawtypes")  JComboBox<String> combobox = new JComboBox<String>();  JButton reset = new JButton("重新开始");  Container container = new Container();     //游戏数据结构  SaoLeiConstant constant = new SaoLeiConstant();  JButton[][] buttons = new JButton[constant.row][constant.col];//定义按钮  int[][] counts = new int [constant.row][constant.col];//定义整型数组保存按钮下方雷数     //创建构造方法  public SaoLei() {  //显示窗口  frame.setSize(600,700);//600*700  frame.setResizable(false);  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.setLayout(new BorderLayout());     //添加重来、选择难度按钮  addtopButton();     //添加雷区按钮  addButtons();     //埋雷  addLei();     //添加雷的计数  calcNeiboLei();     frame.setVisible(true);  }     void addtopButton() {  p.removeAll();  p.add(reset);  reset.setBackground(Color.green);  reset.setOpaque(true);  reset.addActionListener(this);  //combobox.addItem("选择难度");  combobox.addItem("新手难度");  combobox.addItem("初级难度");  combobox.addItem("中级难度");  combobox.addItem("高级难度");  combobox.addItem("大师难度");  combobox.setBackground(Color.GREEN);  combobox.setOpaque(true);  combobox.addItemListener(new ItemListener(){     @Override  public void itemStateChanged(ItemEvent e) {  String item = e.getItem().toString();   if(item == "新手难度") {   constant.leiCount = 20;   ResetGame();  } else if(item == "初级难度") {   constant.leiCount = 43;   ResetGame();  } else if(item == "中级难度"){   constant.leiCount = 63;   ResetGame();  } else if(item == "高级难度"){   constant.leiCount = 99;   ResetGame();  } else if(item == "大师难度") {   constant.leiCount = 119;   ResetGame();  }     }     });  p.add(combobox);  frame.add(p,BorderLayout.NORTH);  //p.add(new Label("总雷数:"+constant.leiCount,Label.CENTER));  //p.add(new Label("总雷数:"+constant.leiCount,Label.RIGHT));     }        /*  void addnanduButton() {  nandu.setBackground(Color.green);  nandu.setOpaque(true);  nandu.addActionListener(this);  frame.add(nandu,BorderLayout.WEST);  }     void addResetButton() {  reset.setBackground(Color.green);  reset.setOpaque(true);  reset.addActionListener(this);  //reset.addMouseListener(this);  frame.add(reset,BorderLayout.NORTH);   }  */     void addLei() {  Random rand = new Random();  int randRow,randCol;  for(int i=0; i<constant.leiCount; i++) {  randRow = rand.nextInt(constant.row);  randCol = rand.nextInt(constant.col);  if(counts[randRow][randCol] == constant.LEICODE) {  i--;  } else {  counts[randRow][randCol] = constant.LEICODE;  //buttons[randRow][randCol].setText("X");  }  }  }     void addButtons() {  frame.add(container,BorderLayout.CENTER);  container.setLayout(new GridLayout(constant.row,constant.col));  for(int i=0;i<constant.row;i++) {  for(int j=0;j<constant.col;j++) {  JButton button = new JButton();  button.setBackground(Color.white);  button.setOpaque(true);  button.addActionListener(this);  button.addMouseListener((MouseListener) this);  buttons[i][j] = button;  container.add(button);  }  }  }     void calcNeiboLei() {  int count;  for(int i=0;i<constant.row;i++) {  for(int j=0;j<constant.col;j++) {  count =0;  if(counts[i][j] == constant.LEICODE) continue;  if(i>0 && j>0 && counts[i-1][j-1] == constant.LEICODE) count++;  if(i>0 && counts[i-1][j] == constant.LEICODE) count++;  if(i>0 && j<19 &&counts[i-1][j+1] == constant.LEICODE) count++;   if(j>0 && counts[i][j-1] == constant.LEICODE) count++;  if(j<19 && counts[i][j+1] == constant.LEICODE) count++;  if(i<19 && j>0 && counts[i+1][j-1] == constant.LEICODE) count++;  if(i<19 && counts[i+1][j] == constant.LEICODE) count++;  if(i<19 && j<19 && counts[i+1][j+1] == constant.LEICODE) count++;  counts[i][j] = count;  buttons[i][j].setMargin(new Insets(0,0,0,0));//让按钮随按钮上的图案变化  //buttons[i][j].setText(counts[i][j] + "");  }  }  }     @Override  public void actionPerformed(ActionEvent e) {     JButton button = (JButton)e.getSource();  if(button.equals(reset)) {  ResetGame();//重新开始游戏  } else {  int count = 0;  for(int i=0;i<constant.row;i++) {  for(int j=0;j<constant.col;j++) {   if(button.equals(buttons[i][j])) {   count = counts[i][j];   if(count == constant.LEICODE) {   loseGame();   } else {   openCell(i,j);    checkWin();   }   return;   }   }  }  }  }     public void mouseClicked(MouseEvent e) {  JButton button = (JButton)e.getSource();  if (e.getButton() == MouseEvent.BUTTON3) {//判断鼠标右击动作  for(int i=0;i<constant.row;i++) {   for(int j=0;j<constant.col;j++) {   if(button.equals(buttons[i][j])) {   if((buttons[i][j].isEnabled() == true)) {   //buttons[i][j].setEnabled(false);   buttons[i][j].setMargin(new Insets(0,0,0,0));//让按钮随按钮上的图案变化   buttons[i][j].setText("?");   return;   }    }   }  }   }  }     void ResetGame() {  for(int i=0;i<constant.row;i++) {  for(int j=0;j<constant.col;j++) {  buttons[i][j].setText("");  buttons[i][j].setEnabled(true);  buttons[i][j].setBackground(Color.white);  counts[i][j] = 0;   }   }  addLei();  calcNeiboLei();   }     void checkWin() {  for(int i=0;i<constant.row;i++) {  for(int j=0;j<constant.col;j++) {  if(buttons[i][j].isEnabled() == true && counts[i][j] != constant.LEICODE ) return;   }  }  JOptionPane.showMessageDialog(frame,"Yeah,你赢了!");   }     //使用递归方法打开格子  void openCell(int i, int j) {  if(buttons[i][j].isEnabled() == false) return;  buttons[i][j].setBackground(Color.yellow);  buttons[i][j].setOpaque(true);  buttons[i][j].setEnabled(false);  if(counts[i][j] == 0) {  if(i>0 && j>0 && counts[i-1][j-1] != constant.LEICODE) openCell(i-1,j-1);  if(i>0 && j<19 && counts[i-1][j] != constant.LEICODE) openCell(i-1,j);  if(i>0 && j<19 &&counts[i-1][j+1] != constant.LEICODE) openCell(i-1,j+1);   if(j>0 && counts[i][j-1] != constant.LEICODE) openCell(i,j-1);  if(j<19 && counts[i][j+1] != constant.LEICODE) openCell(i,j+1);  if(i<19 && j>0 && counts[i+1][j-1] != constant.LEICODE) openCell(i+1,j-1);  if(i<19 && counts[i+1][j] != constant.LEICODE) openCell(i+1,j);  if(i<19 && j<19 && counts[i+1][j+1] != constant.LEICODE) openCell(i+1,j+1);  } else {  buttons[i][j].setMargin(new Insets(0,0,0,0));  buttons[i][j].setText(counts[i][j] + "");  }  }     void loseGame() {  for(int i=0;i<constant.row;i++) {  for(int j=0;j<constant.col;j++) {  int count = counts[i][j];  if(count == constant.LEICODE) {   buttons[i][j].setMargin(new Insets(0,0,0,0));   buttons[i][j].setText("雷");   buttons[i][j].setBackground(Color.red);   buttons[i][j].setEnabled(false);  } else {   buttons[i][j].setMargin(new Insets(0,0,0,0));   buttons[i][j].setText(count + "");   buttons[i][j].setEnabled(false);      }  }  }  JOptionPane.showMessageDialog(frame,"error,你输了!");  }     public static void main(String[] args) {  new SaoLei();  }     @Override  public void mousePressed(MouseEvent e) {  // TODO Auto-generated method stub     }     @Override  public void mouseReleased(MouseEvent e) {  // TODO Auto-generated method stub     }     @Override  public void mouseEntered(MouseEvent e) {  // TODO Auto-generated method stub     }     @Override  public void mouseExited(MouseEvent e) {  // TODO Auto-generated method stub     } } package 大实验; public class SaoLeiConstant {  final int row = 20;//行数30  final int col = 20;//列数30  final int LEICODE = 10;//定义雷下方的数字  protected int temp = 20;  protected int leiCount = temp;//雷数30 }
...全文
33 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
昔青今燕 2018-11-29
  • 打赏
  • 举报
回复
这是一个java的扫雷小游戏
主要特性Java 语言是简单的:Java 语言的语法与 C 语言和 C++ 语言很接近,使得大多数程序员很容易学习和使用。另一方面,Java 丢弃了 C++ 中很少使用的、很难理解的、令人迷惑的那些特性,如操作符重载、多继承、自动的强制类型转换。特别地,Java 语言不使用指针,而是引用。并提供了自动分配和回收内存空间,使得程序员不必为内存管理而担忧。Java 语言是面向对象的:Java 语言提供类、接口和继承等面向对象的特性,为了简单起见,只支持类之间的单继承,但支持接口之间的多继承,并支持类与接口之间的实现机制(关键字为 implements)。Java 语言全面支持动态绑定,而 C++语言只对虚函数使用动态绑定。总之,Java语言是一个纯的面向对象程序设计语言。Java语言是分布式的:Java 语言支持 Internet 应用的开发,在基本的 Java 应用编程接口中有一个网络应用编程接口(java net),它提供了用于网络应用编程的类库,包括 URL、URLConnection、Socket、ServerSocket 等。Java 的 RMI(远程方法激活)机制也是开发分布式应用的重要手段。Java 语言是健壮的:Java 的强类型机制、异常处理、垃圾的自动收集等是 Java 程序健壮性的重要保证。对指针的丢弃是 Java 的明智选择。Java 的安全检查机制使得 Java 更具健壮性。Java语言是安全的:Java通常被用在网络环境中,为此,Java 提供了一个安全机制以防恶意代码的攻击。除了Java 语言具有的许多安全特性以外,Java 对通过网络下载的类具有一个安全防范机制(类 ClassLoader),如分配不同的名字空间以防替代本地的同名类、字节代码检查,并提供安全管理机制(类 SecurityManager)让 Java 应用设置安全哨兵。Java 语言是体系结构中立的:Java 程序(后缀为 java 的文件)在 Java 平台上被编译为体系结构中立的字节码格式(后缀为 class 的文件),然后可以在实现这个 Java 平台的任何系统中运行。这种途径适合于异构的网络环境和软件的分发。Java 语言是可移植的:这种可移植性来源于体系结构中立性,另外,Java 还严格规定了各个基本数据类型的长度。Java 系统本身也具有很强的可移植性,Java 编译器是用 Java 实现的,Java 的运行环境是用 ANSI C 实现的。Java 语言是解释型的:如前所述,Java 程序在 Java 平台上被编译为字节码格式,然后可以在实现这个 Java 平台的任何系统中运行。在运行时,Java 平台中的 Java 解释器对这些字节码进行解释执行,执行过程中需要的类在联接阶段被载入到运行环境中。Java 是高性能的:与那些解释型的高级脚本语言相比,Java 的确是高性能的。事实上,Java 的运行速度随着 JIT(Just-In-Time)编译器技术的发展越来越接近于 C++。Java 语言是多线程的:在 Java 语言中,线程是一种特殊的对象,它必须由 Thread 类或其子(孙)类来创建。通常有两种方法来创建线程:其一,使用型构为 Thread(Runnable) 的构造子类将一个实现了 Runnable 接口的对象包装成一个线程,其二,从 Thread 类派生出子类并重写 run 方法,使用该子类创建的对象即为线程。值得注意的是 Thread 类已经实现了 Runnable 接口,因此,任何一个线程均有它的 run 方法,而 run 方法中包含了线程所要运行的代码。线程的活动由一组方法来控制。Java 语言支持多个线程的同时执行,并提供多线程之间的同步机制(关键字为 synchronized)。Java 语言是动态的:Java 语言的设计目标之一是适应于动态变化的环境。Java 程序需要的类能够动态地被载入到运行环境,也可以通过网络来载入所需要的类。这也有利于软件的升级。另外,Java 中的类有一个运行时刻的表示,能进行运行时刻的类型检查。

50,549

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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