Please do not create multiple JFrames unless absolutely necessary.
Why?
- There's multiple icons in the taskbar (on Windows and Linux).
- Switching windows is an added burden to the user.
- It raises issues with, e.g., the close button (do they all close if any close? is there one master?), etc.
Instead:
Consider using a JTabbedPane
.
To create a tabbed pane, instantiate JTabbedPane
, create the components you wish it to display, and then add the components to the tabbed pane using the addTab
method.
For example:
JTabbedPane tabbedPane = new JTabbedPane();
JComponent someComponent = ...
tabbedPane.addTab("Tab 1", someComponent);
JComponent anotherComponent = ...
tabbedPane.addTab("Tab 2", anotherComponent);
Alternatively, you could use a CardLayout
if you only want your users to see one view at a time.
The CardLayout
class manages two or more components (usually JPanel
instances) that share the same display space. Conceptually, each component that a CardLayout
manages is like a playing card or trading card in a stack, where only the top card is visible at any time.