使用GridBagLayout在下面的图表上出了什么问题?每个厚矩形都是JPanel。 细线只是正确y值的帮助。
提前致谢!
package at;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridMask extends JFrame {
JPanel jPanel1, jPanel2, jPanel3, jPanel4, jPanel5, jPanel6;
public GridMask() {
unimportantStuff();
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints gbc;
gbc = getGbc(0, 0, 3, 1, 0.5);
gbl.setConstraints(jPanel1, gbc);
gbc = getGbc(1, 0, 4, 1, 0.75);
gbl.setConstraints(jPanel2, gbc);
gbc = getGbc(2, 0, 3, 1, 0.5);
gbl.setConstraints(jPanel3, gbc);
gbc = getGbc(0, 3, 3, 1, 0.5);
gbl.setConstraints(jPanel4, gbc);
gbc = getGbc(1, 4, 2, 1, 0.25);
gbl.setConstraints(jPanel5, gbc);
gbc = getGbc(2, 3, 3, 1, 0.5);
gbl.setConstraints(jPanel6, gbc);
add(jPanel1);
add(jPanel2);
add(jPanel3);
add(jPanel4);
add(jPanel5);
add(jPanel6);
setVisible(true);
}
private GridBagConstraints getGbc(int x, int y, int height, int width, double weightY) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridheight = height;
gbc.gridwidth = width;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = weightY;
return gbc;
}
private void unimportantStuff() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jPanel5 = new javax.swing.JPanel();
jPanel6 = new javax.swing.JPanel();
jPanel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jPanel2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jPanel3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jPanel4.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jPanel5.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jPanel6.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
public static void main(String[] args) {
new GridMask();
}
}
结果是一个JFrame,其中两行具有不同的高度,但一行中的单元格具有相同的高度。
答案 0 :(得分:1)
但连续的单元格具有相同的高度。
gbc.fill = GridBagConstraints.BOTH;
您告诉布局调整所有组件的大小以填充每个单元格中的可用空间。