简体   繁体   中英

java nested panels printing on top of each other

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

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();

  }
}

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM