2

I am trying to create a simple GUI. I have a menu bar that is filled with various JMenuItems. Each menu item should link to a different "window". Currently, I am thinking the best way to do this is to create a single frame, and create various JPanels. My ActionListeners will toggle visibility of the different panels, and only one panel should be visible at a time. Is this the best way to go about the task? Or is there a better workaround.

Universal Electricity
  • 775
  • 1
  • 12
  • 26
neilb
  • 120
  • 1
  • 2
  • 11

2 Answers2

10

Yes, the best way is to use a CardLayout of which there's a great tutorial (please see the link), and many examples online including in this very forum, several of which I've written, including:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    See also [Working with 2 or more frames](http://stackoverflow.com/questions/7889922/working-with-2-or-more-frames/7890456#7890456). – Andrew Thompson Nov 26 '11 at 02:03
  • 1
    For an example specific to `CardLayout`, see also [this answer](http://stackoverflow.com/questions/5665156/calling-awt-frame-methods-from-subclass/5786005#5786005). – Andrew Thompson Nov 26 '11 at 02:08
4

JTabbedPane is already implemented for you!
JTabbedPane's tutorial.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • See also [Working with 2 or more frames](http://stackoverflow.com/questions/7889922/working-with-2-or-more-frames/7890456#7890456). – Andrew Thompson Nov 26 '11 at 02:04