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

4

2 回答 2

2

这是你的代码应该是这样的;

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

  }
}

在此处输入图像描述

注意:我使用的是JFrame小程序。另外我只是发布了代码,因为描述整个程序需要很多时间,我认为只有代码就足够了。

如果您有任何问题,请将其写在评论中。

于 2013-10-09T19:42:54.710 回答
1

addCenterPanel您将所有组件直接添加到小程序本身而不是添加到JPanel p. 该面板没有被添加到小程序本身。在addInnermostPanel组件中再次添加到小程序中,并且center JPanel永远不会添加到容器中

于 2013-10-09T19:36:44.473 回答