Including more than two Panels in a JFrame?

后端 未结 3 569
忘了有多久
忘了有多久 2021-01-06 06:05

We are working on a project where we encountered a problem with including more than two Panels on the same JFrame .What we want is one Panel above the other.

Can the

3条回答
  •  迷失自我
    2021-01-06 06:49

    Assuming you want two panels added to a single frame:

    Set a layout for your parent JFrame and add the two panels. Something like the following

    JFrame frame = new JFrame();
    //frame.setLayout(); - Set any layout here, default will be the form layout
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    frame.add(panel1);
    frame.add(panel2);
    

    Assuming you want to add one panel over the other

    JFrame frame = new JFrame();
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    frame.add(panel1);
    panel1.add(panel2);
    

    There is no limit on the number of panels to be added on the JFrame. You should understand that they all are containers when seen on a higher level.

提交回复
热议问题