简体   繁体   中英

BoxLayout not adding another JPanel

I am trying to add a second JPanel to my window, which uses BoxLayout . For some reason everything beyond my overridden JPanel refuses to appear.

Here's the code:

 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;
  }

I must have messed up the BoxLayout setup, or can BoxLayout not contain other JPanel s using BoxLayout ?

You never add the data panel to the gui .

Also, make sure you are calling super.paintComponent(g) (you've commented out that part of the code so I can't tell if you're doing it or not, but that may cause you problems if you don't)

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