好的,所以我正在搞乱 GridBagLayout 来掌握它。我想用它在 JFrame 中布局 JPanel,但这些单独的面板将使用不同的布局管理器,GridBagLayout 可能不适合。
但我有一个问题。如果我不向面板添加任何内容,则所有比例都可以完美地发挥作用,如下例所示:
public class RealMess extends JFrame{
private JPanel panel;
public RealMess(){
initUI();
}
public final void initUI(){
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screensize.getWidth();
int height = (int) screensize.getHeight();
panel = new JPanel();
panel.setLayout(new GridBagLayout());
JPanel left = new JPanel();
left.setBackground(Color.GREEN);
JPanel centre = new JPanel();
centre.setBackground(Color.PINK);
JPanel right = new JPanel();
right.setBackground(Color.CYAN);
JPanel leftBottom = new JPanel();
leftBottom.setBackground(Color.LIGHT_GRAY);
leftBottom.setLayout(new FlowLayout(0));
JPanel bottom = new JPanel();
bottom.setBackground(Color.MAGENTA);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0.11;
gbc.weighty = 0.9;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(left, gbc);
gbc.weightx = 0.11;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LAST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(leftBottom, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.9;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(centre, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(bottom, gbc);
gbc.weightx = 0.11;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_END;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
panel.add(right, gbc);
add(panel);
setTitle("Jack");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
RealMess rm = new RealMess();
rm.setVisible(true);
}
});
}
}
我知道我不必要地重新定义了一些 GridBagConstraints,但我不想错过任何东西。
无论如何,当我向任何面板添加按钮时,我的问题就出现了。像这样:
public class RealMess extends JFrame{
private JPanel panel;
public RealMess(){
initUI();
}
public final void initUI(){
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screensize.getWidth();
int height = (int) screensize.getHeight();
panel = new JPanel();
panel.setLayout(new GridBagLayout());
JPanel left = new JPanel();
left.setBackground(Color.GREEN);
JPanel centre = new JPanel();
centre.setBackground(Color.PINK);
JPanel right = new JPanel();
right.setBackground(Color.CYAN);
JPanel leftBottom = new JPanel();
leftBottom.setBackground(Color.LIGHT_GRAY);
leftBottom.setLayout(new FlowLayout(0));
JButton rewind = new JButton("1");
rewind.setPreferredSize(new Dimension(50, 25));
leftBottom.add(rewind);
JPanel bottom = new JPanel();
bottom.setBackground(Color.MAGENTA);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0.11;
gbc.weighty = 0.9;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(left, gbc);
gbc.weightx = 0.11;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LAST_LINE_START;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(leftBottom, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.9;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
panel.add(centre, gbc);
gbc.weightx = 0.78;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
panel.add(bottom, gbc);
gbc.weightx = 0.11;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_END;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.fill = GridBagConstraints.BOTH;
panel.add(right, gbc);
add(panel);
setTitle("Jack");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
RealMess rm = new RealMess();
rm.setVisible(true);
}
});
}
}
bottomLeft 面板变得更大,因此与之相关的网格方块的大小也随之增大。
在第一个示例中,所有内容都占用了窗口宽度或高度的百分比,由以下内容决定:
gbc.weightx
和
gbc.weighty
这样做可以很容易地使所有东西都具有正确的尺寸。
是否有一种解决方法可以让我强制面板尺寸仅受权重值的影响?