How to create multiple screens in one window?

I mean, how do you create a user interface in which the screen changes with some action, but the program works completely in one window? In other words, I assume that this will be the same concept as website navigation, in which everything happens in one window (web browser), but the contents of the screen change with each action (button click, link click, etc. d.).

I looked at the Java API and found the CardLayout class; Am I heading in the right direction or is there an easier way? (The page indicated by CardLayout is somewhat complicated and recommended for those who want to manually encode everything).

I know that there are validate() and repaint() JFrame in JFrame , but this may not be practical for them. I'm not quite a beginner, but not very experienced, so bear with me.

Any guidance would be helpful. Many thanks.

+4
source share
4 answers

Yes, Cardlayout can be used to disable "Cards" , that is, the various panels that are visible on the screen.

You can also switch JPanels directly with

 jframe.getContentPane().remove(...); // Remove old panel from layout jframe.getContentPane().add(...); jframe.validate(); jframe.repaint() 

Remember that different layouts work differently, so there is no single solution that works every time. Check out the documentation for the different LayoutManager managers to find out how they work.

+4
source

You can use TabbedPane , hide tabs and programmatically switch them from your application.

Thus (for example, for testing purposes) you can easily turn on the tabs and check the status of each of them at any time.

But yes, CardLayout (generally) a good way to go.

You may be tempted to manually add and remove panels in the JFrame . I would advise you to do this - you can fix some rendering problems and your code will become more complex. Also, since you already have dedicated classes for this, you should use them instead of reinventing the wheel.

+2
source

I will personally remove all JFrame components. Something like that:

 myFrame.getContentPane().removeAll(); 

Then add a completely new content area, and then redraw and confirm the frame.

0
source

Create each screen as one JPanel (add screen components to JPanel). When you know which screen is required, use JFrame.setContentPane(Container contentPane) . This will replace everything in the JFrame except the menu. If you want some parts of the page to remain the same, you should use the layout on the contentPane and set the screen to only one part of the layout (depending on layout, see details for details here )

0
source

Source: https://habr.com/ru/post/1494912/


All Articles