gpt4 book ai didi

java - JPanel尺寸问题

转载 作者:行者123 更新时间:2023-12-02 07:04:48 25 4
gpt4 key购买 nike

我已使用 GridBagLayout 将两个 JPanel 添加到 JPanel 中。现在的问题是:

当主面板加载时,它会显示高于实际位置的上面板位,当数据库中的数据加载到 JTextfieldsJComboBox 时,面板将转到它的实际大小。

示例代码:

public class JPanelIssue extends JPanel {
public JPanelIssue() {
JPanel mainPanel = new JPanel(new GridBagLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
mainPanel.add(panel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.6, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
mainPanel.add(panel2, new GridBagConstraints(0, 1, 1, GridBagConstraints.REMAINDER, 1.0, 0.4,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

add(mainPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
}
}

panel1 包含与搜索条件相关的字段,panel2 包含显示结果的 JTable

AWT事件队列有问题还是其他问题?

提前致谢。

最佳答案

仅供评论

无法从此代码片段中重现,因为您忘记添加有关 JTextfields 和 JComboBox 的内容,然后面板将变为其实际大小。

When the main panel loads it shows the upper panel bit above the actual place it should be and when data from db loads into the JTextfields and JComboBox then panel goes to its actual size.

GBC 基于从 JComponents 返回到容器的 PreferredSize(假设关于 JPanel)

有两种方法

  1. (正确)JTextField 设置正确的 PreferredSize和 JComboBox也是

  2. (大小调整黑客)覆盖 JPanelgetPreferredSize 包含 JTextFieldJComboBox

  3. 然后,应通知所有其他父级/容器,而无需覆盖任何 setSizesetBoundssetPreferredSize,仅通过JFrame.pack()

关于java - JPanel尺寸问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16233348/

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