• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I switch?  RSS feed

 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got 2 Panel, but only one should be shown at once. If I press Space bla1 should switch to bla2, but now both are flashing on the screen.All classes are own-defined and use the same package. HELP!

 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why does PacMan extend JPanel?
2. Use CardLayout.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As well as Steve's advice, when comparing Strings always use .equals() and not '=='
 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Myers wrote:1. Why does PacMan extend JPanel?
2. Use CardLayout.

this is an old class name, my project now has nothing to do with pacman
 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better question: how can I remove Panels from the Frame?
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Joost wrote:A better question: how can I remove Panels from the Frame?


You don't remove panels, you use CardLayout as the layout manager for the contentPane of the JFrame, then have your listener call the .next() method of the layout manager.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You remove the component from the content pane and not the JFrame, however JFrame does have a remove(Component c) method which forwards the call to the content panes remove(Component c) method.
If you change the component hierarchy you will need to re-validate the layout as well. If the content pane is a Swing component then call it's revalidate() method.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Myers wrote:You don't remove panels, you use CardLayout as the layout manager for the contentPane of the JFrame

You can remove panels if you want to handle it yourself but you are right in that it is generally easier to use CardLayout. However you don't set the CardLayout on the JFrame, you set it on the content pane or a child component of the content pane.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!