當我收縮JPanel的高度時,我遇到了GridBagLayout的問題。 只要我解決了這個問題,我得到了同樣的jpanel的填充寬度時的問題。如何停止由於weightx = 1f而調整大小的JTextField?
現在的問題是,所有組件的大小調整到所需的寬度以覆蓋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的寬度,你會看到一個文本框被調整過。
也可以建議smth關於這jpanel的邊界。正如你所看到的標題邊界太靠近最頂層的標籤eventLabel。
非常感謝!
你的問題只有邊界和標籤? – alex2410
如果您以後需要幫助,請開始接受您之前提出的問題的答案。 – camickr
你能告訴我們你需要什麼幫助嗎?除了有標籤的邊框,還不清楚你需要什麼樣的幫助。如果您可以提供所需外觀的屏幕截圖並描述組件應具有的行爲,那就太好了。 –