gpt4 book ai didi

java - 使用 Swing 的简单两列布局

转载 作者:行者123 更新时间:2023-11-30 06:38:09 24 4
gpt4 key购买 nike

如何使用 Swing 获得固定的侧边栏 JPanel。现在我正在尝试这个:

public SidebarPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
this.setPreferredSize(new Dimension(200, this.getPreferredSize().height));
...

但是当我调整窗口大小时,边栏的宽度也会改变。如何解决这个问题?

最佳答案

在 Swing 中不能保证首选大小得到遵守。这取决于容器的布局管理器。

如果容器使用 BorderLayout,您可以使用以下方法添加 SidebarPanel:

container.add(sidebarPanel, BorderLayout.EAST)

这将遵守首选宽度。

关于java - 使用 Swing 的简单两列布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2585199/

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