gpt4 book ai didi

java - 如何阻止 JTextField 由于weightx=1f而被调整大小?

转载 作者:行者123 更新时间:2023-12-02 06:35:18 27 4
gpt4 key购买 nike

当我缩小 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 太近了。

非常感谢!

最佳答案

If you resize width of Jpanel you'll see that textfields are resized too.

网格包布局管理器将列的权重计算为列中所有组件的最大weightx。如果生成的布局在水平方向上小于其需要填充的区域,则额外的空间将按其权重的比例分配给每列。权重为零的列不会收到额外的空间。因此,将您不希望调整宽度的文本字段的 wieghtx 设置为 0

编辑: GridBagLayout 尊重其布局的组件的最小和首选大小。尝试设置它们,它应该可以工作。

关于java - 如何阻止 JTextField 由于weightx=1f而被调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19731408/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com