如何通过FlowLayout在左侧拥有一个组件而在右侧拥有一个组件

Dan*_*Dan 1 java swing layout-manager flowlayout

在我的软件的一部分中,我在底部有一个布局,其中包含几个JButtons和一个JLabel。我想在面板的右侧保持一个按钮,在左侧保持标签。我可以设法将按钮放在右侧,但不知道如何将按钮保持JLabel在左侧。

这是代码:

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);
Run Code Online (Sandbox Code Playgroud)

这是我要实现的目标: 在此处输入图片说明

知道如何制作吗?

cam*_*ckr 5

您无法使用来执行此操作FlowLayout

您可以使用水平BoxLayout

Box box = Box.createHorizontalBox();
box.add(label);
box.add(Box.createHorizontalGlue());
box.add(backButton);
box.add(Box.createHorizontalStrut(5));
box.add(nextButton);
Run Code Online (Sandbox Code Playgroud)

阅读Swing教程中有关如何使用BoxLayout的部分,获取更多信息和示例。

或者另一种方法是嵌套布局管理器:

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);
Run Code Online (Sandbox Code Playgroud)