views:

384

answers:

2

Sir, We are working on a project.Here we encountered with a problem ie we are unable to include more than two panels on the same frame .What we want is one panel above the other. Can you please help us with this?

+5  A: 

Assuming you want two panels added to a single frame:

Set a layout for your parent JFrame and add the two panels. Something like the following

JFrame frame = new JFrame();
//frame.setLayout(); - Set any layout here, default will be the form layout
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
frame.add(panel2);

Assuming you want to add one panel over the other

JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
panel1.add(panel2);

There is no limit on the number of panels to be added on the JFrame. You should understand that they all are containers when seen on a higher level.

Bragboy
but the problem here is that the default layout is "FlowLayout" and that will not give him the desired result--Ok I misread the first part of your post a bit, but what is desired here is requires very specific layouts, not just "Set a layout"
Wintermute
+1  A: 

if you want each of the frames/panels the same size, use the GridLayout, with a grid of 1(column) and 2(rows)

Frame myFrame;  
GridLayout myLayout = new GridLayout(2,1);  

myFrame.setLayout(myLayout);  

Panel p1;  
Panel p2;  

myFrame.add(p1);
myFrame.add(p2);

if the panels are different size use the BorderLayout.... set the upper frame to "North" and the lower one to "South" or "Center"

Frame myFrame;  

myFrame.setLayout(new BorderLayout() );  

Panel p1;  
Panel p2;  

myFrame.add(p1, BorderLayout.NORTH);  
myFrame.add(p2, BorderLayout.CENTER);  
Wintermute