2013-11-27 48 views
1

为什么此代码在运行时不会加载按钮?这个问题我知道的很模糊,但我对Java很陌生,一次试图理解很多东西。这段代码是基于我以前成功尝试编写TicTacToe和Connect Four的两个工作。扫雷地雷不会放置或出现

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.applet.Applet; 
import java.awt.event.ActionListener; 

public class Minesweeper extends Applet implements ActionListener { 
    // initializing all data types 

    JButton[] a; // The grid boxes 
    int counter = 1; // Obsolete 
    char[] letter; // Array of mine locations 
    int[] numbers; // Array of numbers; values and locations 
    boolean explode = false; 
    String name1; 
    String name2; 
    int mines; 

    public void init() { 
     // The code below initializes and locates the grid 
     setLayout(new GridLayout(10, 10)); 
     a = new JButton[100]; 
     // The code below fills the grid with buttons that can be clicked 

     for (int counter = 0; counter < 100; counter++) { 
      a[counter] = new JButton(); 
      a[counter].setText(" "); 
      a[counter].setBackground(Color.white); 
      a[counter].addActionListener(this); 
      add(a[counter]); 
     } 

    } 

    public void nit() { 

     numbers = new int[100]; 
     letter = new char[100]; 
     for (counter = 0; counter < 10; counter++) { 
      mines = (int) Math.random() * 100; 
      if (letter[mines] == '*') { 
       counter--; 
      } else { 
       letter[mines] = '*'; 
      } 
     } 

     for (counter = 0; counter < 100; counter++) { 
      numbers[counter] = 0; 
     } 

     for (int search = 0; search < 10; search++) { 
      for (int searchb = 0; searchb < 10; searchb++) { 
       if (letter[search * 10 + searchb] == '*') { 
        if (search != 0) { 
         numbers[((search - 1) * 10) + searchb]++; 
        } 
        if (search != 9) { 
         numbers[((search + 1) * 10) + searchb]++; 
        } 
        if (searchb != 0) { 
         numbers[((search * 10) + searchb) - 1]++; 
        } 
        if (searchb != 9) { 
         numbers[((search * 10) + searchb) + 1]++; 
        } 
        if ((search != 0) && (searchb != 0)) { 
         numbers[(((search - 1) * 10) + searchb) - 1]++; 
        } 
        if ((search != 9) && (searchb != 9)) { 
         numbers[(((search + 1) * 10) + searchb) + 1]++; 
        } 
        if ((search != 0) && (searchb != 9)) { 
         numbers[(((search - 1) * 10) + searchb) + 1]++; 
        } 
        if ((search != 9) && (searchb != 0)) { 
         numbers[(((search + 1) * 10) + searchb) - 1]++; 
        } 

       } 

      } 

     } 
     for (int counter = 0; counter < 100; counter++) { 
      letter[counter] = (char) ('0' + numbers[counter]); 
      JOptionPane.showMessageDialog(null, " " + letter[counter]); 
     } 
    } 

    // ActionEvent e is the click 
    public void actionPerformed(ActionEvent e) { 
     int pickedsquare = 0, coloring, pickedcolumn = 0; 
     JButton b = (JButton) e.getSource(); 
     counter++; 
     b.setText("Test"); 
     for (int f = 0; f < 100; f++) { 
      JOptionPane.showMessageDialog(null, " " + letter[f]); 
      if (a[f].getText() == "Test") { 
       pickedsquare = f; 
       JOptionPane.showMessageDialog(null, " " + letter[f]); 
       name1 = " " + letter[pickedsquare]; 
       a[f].setText(name1); 
       break; 
      } 
     } 
     if (letter[pickedsquare] == '*') 
      explode = true; 

     if (explode == true) { 
      JOptionPane.showMessageDialog(null, "You are dead!"); 
      for (int counterb = 0; counterb <= 99; counterb++) { 
       a[counterb].setEnabled(false); 

      } 
     } 

     if (counter == 89) { 
      JOptionPane.showMessageDialog(null, "You have swept all mines!"); 
      for (int counterb = 0; counterb <= 99; counterb++) { 
       a[counterb].setEnabled(false); 

      } 
     } 
    } 
} 
+0

对,就是名字为我的第二个方法,而不是非常有意义的命名,但如果它被列入第一种方法的Jbuttons中甚至不会出现。不是100%确定为什么,现在试图了解 – user3042985

+0

是你的JButtons出现?我的印象是,他们werent – Cruncher

+0

JButtons现在出现了,我把字母数组移到第二个方法,他们出现了。不知道为什么这有所作为。 现在的问题是,jButton仅仅在点击时在它们上面写有“测试”,仅此而已。 – user3042985

回答

3

由于运营商的操作数被从left to right评价,整数铸造首先出现在该语句

mines = (int) Math.random()*100; 

使所述第一项被转换为0。这会导致counter只要递增就会递减,导致循环自身重复无限。括在括号中的操作数:

mines = (int) (Math.random() * 100); 
+0

实现了代码,没有发现明显的差异,并且发生了相同的问题 – user3042985

+0

您完全从存在问题的'init'方法中删除了循环。现在,答案与 – Reimeus

+0

这个问题的修改后的代码无关。修复了一些其他问题,现在游戏正在运行,非常感谢您的时间 – user3042985