gpt4 book ai didi

java - Swing 全尺寸 JPanel

转载 作者:行者123 更新时间:2023-12-01 13:59:25 27 4
gpt4 key购买 nike

如何达到outerPanel的全宽?

    private JPanel createLabelPanel() {

final JToolTip tt = new JToolTip();
//tt.setSize(new Dimension(100,200));
final CompoundBorder cb = new CompoundBorder(tt.getBorder(), BorderFactory.createEmptyBorder(2, 2, 2, 2));
final JPanel innerPanel = new JPanel();
innerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

innerPanel.setBorder(cb);
innerPanel.setBackground(tt.getBackground());
innerPanel.add(displayLabel);
innerPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));

final JScrollPane tooltipscrool = new JScrollPane(innerPanel);
//tooltipscrool.setMinimumSize(new Dimension(200,100));
tooltipscrool.setPreferredSize(new Dimension(100,120));
tooltipscrool.setBorder(BorderFactory.createLineBorder(Color.BLACK));

final JPanel outerPanel = new JPanel();
outerPanel.add(tooltipscrool);
outerPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
//outerPanel.set
return outerPanel;
}

让我:

enter image description here

我的目标是拥有全宽的innerPanel(绿色的)。

最佳答案

JPanel 默认情况下使用 FlowLayout,它倾向于使用每个组件的首选大小来定义其大小。

而是使用 BorderLayout,例如...

final JPanel outerPanel = new JPanel(new BorderLayout());

看看How to layout components within a container了解更多详情

您还应该尽可能避免使用 setPreferredSize,请查看 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?了解更多详情

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

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