我正在设计一个面板并OptionPane Confirm Dialog
像这样使用这个面板:
JOptionPane.showConfirmDialog(null, slaveGroupPanel, messages.getString("centrum.manageGroups"), JOptionPane.OK_CANCEL_OPTION);
我的网格包约束是这样的:
addGroupButton.setText(messages.getString("centrum.slavegroup.add"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(addGroupButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
add(addGroupTextField, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
add(groupNameLabel, gridBagConstraints);
groupComboBox.setModel(new DefaultComboBoxModel(slaveGroupHash.keySet().toArray() ));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 1;
add(groupComboBox, gridBagConstraints);
allSlavesList.setModel(new DefaultListModel());
for (int i = 0; i < allSlaves.length; i++) {
((DefaultListModel)allSlavesList.getModel()).addElement(allSlaves[i]);
}
allSlavesList.setVisibleRowCount(4);
jScrollPane2.setViewportView(allSlavesList);
jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridheight = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 0.45;
gridBagConstraints.weighty = 1.0;
add(jScrollPane2, gridBagConstraints);
slavesInGroupList.setModel(new DefaultListModel());
slavesInGroupList.setVisibleRowCount(4);
jScrollPane3.setViewportView(slavesInGroupList);
jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridheight = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 0.45;
gridBagConstraints.weighty = 1.0;
add(jScrollPane3, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
add(allSlavesLabel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
add(slavesInGroupLabel, gridBagConstraints);
addSlaveButton.setIcon(GeneralObjects.getrightIcon());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 3;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.weighty = 1.0;
add(addSlaveButton, gridBagConstraints);
deleteSlaveButton.setIcon(GeneralObjects.getleftIcon());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 4;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.weighty = 1.0;
add(deleteSlaveButton, gridBagConstraints);
renameButton.setText(messages.getString("centrum.slavegroup.rename"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(renameButton, gridBagConstraints);
deleteButton.setText(messages.getString("centrum.slavegroup.delete"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 5;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(deleteButton, gridBagConstraints);
但正如您在随附的屏幕截图中看到的那样,文本字段和添加按钮之间有两个间隙。删除和重命名按钮之间也会出现这种差距。但是我不想要那些差距。我没有定义插图,并且我有两个文本字段和一个组合框,weightx = 1;
所以fill = java.awt.GridBagConstraints.HORIZONTAL;
我希望它们尽可能地水平扩展而没有间隙。但为什么会出现这些差距?
其次,您可以清楚地看到右手边的 Jlist 比左手边的要宽。但是,我的两个 JLists(实际上是包含 JLists 的 ScrollPanes)都将gridwidth
,weightx
和fill
参数设置为相同的值。所以我希望它们的大小相同。看起来我错过了一些东西。
SSCCE 添加如下:
import javax.swing.*;
import java.awt.*;
public class SSCCE {
private javax.swing.JButton addGroupButton;
private javax.swing.JTextField addGroupTextField;
private javax.swing.JButton addSlaveButton;
private javax.swing.JList allSlavesList;
private javax.swing.JButton deleteButton;
private javax.swing.JButton deleteSlaveButton;
private javax.swing.JComboBox groupComboBox;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JButton renameButton;
private javax.swing.JLabel groupNameLabel;
private javax.swing.JLabel allSlavesLabel;
private javax.swing.JLabel slavesInGroupLabel;
private javax.swing.JList slavesInGroupList;
public SSCCE() {
}
public JPanel createPanel(){
JPanel panel = new JPanel();
panel.setLayout(new java.awt.GridBagLayout());
addGroupButton=new javax.swing.JButton();
addGroupTextField=new javax.swing.JTextField();
addSlaveButton=new javax.swing.JButton();
allSlavesList=new javax.swing.JList();
deleteButton=new javax.swing.JButton();
deleteSlaveButton=new javax.swing.JButton();
groupComboBox=new javax.swing.JComboBox();
jScrollPane2=new javax.swing.JScrollPane();
jScrollPane3=new javax.swing.JScrollPane();
renameButton=new javax.swing.JButton();
groupNameLabel=new javax.swing.JLabel();
allSlavesLabel=new javax.swing.JLabel();
slavesInGroupLabel=new javax.swing.JLabel();
slavesInGroupList=new javax.swing.JList();
allSlavesList.setModel(new DefaultListModel());
slavesInGroupList.setModel(new DefaultListModel());
GridBagConstraints gridBagConstraints;
addGroupButton.setText("add");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
panel.add(addGroupButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
panel.add(addGroupTextField, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
panel.add(groupNameLabel, gridBagConstraints);
groupComboBox.setModel(new DefaultComboBoxModel(new Object[]{"one","two","three"}));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 1;
panel.add(groupComboBox, gridBagConstraints);
allSlavesList.setVisibleRowCount(4);
jScrollPane2.setViewportView(allSlavesList);
jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridheight = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 0.45;
gridBagConstraints.weighty = 1.0;
panel.add(jScrollPane2, gridBagConstraints);
slavesInGroupList.setVisibleRowCount(4);
jScrollPane3.setViewportView(slavesInGroupList);
jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridheight = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 0.45;
gridBagConstraints.weighty = 1.0;
panel.add(jScrollPane3, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
panel.add(allSlavesLabel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
panel.add(slavesInGroupLabel, gridBagConstraints);
//addSlaveButton.setIcon(GeneralObjects.getrightIcon());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 3;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.weighty = 1.0;
panel.add(addSlaveButton, gridBagConstraints);
//deleteSlaveButton.setIcon(GeneralObjects.getleftIcon());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 4;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.weighty = 1.0;
panel.add(deleteSlaveButton, gridBagConstraints);
renameButton.setText("rename");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
panel.add(renameButton, gridBagConstraints);
deleteButton.setText("delete");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 5;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
panel.add(deleteButton, gridBagConstraints);
((DefaultListModel)allSlavesList.getModel()).addElement("one");
((DefaultListModel)allSlavesList.getModel()).addElement("two");
((DefaultListModel)allSlavesList.getModel()).addElement("three");
((DefaultListModel)slavesInGroupList.getModel()).addElement("one");
((DefaultListModel)slavesInGroupList.getModel()).addElement("two");
((DefaultListModel)slavesInGroupList.getModel()).addElement("three");
panel.setPreferredSize(new Dimension(600,300));
return panel;
}
public static void main(String[] args){
SSCCE a = new SSCCE();
JOptionPane.showConfirmDialog(null, a.createPanel(), "centrum.manageGroups", JOptionPane.OK_CANCEL_OPTION);
}
}