面板对齐练习。每个面板都是一种颜色。我无法使用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的LayoutManager,请使用:
frm.getContentPane().setLayout(frmLayout);
不要将其设置为null,不使用JFrame调整null布局的大小。
I was able to arrange the panels.
似乎我将面板添加到主面板setBounds()
工作。我可能违反了你最喜欢的挥杆习惯,但是嘿,我会在某些时候像你们一样学习。到目前为止只有setBounds()
的问题是调整窗口大小。 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);
}
}