gerky
gerky

Reputation: 6417

How to Display a Panel in another Panel?

I'm coding a simple graphics program in Java.

So, I have 3 classes.

  1. The first class is the GUI w/c extends JFrame, it load the menu bar and panel (drawing class)

  2. The second class is the drawing class, it extends JPanel, and it has simple Graphics commands.

  3. The third class is an animation class, it displays an animation. It also extends JPanel.

So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class. When I try to place it there, it displays a tiny box beside the drawing class panel. I am not very good at frames and panels. Thank you very much in advance!

Upvotes: 1

Views: 1855

Answers (3)

Swaranga Sarma
Swaranga Sarma

Reputation: 13413

You might want to write

GUIFrame.remove(drawingPanel);
GUIFrame.add(animationPanel);
GUIFrame.pack();

However the behavior may vary if you have other components added to your GUIFrame. It is difficult to help you exactly because you have not posted an SSCCE.

Upvotes: 1

camickr
camickr

Reputation: 324118

So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class.

Use a Card Layout.

Upvotes: 0

Trasvi
Trasvi

Reputation: 1247

All JPanels have a LayoutManager which, rather handily, manages how Components are laid out. The default layout is FlowLayout, which default mode will simply place each component to the right of the last component.

If you want to change the layout to something more useful, there are many options; BorderLayout, GridLayout, GridBag layout are popular ones. Myself, I use MigLayout, an external library which is very powerful :).

As for it appearing small, try manually enforcing the size with setSize(w, h) or setPreferredSize(w, h).

Upvotes: 1

Related Questions