We are working on a project where we encountered a problem with including more than two Panels on the same JFrame .What we want is one Panel above the other.
Can the
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);
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.
//you can also use card Layout, that enables you to add multiple card-panels on Main panel.
CardLayout cl;
JPanel main,one,two,three;
JButton button1,button2;
cl = new CardLayout();
main.setLayout(cl);
main.add(one,"1");
main.add(two,"2");
main.add(three,"3");
cl.show(main,"1");
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1)
cl.show(main,"2");
else if(e.getSource() == button2)
cl.show(main,"3");
}