• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing JPanel from JFrame and Initialize new JPanel Help

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Team,

I hope you are all well! I have begun making progress on my game and decided it was time to work with GUI. First I will post my code and then I will state my question at the end:

Main Class

Main Frame class used both in menu and ingame

Start Menu Class

In Game Class


So here is my question!

When I load the game, the start menu loads fine, when I click the buttons for test output it all works fine. But my core issue is, when I click new IngameFrame(); to go to the IngameFrame method, it won't load my panels.
Now, I believe this is because I need to initialise the IngameFrame class into the MainFrame class like I did for the TitleScreen class.

If this is the case, could you please aid me in figuring out how I should achieve this? I have attempted (I have commented it out in MainFrame class) in which obviously fails to load because I need something that will call the titlescreen and then place the IngameFrame on hold till it needs to be initialised.
I might be thinking of this in the wrong way, so any assistance would be amazing!! I am still new to java so, please if it's not to much to ask provide examples with what you say, as I am still learning.

Thanks in advanced for all your assistance!

Mark.D
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Team,

I managed to get it to work, but I don't think this is the best idea...?

I created an action listener on MainFrame class that listens for the startButton in that TitleScreen Class. But from memory, I should keep my buttons private?
If that is the case, how do I access buttons from another class?

Anyways, I will share what I have worked out, But as I said I don't know if this is the best solution?



 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swing has a Layout called CardLayout that lets you swap between components, in this case panels.
There's a tutorial page for it as well.

So your MainFrame would act similar to the way you've written it, in that it would react to the button...or better it would react to an event from the panel that would tell it to swap screens.

One thing, I wouldn't have Mainframe extend JFrame.  There's no point.  You're not actually adding any functionality to the JFrame class.  It's a bit like extending ArrayList and then only populating the values.
reply
    Bookmark Topic Watch Topic
  • New Topic