0
import java.awt.*;
import javax.swing.*;

public class Panels extends JApplet
{
  private Container c = getContentPane();

  public void init()
  {
    BorderLayout bl = new BorderLayout();   
    setLayout(bl);

    add(new JButton("East "),   BorderLayout.EAST);  
    add(new JButton("West "),   BorderLayout.WEST);  
    add(new JButton("North "),  BorderLayout.NORTH); 
    add(new JButton("South "),  BorderLayout.SOUTH);

    addCenterPanel();
  }

  void addCenterPanel()
  {
    JPanel p = new JPanel();
    setLayout(new BorderLayout());

    add(new JButton("Right "),  BorderLayout.EAST);  
    add(new JButton("Left "),   BorderLayout.WEST);  
    add(new JButton("Up "),     BorderLayout.NORTH); 
    add(new JButton("Down "),   BorderLayout.SOUTH);

    addInnermostPanel();
  }

  void addInnermostPanel()
  {
    JPanel center = new JPanel();

    center.setLayout(new BorderLayout());

    add(new JButton("> "),  BorderLayout.EAST);  
    add(new JButton("< "),  BorderLayout.WEST);  
    add(new JButton("^ "),  BorderLayout.NORTH); 
    add(new JButton("v "),  BorderLayout.SOUTH);
    add(new JButton("O"),   BorderLayout.CENTER);  
  }
}

I want the panels to display inside of each other (in the CENTER region) , but they are printing on top of each other and for whatever reason I cant figure out what Im missing. Thanks in advance, any help is appreciated

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166

2 Answers2

2

Here is you're code should be like this;

import java.awt.*;
import javax.swing.*;

public class Panels extends JFrame
{
  private Container c = getContentPane();

  public void init()
  {
    BorderLayout bl = new BorderLayout();
    setLayout(bl);
    setDefaultCloseOperation(3);
    setLocationRelativeTo(null);

    add(new JButton("East "),   BorderLayout.EAST);
    add(new JButton("West "),   BorderLayout.WEST);
    add(new JButton("North "),  BorderLayout.NORTH);
    add(new JButton("South "),  BorderLayout.SOUTH);

    add(addCenterPanel(),"Center");
  }

  JPanel addCenterPanel()
  {
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());

    p.add(new JButton("Right "),  BorderLayout.EAST);
    p.add(new JButton("Left "),   BorderLayout.WEST);
    p.add(new JButton("Up "),     BorderLayout.NORTH);
    p.add(new JButton("Down "),   BorderLayout.SOUTH);

    p.add(addInnermostPanel(),"Center");
    return p;
  }

  JPanel addInnermostPanel()
  {
    JPanel center = new JPanel();

    center.setLayout(new BorderLayout());

    center.add(new JButton("> "),  BorderLayout.EAST);
    center.add(new JButton("< "),  BorderLayout.WEST);
    center.add(new JButton("^ "),  BorderLayout.NORTH);
    center.add(new JButton("v "),  BorderLayout.SOUTH);
    center.add(new JButton("O"),   BorderLayout.CENTER);
    return center;
  }

  public static void main(String ...args){
       new Panels().setVisible(true);
  }
  public Panels(){
      init();
      pack();

  }
}

enter image description here

Note: I used JFrame instead applet. Also I just posted the code because it takes many time to describe the whole program, I think only the code will be enough.

If you have a question, write it in the comment.

Caffe Latte
  • 1,693
  • 1
  • 14
  • 32
  • +1 for [sscce](http://sscce.org/); see also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) and these convenient [hybrids](http://stackoverflow.com/a/12449949/230513). – trashgod Oct 09 '13 at 23:51
1

In addCenterPanel youre adding all components directly to the applet itself rather than to the JPanel p. That panel is not being added to the applet itself. In addInnermostPanel the components are again added to the applet and the center JPanel is never added to the container

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thats very helpful. One more question - in the addInnermostPanel method, how would I add the center panel to the CENTER region of panel p instead of the CENTER of the applet? Now I have the center panel printing inside of the North/East/South/West buttons, but replacing panel p in the CENTER. – smitty werbenjagermanjensen Oct 09 '13 at 20:49
  • Pass the `JPanel` `p` as an argument to `addInnermostPanel(Container c)`, then add `center` to `c` – Reimeus Oct 09 '13 at 20:52