请提供您在框架中布局和调整“JPanel”大小的方法

Please provide your method of laying out and resizing `JPanel`s in a frame

Panel Alignment Practice

面板对齐练习。每个面板都是一种颜色。我无法使用 new Dimension() 调整大小或灵活地操作面板。我试过 frm.setLayout(null)setBounds()GridBagConstraints

   frm = new JFrame();

   frmLayout = new BorderLayout();
   frmLayout.layoutContainer(frm.getContentPane());


   mainPnl = new MainPanel();
   sP = new SecondPanel();
   tP = new ThirdPanel();

   getContentPane().add(mainPnl, BorderLayout.WEST);
   getContentPane().add(sP, BorderLayout.EAST);
   getContentPane().add(tP, BorderLayout.SOUTH);

要更改 JPanel 的布局管理器,请使用:

frm.getContentPane().setLayout(frmLayout);

不要将其设置为 null,null 布局不会使用 JFrame 调整大小。

I was able to arrange the panels.

似乎如果我将面板添加到主面板 setBounds() 就可以了。我可能违反了你们最喜欢的所有挥杆惯例,但是嘿,我会在某个时候像你们一样学习。到目前为止 setBounds() 的唯一问题是调整 window 的大小。 getWidth()getHeight() 导致面板消失。

public class MainPanel extends JPanel {

SecondPanel sP;
ThirdPanel tP;
FourthPanel fP;

public MainPanel()
{

  setBackground(Color.ORANGE);
  setLayout(null);
  sP = new SecondPanel();
  sP.setBounds(0, 0, 90, 90);
  tP = new ThirdPanel();
  tP.setBounds(90, 0, 410, 90);
  fP = new FourthPanel();
  fP.setBounds(0, 400, 500, 100);

  add(sP);
  add(tP);
  add(fP);

}

}