Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to close a Jpanel
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Unknown25




PostPosted: Sat Jan 12, 2013 5:46 pm   Post subject: How to close a Jpanel

Hi,

I am making a Java game and I need some advice on how to do something.

Currently I am using 2 different .java files to make the game. I have a main program that extends JFrame and I call the other .java file which extends JPanel.

The game currently works but I want to make another mode for the game and I think it would be better if I made another file for it.

Currently in the main program, what i use to call the Jpanel program is:

Quote:
private Program screen;
screen = new Program ();
getContentPane ().add (screen, BorderLayout.CENTER);


There are many other things too, but it is basically what I posted above that calls the other file.

Basically what I want to know is, how can I get one to close if I would like to launch another gameBoard file. Not sure how to make it so that works.

For example I would have an if statement like

If (something is true)
getContentPane ().add (gameBoard, BorderLayout.CENTER);
else
getContentPane ().add (gameBoard2, BorderLayout.CENTER);

This would also be helpful for the main Menu of the game as well. Because I don't want to add everything in the GameBoard.java file, it will be crowded.

Thanks a lot!

EDIT:

Here is all of the code for the main file. I removed all the useless unneeded things:


Quote:
import java.awt.*;
import javax.swing.*;


public class ProgramMain extends JFrame
{
private Program screen;

public ProgramMain ()
{
setResizable (false);
screen = new Program ();
getContentPane ().add (screen, BorderLayout.CENTER);
}


public static void main (String[] args)
{
GameMain frame = new GameMain ();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.pack ();
frame.setVisible (true);
}
}



Basically right now it is reading the Program file, how about if I have another file and I want it to switch and read that instead. I want to be able to switch between the 2 if possible. And maybe even a 3rd one for main menu.

EDIT:


I actually just found a very nice way of doing this. But the one problem is, this check is only preformed once at the start. Is there a way to make this check happen every time paintComponent is repainted in the gameBoard java program?

Would it be possible to recall the ProgramMain method in the ProgramMain class? I can always stop it from doing the previous stuff all over again like resetting the size, icon and screen. But it will check if the getContentPane needs to be changed to another java file. That is exactly what I am looking for if it is available.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: