java swing BoxLayout 中的中心面板组件
Center Panel's Component Within a java swing BoxLayout
我正在尝试使用 BoxLayout 垂直显示 2 个面板,我搜索了如何将这些面板中的组件居中。目前,我的组件放置在每个面板的顶部中心,我想将它们放在中心 X 和 Y 上。
我在 2 个面板中添加了我想要的组件,然后在我的 BoxLayout 中添加了面板。这样它们就按照我希望的那样垂直显示,但正如我所说,我不希望它们位于顶部中心。
我尝试使用 setAlignementY 和 setLocation 等方法,但它们中的任何一个实际上都移动了组件。我还看到 BoxLayout 会尝试将组件设置为与最宽的组件一样宽,但由于我只有 2 个具有相同尺寸的面板,所以我不太理解它。
这基本上就是我添加组件的方式(没有尝试居中):
private void initPanels ()
{
this.titlePanel.add(this.title);
this.bookInputPanel.add(bookTitle);
this.bookInputPanel.add(bookInput);
this.authorInputPanel.add(by);
this.authorInputPanel.add(authorInput);
this.authorInputPanel.add(this.authorsTable);
this.buttonsPanel.add(confirm);
this.contentPanel.setLayout(new BoxLayout(this.contentPanel, BoxLayout.Y_AXIS));
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.add(this.titlePanel, BorderLayout.NORTH);
this.add(this.contentPanel, BorderLayout.CENTER);
this.add(this.buttonsPanel, BorderLayout.SOUTH);
}
我做了一张图片来向您展示我想要的东西,但似乎我需要 10 个代表才能完成,对此感到抱歉。
This way they're displayed vertically as I want them to be, but as I said I don't want them to be on top center.
一种方法是将 "glue" 添加到面板的 top/bottom。此 "glue" 将扩展以填充面板可用的额外 space:
this.contentPane.add(Box.createVerticalGlue());
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.contentPane.add(Box.createVerticalGlue());
阅读有关 How to Use BoxLayout 的 Swing 教程部分,了解有关 BoxLayout
功能的更多信息。
另一种选择可能是使用使用不同布局管理器的 "wrapper" 面板。例如具有默认约束的 GridBagLayout
将自动居中组件 horizontally/vertically:
//this.add(this.contentPanel, BorderLayout.CENTER);
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add( contentPanel );
this.add(wrapper, BorderLayout.CENTER);
我正在尝试使用 BoxLayout 垂直显示 2 个面板,我搜索了如何将这些面板中的组件居中。目前,我的组件放置在每个面板的顶部中心,我想将它们放在中心 X 和 Y 上。
我在 2 个面板中添加了我想要的组件,然后在我的 BoxLayout 中添加了面板。这样它们就按照我希望的那样垂直显示,但正如我所说,我不希望它们位于顶部中心。
我尝试使用 setAlignementY 和 setLocation 等方法,但它们中的任何一个实际上都移动了组件。我还看到 BoxLayout 会尝试将组件设置为与最宽的组件一样宽,但由于我只有 2 个具有相同尺寸的面板,所以我不太理解它。
这基本上就是我添加组件的方式(没有尝试居中):
private void initPanels ()
{
this.titlePanel.add(this.title);
this.bookInputPanel.add(bookTitle);
this.bookInputPanel.add(bookInput);
this.authorInputPanel.add(by);
this.authorInputPanel.add(authorInput);
this.authorInputPanel.add(this.authorsTable);
this.buttonsPanel.add(confirm);
this.contentPanel.setLayout(new BoxLayout(this.contentPanel, BoxLayout.Y_AXIS));
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.add(this.titlePanel, BorderLayout.NORTH);
this.add(this.contentPanel, BorderLayout.CENTER);
this.add(this.buttonsPanel, BorderLayout.SOUTH);
}
我做了一张图片来向您展示我想要的东西,但似乎我需要 10 个代表才能完成,对此感到抱歉。
This way they're displayed vertically as I want them to be, but as I said I don't want them to be on top center.
一种方法是将 "glue" 添加到面板的 top/bottom。此 "glue" 将扩展以填充面板可用的额外 space:
this.contentPane.add(Box.createVerticalGlue());
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.contentPane.add(Box.createVerticalGlue());
阅读有关 How to Use BoxLayout 的 Swing 教程部分,了解有关 BoxLayout
功能的更多信息。
另一种选择可能是使用使用不同布局管理器的 "wrapper" 面板。例如具有默认约束的 GridBagLayout
将自动居中组件 horizontally/vertically:
//this.add(this.contentPanel, BorderLayout.CENTER);
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add( contentPanel );
this.add(wrapper, BorderLayout.CENTER);