我在垂直JSplitPane中有JPanel。 JPanel包含jlabels和jtextfields。通过移动JSplitPane的分隔符缩小JPanel的高度jpanel中的所有组件都会自行调整大小。如何告诉他们在父jpanel的高度缩小时不调整大小。 我无法为JPanel调整minSize的大小,因为它使JSplitPane无法移动分隔符。
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setDividerSize(10);
splitPane.setOneTouchExpandable(true);
JPanel searchPanel = createSearchPanel();
splitPane.setDividerLocation(searchPanel.getPreferredSize().height + 5);
splitPane.setTopComponent(searchPanel);
如您所见searchPanel
。我们来看看:
JPanel searchPanel = new JPanel(new GridBagLayout()) {
Dimension minSize = new Dimension(200, 0);
@Override
public Dimension getMinimumSize() {
return minSize;
}
};
searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
new Insets(5, 2, 5, 3), 0, 0);
searchPanel.add(eventLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
searchPanel.add(eventLabelField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
searchPanel.add(timestampLabel, gbc);
gbc.gridx = 1;
searchPanel.add(timestampStartField, gbc);
gbc.gridx = 2;
searchPanel.add(timestammpToLabel, gbc);
gbc.gridx = 3;
searchPanel.add(timestampEndField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
searchPanel.add(locationLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 3;
searchPanel.add(locationField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
searchPanel.add(durationMagnitudeLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
searchPanel.add(durationMagnitudeMinField, gbc);
gbc.gridx = 2;
searchPanel.add(durationToLabel, gbc);
gbc.gridx = 3;
searchPanel.add(durationMagnitudeMaxField, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.weighty = 1.0;
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.insets = new Insets(15, 0, 0, 0);
searchPanel.add(searchButton, gbc);
当我向上移动垂直分隔线时,searchPanel的高度会缩小,组件看起来像:
你看到jtextFields比我移动divider后小得多
请帮帮我。
答案 0 :(得分:2)
我已经更改了你的代码。使用GridBagConstraint的 weightx 属性和GridBagConstraints.HORIZONTAL填充,它可以帮助组件正确填充其单元格。 示例代码:
JPanel searchPanel = new JPanel(new GridBagLayout()) {
Dimension minSize = new Dimension(200, 0);
@Override
public Dimension getMinimumSize() {
return minSize;
}
};
searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
new Insets(5, 2, 5, 3), 0, 0);
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.weighty = 1.0;
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.insets = new Insets(15, 0, 0, 0);
gbc.weightx = 0;
searchPanel.add(new JButton("b"), gbc);
return searchPanel;
观看docs How to use GridBagLayout
结果:
编辑:对不起,我在标签上使用了糟糕的名字)
答案 1 :(得分:1)
在底部添加一个虚拟JPanel
,并为JPanel
设置weightY = 1。 ALl其余组件应具有权重Y = 0。因此,在高度增加时,所有附加像素都以虚拟JPanel
为目标。