BoxLayout没有添加另一个JPanel

时间:2012-11-22 03:54:09

标签: java swing jpanel boxlayout

我正在尝试向我的窗口添加第二个JPanel,该窗口使用BoxLayout。出于某种原因,超出我被覆盖的JPanel的所有内容都拒绝出现。

以下是代码:

 public void initialize()
  {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Polygon Viewer");
    frame.setContentPane(makeGUI(frame));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,700);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
  }
  public JPanel makeGUI(final JFrame frame)
  {
    JPanel gui = new JPanel();
    gui.setLayout(new BoxLayout(gui,BoxLayout.PAGE_AXIS));

    class GraphPaint extends JPanel
    {
      public void paintComponent(Graphics g)
      {
        // Lots of graphics stuff
      }
    }

    GraphPaint mainG = new GraphPaint();
    mainG.setMinimumSize(new Dimension(600,600));
    mainG.setMaximumSize(new Dimension(600,600));
    mainG.setPreferredSize(new Dimension(600,600));
    gui.add(mainG);

    // Everything beyond here refuses to show up in the window

    JPanel lowerBar = new JPanel();
    lowerBar.setLayout(new BoxLayout(lowerBar,BoxLayout.LINE_AXIS));
    lowerBar.setMinimumSize(new Dimension(600,100));
    lowerBar.setPreferredSize(new Dimension(600,100));
    lowerBar.setBackground(Color.RED);
    gui.add(lowerBar);

    JPanel data = new JPanel();
    data.setLayout(new BoxLayout(data,BoxLayout.PAGE_AXIS));

    JLabel area = new JLabel("Area: <insert area here>");
    data.add(area);

    JLabel perimeter = new JLabel("Perimeter: " + shape.perimeter());
    data.add(perimeter);

    return gui;
  }

我一定搞砸了BoxLayout设置,或者BoxLayout可以使用JPanel不包含其他BoxLayout

1 个答案:

答案 0 :(得分:2)

您永远不会将data面板添加到gui

另外,请确保您正在呼叫super.paintComponent(g)(您已经注释掉了部分代码,因此无法判断您是否正在执行此操作,但如果您不这样做,可能会导致问题“T)