Hi,

Could someone point out ways on how I could reload the frame?

so far, what I've done :

int more = JOptionPane.YES_OPTION;
more = JOptionPane.showConfirmDialog(null,
            "BOOOoooM!!! Would you like to play again?", "", JOptionPane.YES_NO_OPTION);
 
if(more == JOptionPane.YES_OPTION){
	myFrame.setVisible(true);
}
else{
	myFrame.setVisible(false);
}

I'm not sure what I should do to reload "myFrame" again if the user click yes

Thanks

Recommended Answers

All 6 Replies

It doesn't need to be reloaded - it just needs to be reset. How to do that depends upon how you have structured the code that initializes it.

i'm not sure how to reset it
could you give me a simple example that could give me roughly the idea on how to use it?

thanks again

Like I said - it depends on your initialization code. There is not anything like a "reset()" method. You simply set your panels and controls back to whatever state they should have at the beginning of your game. If you post the code for "myFrame" we may be able to offer some more specific advice.

i could make a new method for reset, right?
but then, i have no idea what should i write in it lol

/**
 * Mini Minesweeper
 * Copyright: Copyright (c) 2008
 * @author Noradila Nordin
 * @version 1.0
 */

import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;

public class AnotherSimpleGui2 {
	
JButton myButton;
JButton myButton1;
JButton myButton2;
JButton myButton3;
JButton myButton4;
JButton myButton5;
JButton myButton6;
JButton myButton7;
JButton myButton8;
JFrame myFrame = new JFrame("Boom!");


public static void main(String[] args) {
	JOptionPane.showMessageDialog(null, "Welcome to Mini-MiniSweeper!\n" +
			"The object of the game is to click on all\n" +
			"the squares EXCEPT the one with the bomb.\n" +
			"(There is only one bomb). To choose a square\n" +
			"to display please simply click on the square.", "Message", JOptionPane.INFORMATION_MESSAGE);
	
	
AnotherSimpleGui2 myGui = new AnotherSimpleGui2();
UIManager.put("Button.background", Color.lightGray);
myGui.go();
}

public class ButtonListener implements ActionListener {
	public void actionPerformed(ActionEvent labelEvent) {
	myButton.setBackground(Color.black);

	int more = JOptionPane.YES_OPTION;
		more = JOptionPane.showConfirmDialog(null,
            "BOOOoooM!!! Would you like to play again?", "", JOptionPane.YES_NO_OPTION);
	if(more == JOptionPane.YES_OPTION){
		//myFrame.setVisible(true);
	}
	else{
		myFrame.setVisible(false);
	}
	}
	}
public class ButtonListener1 implements ActionListener {
	public void actionPerformed(ActionEvent colourEvent) {
				myButton1.setBackground(Color.white);
	}
}

public class ButtonListener2 implements ActionListener {
	public void actionPerformed(ActionEvent labelEvent) {
				myButton2.setBackground(Color.white);
	}
}

public class ButtonListener3 implements ActionListener {
	public void actionPerformed(ActionEvent colourEvent) {
				myButton3.setBackground(Color.white);
	}
}
public class ButtonListener4 implements ActionListener {
	public void actionPerformed(ActionEvent labelEvent) {
				myButton4.setBackground(Color.white);
	}
}
public class ButtonListener5 implements ActionListener {
	public void actionPerformed(ActionEvent colourEvent) {
				myButton5.setBackground(Color.white);
	}
}
public class ButtonListener6 implements ActionListener {
	public void actionPerformed(ActionEvent labelEvent) {
				myButton6.setBackground(Color.white);
	}
}
public class ButtonListener7 implements ActionListener {
	public void actionPerformed(ActionEvent colourEvent) {
				myButton7.setBackground(Color.white);
	}
}

public class ButtonListener8 implements ActionListener {
	public void actionPerformed(ActionEvent labelEvent) {
				myButton8.setBackground(Color.white);
	}
}


public void go() {
	myFrame.setLayout(new GridLayout(3, 3));

	myButton = new JButton("1");
	myButton1 = new JButton("2");
	myButton2 = new JButton("3");
	myButton3 = new JButton("4");
	myButton4 = new JButton("5");
	myButton5 = new JButton("6");
	myButton6 = new JButton("7");
	myButton7 = new JButton("8");
	myButton8 = new JButton("9");

	myButton.addActionListener(new ButtonListener());
	myButton1.addActionListener(new ButtonListener1());
	myButton2.addActionListener(new ButtonListener2());
	myButton3.addActionListener(new ButtonListener3());
	myButton4.addActionListener(new ButtonListener4());
	myButton5.addActionListener(new ButtonListener5());
	myButton6.addActionListener(new ButtonListener6());
	myButton7.addActionListener(new ButtonListener7());
	myButton8.addActionListener(new ButtonListener8());
	
	if(Math.random() > 0.0 && Math.random() < 0.1){
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
	}
	else if(Math.random() > 0.1 && Math.random() < 0.2){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
	}
	else if(Math.random() > 0.2 && Math.random() < 0.3){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
		}
	else if(Math.random() > 0.3 && Math.random() < 0.4){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
		}
	else if(Math.random() > 0.4 && Math.random() < 0.5){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
		}
	else if(Math.random() > 0.5 && Math.random() < 0.6){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
		}
	else if(Math.random() > 0.6 && Math.random() < 0.7){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
		}
	else if(Math.random() > 0.7 && Math.random() < 0.8){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton);
		myFrame.getContentPane().add(myButton8);
		}
	else if(Math.random() > 0.8 && Math.random() < 1.0){
		myFrame.getContentPane().add(myButton1);
		myFrame.getContentPane().add(myButton2);
		myFrame.getContentPane().add(myButton3);
		myFrame.getContentPane().add(myButton4);
		myFrame.getContentPane().add(myButton5);
		myFrame.getContentPane().add(myButton6);
		myFrame.getContentPane().add(myButton7);
		myFrame.getContentPane().add(myButton8);
		myFrame.getContentPane().add(myButton);
		}
	
	myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	myFrame.setSize(400, 200);
	myFrame.setLocationRelativeTo(null);
	myFrame.setVisible(true);
	myFrame.setResizable(false);
	}
}

i tried making another method reset which does the same thing as method go

it works but i have to repeat typing the same codes for method reset as in go

is there any other way?

thanks

Just put the common code into it's own method separate from the code that creates and adds the components to the container.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.