我找不到任何适用于我的问题的内容,也没有找到我测试过的解决方案。
所以,我正在为一门课程开发一个UI,当我调整窗口大小时,会在单元格之间创建一个巨大的空白区域,而不是像人们期望的那样(和我想要的那样)
我对面板进行了颜色编码:
所以,在勃艮第面板上我使用网格袋,因为我希望字段和标签按原样对齐,这是我发现这样做的最简单方法。现在,正如您在红色圆圈上看到的那样,在调整窗口大小时会创建一个巨大的空间,为什么?谁在做那件事?什么面板/配置是罪魁祸首?
以下是我认为涉及的部分代码,如果您需要其他内容,请告诉我
//----Display Panel (green box)
public void createDisplayPanel(){
JPanel dPanel = new JPanel();
dPanel.setBorder(margins);
add(dPanel, BorderLayout.EAST);
dPanel.setLayout(new BoxLayout(dPanel, BoxLayout.Y_AXIS));
dPanel.add(createDisplayTitlePanel()); //Purple box
dPanel.add(createDisplayFieldsPanel());//Burgundy box
dPanel.add(createDisplayControlsPanel());//Orange box
}
private JPanel createDisplayFieldsPanel(){
JPanel dFieldsPanel = new JPanel();
GridBagLayout gbl_dFieldsPanel = new GridBagLayout();
gbl_dFieldsPanel.columnWidths = new int[]{0};
gbl_dFieldsPanel.rowHeights = new int[] {0};
gbl_dFieldsPanel.columnWeights = new double[]{Double.MIN_VALUE};
gbl_dFieldsPanel.rowWeights = new double[]{Double.MIN_VALUE};
dFieldsPanel.setLayout(gbl_dFieldsPanel);
createDisplayFields(dFieldsPanel); //What really create the fields
return dFieldsPanel;
}
protected void createDisplayFields(JPanel dFieldsPanel) {
Object[][] inputFieldObjs = {
{ dID, "Residential Property ID:" },
{ dFName, "Property Legal Description:" },
{ dLName, "Property Address:" },
{ dAddress, "City Quadrant:"},
{ dPCode, "Zoning of Property:"},
{ dPhone, "Property Asking Price:" },
{ dType, "Building Square Footage:",clientType },
};
int row = 0;
for (Object[] obj : inputFieldObjs){
GridBagConstraints gbc_Label = createLabelGBConstrain(row,0);
GridBagConstraints gbc_InputField = createInputFieldGBConstrain(row,1);
dFieldsPanel.add(createLabel(obj),gbc_Label);
if(obj.length == 2)
dFieldsPanel.add(createTextField(obj),gbc_InputField);
else
dFieldsPanel.add(createComboBox(obj),gbc_InputField);
row ++;
}
private JLabel createLabel (Object[] objs){
String labelText = (String) objs[1];
JLabel label = new JLabel(labelText);
label.setFont(text);
return label;
}
private JTextField createTextField(Object[] objs){
JTextField field = (JTextField) objs[0];
field = new JTextField();
field.setFont(text);
field.setColumns(10);
return field;
}
private JComboBox createComboBox(Object[] objs){
JComboBox field = (JComboBox) objs[0];
String[] options = (String[]) objs[2];
field = new JComboBox(options);
field.setFont(text);
return field;
}
private GridBagConstraints createLabelGBConstrain(int row, int col){
GridBagConstraints gbc_Label = new GridBagConstraints();
gbc_Label.anchor = GridBagConstraints.EAST;
gbc_Label.insets = new Insets(0, 0, 5, 5);
gbc_Label.gridx = col;
gbc_Label.gridy = row;
return gbc_Label;
}
private GridBagConstraints createInputFieldGBConstrain(int row, int col){
GridBagConstraints gbc_InputField = new GridBagConstraints();
gbc_InputField.anchor = GridBagConstraints.WEST;
gbc_InputField.insets = new Insets(0, 0, 5, 0);
gbc_InputField.gridx = 1;
gbc_InputField.gridy = row;
return gbc_InputField;
}
我测试过的解决方案: 将weightx和weighty设置为0并且字段/标签上的填充为无...不起作用,我只能找到与我的案例远程相关的解决方案
感谢您的帮助=)
编辑:谢谢你们的答案,我的问题确实是由camickr的解决方案解决的。我真的不知道那段代码在做什么。它是自动生成的。还要感谢有关如何发布更好问题的评论提示,我一定会在下次记住这一点=)
答案 0 :(得分:1)
我做的第一件事是评论以下内容:
/*
gbl_dFieldsPanel.columnWidths = new int[]{0};
gbl_dFieldsPanel.rowHeights = new int[] {0};
gbl_dFieldsPanel.columnWeights = new double[]{Double.MIN_VALUE};
gbl_dFieldsPanel.rowWeights = new double[]{Double.MIN_VALUE};
*/
这会使所有组件垂直显示在一起。
答案 1 :(得分:0)
您必须为要包含额外空间的任何字段设置weightx和weighty约束。您还需要确保为该字段设置了锚点。
确保其余字段的weightx和weighty设置为零。我通常在开头将它们设置为零,并且在添加最后一个字段时仅转换为1。