当我缩小 JPanel 的高度时,我遇到了 GridBagLayout 的问题。一旦我解决了这个问题,我就在填充同一个 jpanel 的宽度时遇到了问题。
现在的问题是所有组件的大小都被调整为覆盖 jpanel 宽度所需的宽度。但是,如果我改变他们的 weightx 或者我改变填充属性,那么我会遇到 jpanel 高度缩小的新问题。
请复制粘贴此类以查看:
public class DateFormatDemo extends JFrame
{
JLabel eventLabel = new JLabel("Label : ", SwingConstants.LEFT);
JTextField eventLabelField = new JTextField("myFirstEvent", 30);
JLabel timestampLabel = new JLabel("Time stamp : ");
JLabel timestammpToLabel = new JLabel(" to ");
JTextField timestampStartField = new JTextField("2011-11-30 12:30:45", 15);
JTextField timestampEndField = new JTextField("2011-10-12 07:22:50", 15);
JLabel durationMagnitudeLabel = new JLabel("Duration Magnitude : ");
JTextField durationMagnitudeMinField = new JTextField("2011-10-12 09:12:40", 15);
JLabel durationMagnitudeToLabel = new JLabel(" to ");
JTextField durationMagnitudeMaxField = new JTextField("2011-10-12 11:13:34", 15);
JButton searchButton = new JButton("search");
public DateFormatDemo() {
JPanel searchPanel = new JPanel(new GridBagLayout()) {
Dimension minSize = new Dimension(200, 0);
Border border = BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Search query"),
BorderFactory.createEmptyBorder(20, 5, 10, 5));
@Override
public Dimension getMinimumSize() {
return minSize;
}
@Override
public Border getBorder() {
return border;
}
};
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.LINE_START,
GridBagConstraints.BOTH, new Insets(5, 2, 5, 3), 0, 0);
searchPanel.add(eventLabel, updateGridBagConstraints(gbc, 0, 0, 1, 1, 0f, 0f));
searchPanel.add(eventLabelField, updateGridBagConstraints(gbc, 1, 0, 3, 1, 1f, 0f));
searchPanel.add(timestampLabel, updateGridBagConstraints(gbc, 0, 1, 1, 1, 0f, 0f));
searchPanel.add(timestampStartField, updateGridBagConstraints(gbc, 1, 1, 1, 1, 1f, 0f));
searchPanel.add(timestammpToLabel, updateGridBagConstraints(gbc, 2, 1, 1, 1, 0f, 0f));
searchPanel.add(timestampEndField, updateGridBagConstraints(gbc, 3, 1, 1, 1, 1f, 0f));
searchPanel.add(durationMagnitudeLabel, updateGridBagConstraints(gbc, 0, 2, 1, 1, 0f, 0f));
searchPanel.add(durationMagnitudeMinField, updateGridBagConstraints(gbc, 1, 2, 1, 1, 1f, 0f));
searchPanel.add(durationMagnitudeToLabel, updateGridBagConstraints(gbc, 2, 2, 1, 1, 0f, 0f));
searchPanel.add(durationMagnitudeMaxField, updateGridBagConstraints(gbc, 3, 2, 1, 1, 1f, 0f));
gbc = updateGridBagConstraints(gbc, 0, 4, 3, 1, 0f, 0f);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.ipadx = 5;
gbc.ipady = 5;
gbc.insets = new Insets(15, 0, 0, 0);
searchPanel.add(searchButton, gbc);
super.getContentPane().add(searchPanel);
super.pack();
super.setVisible(true);
}
private GridBagConstraints updateGridBagConstraints(GridBagConstraints gbc, int gridx, int gridy, int gridwidth,
int gridheight, float weightx, float weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
return gbc;
}
public static void main(String[] args) throws ParseException {
new DateFormatDemo();
}
}
如果您调整 Jpanel 的宽度,您会看到文本字段也被调整了大小。
您也可以就这个 jpanel 的边框提出建议吗?如您所见,标题边框与最上面的标签 eventLabel 太接近。
非常感谢!