Java: single frame or multiple frames

Think of the classic installation process, where you have the β€œnext” button, and when you click on it, the contents of the window change. To present this situation, I thought of two possible solutions:

-when "next" click, destroy the current JFrame and create a new JFrame, possibly pass useful information to its constructor (for example, the actual size of the window, the content inserted by the user in the current frame, ...)

- when you click "next", all components from the current JFrame are deleted and, if necessary, new components are added

The first solution looks better than OOprogramming, because I can store separate classes for different frames, and I can avoid the huge methods that empty the frame and populate it. However, the first solution sounds a little "dirty", and I have to pass a lot of parameters to a new frame. To present this situation, I would choose the second solution.

Now think of a menu with the option component: in this situation, I would create a new JFrame when I clicked on option so that I could fill it with options. This is the right decision? Is there a way that I can always know which one is the best solution? Are there any solutions that I haven't thought about?

+4
source share
2 answers

Destroying the main JFrame would be stupid - not to mention jarring for the user. Just use one JFrame and change its contents.

To implement the installation wizard, use one JFrame containing one large JPanel top and a smaller one containing the Next, Back, and Cancel buttons at the bottom. When the Next or Back buttons are clicked, you replace the large JPanel . You can have many different subclasses of JPanel , one for each wizard page.

Here a LayoutManager is called CardLayout , which is ideal for implementing this scenario - it manages the "stack" of components and displays only one of these components at a time. Use BorderLayout in a JFrame . In the center position, put a JPanel with CardLayout . Then add individual wizard pages to JPanel , so CardLayout can manage them.

+5
source

CardLayout is well suited for this. You simply replace the contents of JPanel when you click Next.

+3
source

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


All Articles