【问题标题】:Using BoxLayout to resize JPanels in GUI [closed]使用 BoxLayout 在 GUI 中调整 JPanel 的大小 [关闭]
【发布时间】:2017-02-26 21:55:37
【问题描述】:

我目前正在使用此代码:

this.getContentPane().add(wwjPanel, BorderLayout.EAST);
        if (includeLayerPanel)
        {
            this.controlPanel = new JPanel();
            controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
            this.layerPanel = new LayerPanel(this.getWwd());
            this.controlPanel.add(new FlatWorldPanel(this.getWwd()),Box.createRigidArea(new Dimension(1000, 1000))); //This is the top pan
            this.controlPanel.add(this.getStates(), Box.createRigidArea(new Dimension(0, 100)));
            this.controlPanel.add(this.getPanelAlerts(), Box.createRigidArea(new Dimension(0,100)));

            this.getContentPane().add(this.controlPanel, BorderLayout.WEST);//This is the whole panel on the left
        }

我正在尝试调整 JPanel(此处称为 controlPanel)的大小,以使每个 JPanel 在我的 GUI 中都有自己独特的大小。我是使用 java 构建 GUI 的新手,我拥有的大部分代码都是从另一个文件中提取的。我试图合并的代码是我的代码中描述的这些新面板,并试图调整它们的大小。我想要使​​用 BoxLayout 来获得想要的效果吗?

此外,当我使用 createRigidArea 时,它似乎可以工作,但如果我继续更改您传递给它的 x 和 y 值,似乎什么也没有发生。我的意思是,通过更改值我看不到任何视觉差异,并且我使用的值范围为 0-1000。

谢谢。

【问题讨论】:

    标签: java swing jpanel layout-manager boxlayout


    【解决方案1】:
    this.controlPanel.add(new FlatWorldPanel(this.getWwd()),Box.createRigidArea(new Dimension(1000, 1000))); 
    

    Box.createRigidArea(...) 没有做任何事情。 add(...) 方法的第二个参数是布局管理器要使用的约束,而 BoxLayout 不需要任何约束,因此应该忽略它。

    如果您想要垂直堆叠的面板之间的垂直空间,那么您需要将其添加为单独的组件,您可能会使用Box.createVerticalStrut()

    this.controlPanel.add(new FlatWorldPanel(this.getWwd()));
    this.controlPanel.add(Box.createVerticalStrut( 50 ));
    

    FlatWorldPanel 的大小由您添加到其中的组件决定。

    阅读 How to Use BoxLayout 上的 Swing 教程部分,了解更多信息和工作示例。

    【讨论】:

    • 这是有道理的,我在您链接到的文档中看到了。但是,让你对它的看法清除它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2012-09-01
    • 2013-08-04
    相关资源
    最近更新 更多