我想创建一个由4个组件组成的JPanel,分为2列。左列的上部组件应占垂直空间的60%,下部组件的剩余部分占40%。在右栏中,它应该是相反的 - 上部组件需要40%而下部组件需要60%。
所以基本上我想把我的组件放在这张照片上:
我试图用GridBagLayout
来实现这种行为。
public class Test extends JFrame {
JPanel testPanel = new JPanel();
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Test().setVisible(true);
});
}
Test() {
prepareTestPanel();
setContentPane(testPanel);
setSize(500, 500);
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void prepareTestPanel() {
testPanel.setLayout(new GridBagLayout());
addComp(makeUpperLeft());
addComp(makeLowerLeft());
addComp(makeUpperRight());
addComp(makeLowerRight());
}
private void addComp(Pair p) {
testPanel.add(p.comp, p.gbc);
}
private Pair makeUpperLeft() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Upper Left"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 60;
gbc.gridwidth = 50;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.weightx = 0.5;
gbc.weighty = 0.6;
return new Pair(panel, gbc);
}
private Pair makeLowerLeft() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Lower Left"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 60;
gbc.gridheight = 40;
gbc.gridwidth = 50;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.weightx = 0.5;
gbc.weighty = 0.4;
return new Pair(panel, gbc);
}
private Pair makeUpperRight() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Upper Right"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 50;
gbc.gridy = 0;
gbc.gridheight = 40;
gbc.gridwidth = 50;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.weightx = 0.5;
gbc.weighty = 0.4;
return new Pair(panel, gbc);
}
private Pair makeLowerRight() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Lower Right"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 50;
gbc.gridy = 40;
gbc.gridheight = 60;
gbc.gridwidth = 50;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.weightx = 0.5;
gbc.weighty = 0.6;
return new Pair(panel, gbc);
}
private class Pair {
Component comp;
GridBagConstraints gbc;
public Pair(Component comp, GridBagConstraints gbc) {
this.comp = comp;
this.gbc = gbc;
}
}
}
不幸的是,我得到的是:
我应该如何更正我的代码?此外,无论窗口的大小如何调整,我都希望保留这个比例。我该如何设置weighty
?
答案 0 :(得分:2)
GridBagLayout并不容易支持:
您无法以所需的比例指定网格宽度/高度。如果你想要6/4的比例,那么你实际上需要创建10个相同大小的虚拟组件,并首先将它们添加到面板中。然后你可以说一个组件的网格高度等于那些虚拟组件中的4个(或6个)。
在调整帧大小时,weightx / y约束仅适用于额外空间。因此,组件最初将按其首选尺寸进行包装,这意味着您还需要手动设置所需比例的首选尺寸。
您可以查看RelativeLayout。它允许您根据可用空间轻松创建相对大小的组件。
使用此布局的基本代码如下:
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
JPanel left = new JPanel( rl );
left.add(upperLeft, new Float(60));
left.add(lowerLeft, new Float(40));
// repeat above for the right panel
JPanel main = new JPanel( new GridLayout(0, 2) );
main.add(left);
main.add(right);
frame.add(main);
答案 1 :(得分:1)
这不能直接在一个容器中完成。您必须为两个列创建两个容器元素(比如说两个JPanel
对象)。在外部容器中添加列,然后在内部面板中添加组件。以下是一些代码:
public class Test extends JFrame {
public Test() {
JPanel testPanel = new JPanel();
setContentPane(testPanel);
GridBagLayout gbl_testPanel = new GridBagLayout();
gbl_testPanel.columnWidths = new int[]{0, 0};
gbl_testPanel.rowHeights = new int[]{0};
gbl_testPanel.columnWeights = new double[]{0.5, 0.5};
gbl_testPanel.rowWeights = new double[]{1.0};
testPanel.setLayout(gbl_testPanel);
JPanel leftPanel = new JPanel();
GridBagConstraints gbc_leftPanel = new GridBagConstraints();
gbc_leftPanel.fill = GridBagConstraints.BOTH;
gbc_leftPanel.insets = new Insets(0, 0, 0, 5);
gbc_leftPanel.gridx = 0;
gbc_leftPanel.gridy = 0;
testPanel.add(leftPanel, gbc_leftPanel);
GridBagLayout gbl_leftPanel = new GridBagLayout();
gbl_leftPanel.columnWidths = new int[]{0};
gbl_leftPanel.rowHeights = new int[]{0, 0};
gbl_leftPanel.columnWeights = new double[]{0.0};
gbl_leftPanel.rowWeights = new double[]{0.6, 0.4};
leftPanel.setLayout(gbl_leftPanel);
JLabel lbl00 = new JLabel("Upper Left");
GridBagConstraints gbc_lbl00 = new GridBagConstraints();
gbc_lbl00.anchor = GridBagConstraints.NORTH;
gbc_lbl00.fill = GridBagConstraints.HORIZONTAL;
gbc_lbl00.insets = new Insets(0, 0, 5, 0);
gbc_lbl00.gridx = 0;
gbc_lbl00.gridy = 0;
leftPanel.add(lbl00, gbc_lbl00);
JLabel lbl10 = new JLabel("Lower Left");
GridBagConstraints gbc_lbl10 = new GridBagConstraints();
gbc_lbl10.anchor = GridBagConstraints.NORTH;
gbc_lbl10.fill = GridBagConstraints.HORIZONTAL;
gbc_lbl10.gridx = 0;
gbc_lbl10.gridy = 1;
leftPanel.add(lbl10, gbc_lbl10);
JPanel rightPanel = new JPanel();
GridBagConstraints gbc_rightPanel = new GridBagConstraints();
gbc_rightPanel.fill = GridBagConstraints.BOTH;
gbc_rightPanel.gridx = 1;
gbc_rightPanel.gridy = 0;
testPanel.add(rightPanel, gbc_rightPanel);
GridBagLayout gbl_rightPanel = new GridBagLayout();
gbl_rightPanel.columnWidths = new int[]{0};
gbl_rightPanel.rowHeights = new int[]{0, 0};
gbl_rightPanel.columnWeights = new double[]{0.0};
gbl_rightPanel.rowWeights = new double[]{0.4, 0.6};
rightPanel.setLayout(gbl_rightPanel);
JLabel lbl01 = new JLabel("Upper Right");
GridBagConstraints gbc_lbl01 = new GridBagConstraints();
gbc_lbl01.insets = new Insets(0, 0, 5, 0);
gbc_lbl01.fill = GridBagConstraints.HORIZONTAL;
gbc_lbl01.anchor = GridBagConstraints.NORTH;
gbc_lbl01.gridx = 0;
gbc_lbl01.gridy = 0;
rightPanel.add(lbl01, gbc_lbl01);
JLabel lbl11 = new JLabel("Lower Right");
GridBagConstraints gbc_lbl11 = new GridBagConstraints();
gbc_lbl11.anchor = GridBagConstraints.NORTH;
gbc_lbl11.fill = GridBagConstraints.HORIZONTAL;
gbc_lbl11.gridx = 0;
gbc_lbl11.gridy = 1;
rightPanel.add(lbl11, gbc_lbl11);
}
}