我创建了一个 JPanel
并增加两个 JButtons
中。我将面板布局设置为 FlowLayout
...我想要一个 JButton
在左边 JFrame
其二 JButton
是在右边。
我试过了,但它抛出了一个... ... IllegalArgumentException
:
JPanel mainPanel = new JPanel(new FlowLayout());
JButton login = new JButton("Login");
JButton register = new JButton("Register");
mainPanel.add(register, FlowLayout.RIGHT);
mainPanel.add(login, FlowLayout.LEFT);
我可以用 FlowLayout
? 哪种布局可以让它工作?
mainPanel.add(register, FlowLayout.RIGHT);
mainPanel.add(login, FlowLayout.LEFT);
这些FlowLayout变量不是这样使用的。它们是作为布局管理器的属性使用的,而不是作为add(...)方法的约束条件。请阅读 FlowLayout
API的更多信息。
我希望一个JB按钮在JFrame的左边,而另一个JB按钮在右边。我可以使用FlowLayout来实现吗?
不可以。
哪种布局可以做到这一点?
你可以使用一个带有面板的。
BorderLayout
- 添加一个按钮到 BorderLayout.LINE_START
和一个到 BorderLayout.LINE_END
BoxLayout
- 添加 Box.createHorizontalGlue()
之间的两个按钮。阅读Swing教程中关于 布局经理 以获取更多关于上述每种布局管理器的信息和示例。