如何在FlowLayout的左边和右边有一个组件
问题描述:
在我的软件的一部分中,我有一个底部布局,其中包含JButton
s和JLabel
的几个。我想让按钮位于面板的右侧,并在左侧标注。我可以设法将按钮放在右侧,但不知道如何将JLabel
保留在左侧。如何在FlowLayout的左边和右边有一个组件
下面是代码:
bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
ftpBack = new JButton("Back");
ftpNext = new JButton("Next");
label = new JLabel("Text);
bottomPanel.add(label);
bottomPanel.add(ftpBack);
bottomPanel.add(ftpNext);
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
这就是我想实现:
不知道如何做呢?
答
你不能用FlowLayout
来做到这一点。
您可以使用水平BoxLayout
:
Box box = Box.createHorizontalBox();
box.add(label);
box.add(Box.createHorizontalGlue());
box.add(backButton);
box.add(Box.createHorizontalStrut(5));
box.add(nextButton);
阅读从How to Use BoxLayout Swing的教程部分获取更多信息和示例。
或者另一种方法是窝布局管理器:
JPanel main = new JPanel(new BorderLayout());
main.add(label, BorderLayout.WEST);
JPanel buttonPanel= new JPanel();
buttonPanel.add(back);
buttonPanel.add(next);
main.add(buttonPanel, BorderLayout.EAST);