Adding JPanels in series?

Hi, Im trying to add 2 Panels to a Frame. When I add the First one alone it appears in the frame (Both Panels just have a single button in them to test that its Accualy there), but when I goto add another Panel it seems as if it overlapped the first Panel or somthing because now I can only see the new Button. I thought that if you add 2 Panels to a frame that they are added side by side in series.

publicvoid makeFrame()

{

JFrame myFrame =new JFrame();

frame = myFrame;

frame.setSize(500,500);

frame.setTitle("Nasa Program");

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicvoid addPanels()

{

JPanel panel =new JPanel();

panel.add(new JButton("dfgdfghjkghjkghjkghjkgjkgjk"));

JPanel panel2 =new JPanel();

panel2.add(new JButton("new"));

frame.add(panel);

frame.add(panel2);

}

This is the code block.

So when I run it it just shows one button that says "new" and it appears in the top middle of the frame.

Any Ideas how to have both frames appear right?

[1641 byte] By [Kroogera] at [2007-11-26 21:38:47]
# 1
You need to use a layout manager: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
ignignokt84a at 2007-7-10 3:22:04 > top of Java-index,Java Essentials,New To Java...
# 2
Try:frame.getContentPane().add(component)
TuringPesta at 2007-7-10 3:22:04 > top of Java-index,Java Essentials,New To Java...
# 3

frame.add(panel, BorderLayout.NORTH);

frame.add(panel2, BorderLayout.SOUTH);

By default, the content pane of a JFrame uses layout manager BorderLayout.

Simply calling add(comp) puts comp into the centre of the BorderLayout,

and there should be at most one component in the centre of BorderLayout.

Tutorial: http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

DrLaszloJamfa at 2007-7-10 3:22:04 > top of Java-index,Java Essentials,New To Java...
# 4

import javax.swing.*;

import java.awt.*;

public class Layout{

public static void main(String[] args){

new Layout();

}

public Layout(){

addPanels();

makeFrame();

}

public void addPanels(){

JPanel panel1 = new JPanel();

panel1.add(new JButton("new1"));

JPanel panel2 = new JPanel();

panel2.add(new JButton("new2"));

frame.getContentPane().setLayout(new FlowLayout());

frame.getContentPane().add(panel1);

frame.getContentPane().add(panel2);

}

public void makeFrame(){

frame.setSize(500,500);

frame.setTitle("Nasa Program");

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

JFrame frame = new JFrame();

}

TuringPesta at 2007-7-10 3:22:04 > top of Java-index,Java Essentials,New To Java...
# 5
Ahh intresting, Ok thank you for the help.
Kroogera at 2007-7-10 3:22:04 > top of Java-index,Java Essentials,New To Java...