我正在尝试将一些组件放在左侧对齐。
public class MyGUI extends JPanel
{
public MyGUI()
{
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
setLayout(layout);
JLabel label_1 = new JLabel("label1");
JTextField textArea = new JTextField(15);
JButton button_1 = new JButton("button 1");
button_1.addActionListener(new EventHandler());
JLabel label_2 = new JLabel();
JButton button_2 = new JButton("button 2");
button_2.addActionListener(new EventHandler());
JLabel label_3 = new JLabel();
JButton button_3 = new JButton("button 3");
button_3.addActionListener(new EventHandler());
JLabel label_4 = new JLabel();
add(label_1);
add(textArea);
add(button_1);
add(label_2);
add(button_2);
add(label_3);
add(button_3);
add(label_4);
}
但这就是我所得到的:
我需要位于左侧的按钮,位于右侧的标签(不可见)。什么布局管理器最适合这个,如何使用x / y coords手动定位任何组件?
答案 0 :(得分:3)
您是否尝试过SpringLayout(http://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html)?使用SpringLayout,您可以将组件的边缘附加到其他组件的边缘。例如,此语句将textArea
的西边缘附加到包含面板的西边缘,偏移量为5像素:
layout.putConstraint(SpringLayout.WEST, textArea, 5,SpringLayout.WEST, this);
您还可以将同一组件的北边附加到包含面板:
layout.putConstraint(SpringLayout.NORTH, textArea, 5, SpringLayout.NORTH, this);
您可以将这些约束添加到所有组件中。这有点单调乏味,但您可以控制放置组件的位置。这是一张图片:
以下是显示如何使用SpringLayout的代码示例:
public MyGUI()
{
SpringLayout layout = new SpringLayout();
setLayout(layout);
JLabel label_1 = new JLabel("label1");
JTextField textArea = new JTextField(15);
JButton button_1 = new JButton("button 1");
JLabel label_2 = new JLabel("1");
JButton button_2 = new JButton("button 2");
JLabel label_3 = new JLabel("2");
JButton button_3 = new JButton("button 3");
JLabel label_4 = new JLabel("3");
add(label_1);
add(textArea);
add(button_1);
add(label_2);
add(button_2);
add(label_3);
add(button_3);
add(label_4);
layout.putConstraint(SpringLayout.WEST, label_1, 5,SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, label_1, 6, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, textArea, 5,SpringLayout.EAST, label_1);
layout.putConstraint(SpringLayout.NORTH, textArea, 5, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, button_1, 5,SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, button_1, 5, SpringLayout.SOUTH, textArea);
layout.putConstraint(SpringLayout.WEST, label_2, 5,SpringLayout.EAST, button_1);
layout.putConstraint(SpringLayout.NORTH, label_2, 10, SpringLayout.SOUTH, textArea);
layout.putConstraint(SpringLayout.WEST, button_2, 5,SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, button_2, 5, SpringLayout.SOUTH, button_1);
layout.putConstraint(SpringLayout.WEST, label_3, 5,SpringLayout.EAST, button_2);
layout.putConstraint(SpringLayout.NORTH, label_3, 10, SpringLayout.SOUTH, button_1);
layout.putConstraint(SpringLayout.WEST, button_3, 5,SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, button_3, 5, SpringLayout.SOUTH, button_2);
layout.putConstraint(SpringLayout.WEST, label_4, 5,SpringLayout.EAST, button_3);
layout.putConstraint(SpringLayout.NORTH, label_4, 10, SpringLayout.SOUTH, button_2);
}
答案 1 :(得分:0)
您可以使用null布局并使用setBounds(x,y)手动定位它们:
setLayout(null);
setBounds(0,0);
我会在oracle.com上向您介绍有关布局管理器的指南。 Gridbag布局是最强大但难以学习的之一。
答案 2 :(得分:0)
我认为你需要使你的框架宽度更大,以便能够将所有组件放在一个ligne上,因为FLowLayout默认情况下对齐组件,除非没有更多的空间它跳转到下一个ligne。或者你可以使用gridLayout一个ligne和很多列,或GridBagLayout,但使用起来很痛苦。
答案 3 :(得分:0)
“哪个布局管理器最适合此任务”这一问题的答案是主观的。任何有能力的布局管理员都可以轻松完成您的布局 - GridBagLayout
,GroupLayout
,BoxLayout
(通过嵌套)或MigLayout
。
MigLayout
是目前功能最强的布局管理器。