我正在使用DualListBox.java示例,但我遇到的问题是JList的宽度根据条目的长度进行缩放。理想情况下,我希望left JList
占据宽度的40%,JButtons
占据10%,right JList
占据剩余的40%。
private void initScreen()
{
setBorder(BorderFactory.createEtchedBorder());
setLayout(new GridBagLayout());
title = new JTextField("");
title.setEditable(false);
add(title, new GridBagConstraints(0, 0, 3, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
EMPTY_INSETS, 0, 0));
sourceLabel = new JLabel(DEFAULT_SOURCE_CHOICE_LABEL);
sourceListModel = new SortedListModel();
sourceList = new JList(sourceListModel);
sourceList.addListSelectionListener(new AddSelectionListener());
add(sourceLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
EMPTY_INSETS, 0, 0));
add(new JScrollPane(sourceList), new GridBagConstraints(0, 2, 1, 5, .5, 1, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, EMPTY_INSETS, 0, 0));
addButton = new JButton(ADD_BUTTON_LABEL);
add(addButton, new GridBagConstraints(1, 3, 1, 2, 0, .25, GridBagConstraints.CENTER, GridBagConstraints.NONE,
EMPTY_INSETS, 0, 0));
addButton.addActionListener(new AddListener());
removeButton = new JButton(REMOVE_BUTTON_LABEL);
add(removeButton, new GridBagConstraints(1, 5, 1, 2, 0, .25, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 5, 0, 5), 0, 0));
removeButton.addActionListener(new RemoveListener());
destLabel = new JLabel(DEFAULT_DEST_CHOICE_LABEL);
destListModel = new SortedListModel();
destList = new JList(destListModel);
destList.addListSelectionListener(new RemoveSelectionListener());
add(destLabel, new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
EMPTY_INSETS, 0, 0));
add(new JScrollPane(destList), new GridBagConstraints(2, 2, 1, 5, .5, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, EMPTY_INSETS, 0, 0));
}
DualListBBox()
已添加到JPanel,该JPanel已添加到compact grid
SpringUtilities.makeCompactGrid(content, 1, 1, 6, 6, 6, 6);
如果面板有水平滚动,我不知道,我只是希望它们都是相同的宽度。任何更熟悉网格和布局的人都可以给我一些指示,或者你在上面的代码中看到了什么错误的值?
谢谢!