Hi guys,
First post here, but I've been viewing the forums for a long time. I had to write this program for my Java II Class in school (I'm in highschool). I can't seem to figure out what the problem is here that is keeping me.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Minefield extends JFrame implements ActionListener { JPanel[] aPanel = new JPanel[20]; JButton[] aButton = new JButton[20]; int bomb = (int) (Math.random() * 100) % 20; int counter; JLabel bLab = new JLabel(""); public Minefield() { setTitle("Mine Field"); setSize(500, 500); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(5, 5)); for (int i = 0; i<aPanel.length; i++) { aPanel[i] = new JPanel(new GridLayout(1, 1)); aButton[i] = new JButton(""); aButton[i].setBackground(Color.BLUE); add(aPanel[i]); aPanel[i].add(aButton[i]); aButton[i].addActionListener(this); }//end forloop } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); counter++; for (int i = 0; i<aButton.length; i++) { if(aButton[i] == source) { if(i == bomb); { aButton[i].setBackground(Color.RED); for (int a = 0; i<aButton.length; a++) { if(a != bomb) { aButton[a].setBackground(Color.WHITE); } } } else { aButton[i].setBackground(Color.WHITE); if(counter == 10) { bLab.setText("You win!"); } } } }//end forloop } public static void main(String[] args) { Minefield b1 = new Minefield(); }//END MAIN METHOD }//END CLASS Minefield
Any help would be very appreciated! Thanks in advance