1
以下时,进行中的GridBagLayout组件跨越多个行,你可以看到一个简单的例子,我尝试绘制泰国国旗:使用只有一列
。
此标志有一个特殊功能。蓝线比其他线路的两倍大:
public class GridBagLayoutTest {
private static final Insets insets = new Insets(0, 0, 0, 0);
public static void main(String args[]) {
final JFrame frame = new JFrame("Make a component span multiple rows in GridBagLayout when using only one column");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JPanel row1 = new JPanel();
row1.setBackground(new Color(237, 27, 36));
JPanel row2 = new JPanel();
row2.setBackground(Color.WHITE);
JPanel row3 = new JPanel();
row3.setBackground(new Color(26, 29, 80));
JPanel row4 = new JPanel();
row4.setBackground(Color.WHITE);
JPanel row5 = new JPanel();
row5.setBackground(new Color(237, 27, 36));
//We are trying to draw Flag of Thailand
addComponent(frame, row1, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(frame, row2, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(frame, row3, 0, 2, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(frame, row4, 0, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(frame, row5, 0, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
frame.setSize(500, 200);
frame.setVisible(true);
}
private static void addComponent(Container container, Component component, int gridx, int gridy,
int gridwidth, int gridheight, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
}
我不想因为我想保留相同的行为,当我调整框架使用GridBagConstraint的财产ipady。 第三行(蓝色)的高度必须是其他尺寸的两倍。
我使用的是财产gridheight。对于蓝线,我把这个值赋给这个属性,但是什么也没有发生。
你有什么想法来实现这个目标吗?
您可以随时在row3后添加另一个蓝色的行。没有真正回答你的问题,但会解决你的问题 – Ian2thedv
我举了一个简单的例子,以便轻松地解释问题。实际上,它是一些JList或我操纵的某个JTextArea。因此,您的答案不能成为解决方案。 – Rafalsh