繁体   English   中英

BoxLayout没有添加另一个JPanel

[英]BoxLayout not adding another JPanel

我正在尝试向我的窗口添加第二个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 S使用BoxLayout

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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