Results 1 to 14 of 14
- 01-23-2012, 12:08 AM #1
Member
- Join Date
- Jan 2012
- Location
- Adelaide, Australia
- Posts
- 17
- Rep Power
- 0
Working with multiple JComponents
Hello, I have been making a game and it works great! I have now been trying to implement a menu by putting the main game in a JComponent, and the menu in a seperate JComponent. In a main class I add them both to the JFrame and do a little test to make only the enabled one visible. It doesnt really work. Please help! Oasis is one JComponent, MainMenu is another (both extend JComponent).
But it only shows a white/ish screen and not the game component. (It should definately draw something because if I only add the game component it works perfectly, but without a menu!)Java Code:public class Main extends JFrame implements GameConfig{ private Oasis gamePanel = new Oasis(); private MainMenu menuPanel = new MainMenu(); private String gamePhase = "runGame"; public Main() { this.setTitle("Oasis"); this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); this.setLocationRelativeTo(null); this.setResizable(false); this.setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { setVisible(false); // hide the window quickly dispose(); // release all system resources System.exit(0); // finally exit. } }); add(gamePanel); add(menuPanel); if (gamePhase.equals("runGame")) { menuPanel.setVisible(false); menuPanel.setEnabled(false); gamePanel.setVisible(true); gamePanel.setEnabled(true); } if (gamePhase.equals("mainMenu")) { gamePanel.setVisible(false); gamePanel.setEnabled(false); menuPanel.setVisible(true); menuPanel.setEnabled(true); } } public static void main(String[] args) { @SuppressWarnings("unused") Main gameFrame = new Main(); } }
Thankyou very much.
-
Re: Working with multiple JComponents
Don't try to fiddle with this yourself when the CardLayout was made just for this situation. So have the contentPane use this layout and swap views when necessary.
- 01-23-2012, 12:20 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Working with multiple JComponents
You add these panels to the frame's content pane which has a border layout. The effect is that menuPanel will replace gamePanel since both can't occupy the center of the content pane.Java Code:add(gamePanel); add(menuPanel);
You could set a different layout manager on the frame's content pane. Or, better I think, create a panel to hold the gamePanel and menuPanel and give this other panel an appropriate layout manager (CardLayout?). Then set this other panel as the content pane of your frame.
See Using toplevel Containers and Laying out Components within a Container in Oracle's Tutorial.
-----
This is a Swing question do I'll move it to the Awt/Swing forum.
- 01-23-2012, 12:24 AM #4
Member
- Join Date
- Jan 2012
- Location
- Adelaide, Australia
- Posts
- 17
- Rep Power
- 0
Re: Working with multiple JComponents
Thankyou for your quick answers and moving the thread, I'll have to check out how to use CardLayout!
- 01-23-2012, 01:11 AM #5
Member
- Join Date
- Jan 2012
- Location
- Adelaide, Australia
- Posts
- 17
- Rep Power
- 0
Re: Working with multiple JComponents
so that works like a treat (Thankyou so much!)
But there is one problem, I cannot access the CardLayout JPanel object from within the two components that it is holding to be able to change which one is being shown.
If that is a bit confusing, in the Main class, I create the JPanel with cardlayout layout. I create two custom JComponent classes (extends jcomponent), and it is within them that I need to be able to access the JPanel within the main class.
How do you think I should do this?
-
Re: Working with multiple JComponents
You could pass a reference from one class into the other. For instance, you could give the top level GUI a public class that allows other classes to swap views, say something like
You could also give this class public static void String constants that hold the name Strings.Java Code:public void swapView(String name) { // cardLayout is a class field that refers to the CardLayout // cardContainer is a class field that refers to the container that uses CardLayout cardLayout.show(cardContainer, name); }
Then pass a reference of the top level gui class to the subclasses so that they can call this method when necessary.
- 01-23-2012, 02:16 AM #7
Member
- Join Date
- Jan 2012
- Location
- Adelaide, Australia
- Posts
- 17
- Rep Power
- 0
-
Re: Working with multiple JComponents
You're already doing this as your main class will of necessity hold other objects, Oasis and MainMenu. If you want to allow objects of these classes to call methods of the main object, you'll need to pass a reference from the main into them, likely through a constructor parameter:
Java Code:public class Main extends JFrame implements GameConfig{ private Oasis gamePanel = new Oasis(this); private MainMenu menuPanel = new MainMenu(this); private String gamePhase = "runGame";Java Code:public class Oasis { private Main main; public Oasis (Main main) { this.main = main; // ... plus other code for the constructor. } // then in an ActionListener for some JButton or menu, you'll call // main.swapView(Main.OTHER_PANEL); }
- 01-23-2012, 03:11 AM #9
Member
- Join Date
- Jan 2012
- Location
- Adelaide, Australia
- Posts
- 17
- Rep Power
- 0
Re: Working with multiple JComponents
Wow, I was never aware you could pass the whole class itself as a parameter. Thankyou so much!!! I appreaciate your help.
-
Re: Working with multiple JComponents
You're welcome! Did it work?
- 01-23-2012, 03:40 AM #11
Member
- Join Date
- Jan 2012
- Location
- Adelaide, Australia
- Posts
- 17
- Rep Power
- 0
Re: Working with multiple JComponents
haha, have not tried it yet, no time!
I will definitely post back here to let you know though.
-
Re: Working with multiple JComponents
OK, please do!
- 01-23-2012, 06:36 AM #13
Member
- Join Date
- Jan 2012
- Location
- Adelaide, Australia
- Posts
- 17
- Rep Power
- 0
Re: Working with multiple JComponents
Worked like a treat, thankyou so much!
-
Similar Threads
-
Working on Payroll Program Part 2 and getting multiple errors
By blondielox in forum New To JavaReplies: 3Last Post: 08-29-2011, 01:54 AM -
HashMaps containing JComponents
By chillin in forum AWT / SwingReplies: 3Last Post: 05-04-2011, 12:05 AM -
JComponents Are Wrong Size
By MrFish in forum AWT / SwingReplies: 2Last Post: 12-21-2010, 03:13 AM -
Multiple JPanels Not Working
By jgezau in forum AWT / SwingReplies: 5Last Post: 04-17-2009, 04:52 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks