Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Having trouble ending a game

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Default Having trouble ending a game

    I had to make a minefield game for my java class, in which a user must click 10 squares without hitting the mine. I was able to make it so when the mine is clicked, all the other squares change their color and the game ends. However, i need to add a congratulatory message in the NORTH JFrame region when the user clicks 10 squares successfully. How would i go about doing this? I added an if statement for this,
    however it doesnt seem to be working, so i'm assuming i did it wrong.

      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);
      		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("");
      		}//end forloop
      	public void actionPerformed(ActionEvent e)
      		Object source = e.getSource();
      		for (int i = 0; i<aButton.length; i++)
      			if(aButton[i] == source)
      				if(i == bomb)
      					for (int a = 0; i<aButton.length; a++)
      						if(a != bomb)
      					if(counter == 10)
      						bLab.setText("You win!");
      		}//end forloop
      	public static void main(String[] args)
      		Minefield b1 = new Minefield();
    Last edited by wakaxwaka; April 25th, 2012 at 08:17 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,710 Times in 2,660 Posts

    Default Re: Having trouble ending a game

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. [SOLVED] JOptionPane preventing program from ending
    By RichTea in forum AWT / Java Swing
    Replies: 2
    Last Post: February 23rd, 2012, 07:42 AM
  3. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 01:56 AM
  4. program loops never ending
    By Ccogh05 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 24th, 2011, 01:42 AM
  5. Weird issue with while loop ending/being skipped
    By ang3c0 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 25th, 2009, 12:09 PM

Tags for this Thread