Java定位GUI组件(带/不带布局管理器)

Java定位GUI组件(带/不带布局管理器),java,swing,user-interface,layout,Java,Swing,User Interface,Layout,我试图定位一些组件,使其与左侧对齐 public class MyGUI extends JPanel { public MyGUI() { FlowLayout layout = new FlowLayout(FlowLayout.LEFT); setLayout(layout); JLabel label_1 = new JLabel("label1"); JTextField textA

我试图定位一些组件,使其与左侧对齐

   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坐标手动定位任何组件

您可以使用空布局和设置边界(x,y)手动定位它们:


我想让您参考oracle.com上关于布局管理器的指南。Gridbag布局是最强大但很难学习的布局之一。

您可以使用空布局和设置边界(x,y)手动定位它们:


我想让您参考oracle.com上关于布局管理器的指南。Gridbag布局是最强大但很难学习的布局之一。

您可以使用空布局和设置边界(x,y)手动定位它们:


我想让您参考oracle.com上关于布局管理器的指南。Gridbag布局是最强大但很难学习的布局之一。

您可以使用空布局和设置边界(x,y)手动定位它们:


我想让您参考oracle.com上关于布局管理器的指南。Gridbag布局是最强大但很难学习的布局之一。

我认为您需要将框架宽度增大,以便能够在一个对齐上安装所有组件,因为FLowLayout默认情况下对齐组件,除非没有更多空间,否则它将跳转到下一个对齐。或者您可以使用gridLayout一个对齐多个列,或者GridBagLayout,但使用起来很痛苦。

我认为您需要将框架宽度增大,以便能够在一个对齐上容纳所有组件,因为默认情况下,FLowLayout对齐组件,除非没有更多空间,否则它将跳转到下一个对齐。或者您可以使用gridLayout一个对齐多个列,或者GridBagLayout,但使用起来很痛苦。

我认为您需要将框架宽度增大,以便能够在一个对齐上容纳所有组件,因为默认情况下,FLowLayout对齐组件,除非没有更多空间,否则它将跳转到下一个对齐。或者您可以使用gridLayout一个对齐多个列,或者GridBagLayout,但使用起来很痛苦。

我认为您需要将框架宽度增大,以便能够在一个对齐上容纳所有组件,因为默认情况下,FLowLayout对齐组件,除非没有更多空间,否则它将跳转到下一个对齐。或者您可以使用gridLayout一行多列,或者GridBagLayout,但使用起来很痛苦。

对于“哪个布局管理器最适合此任务”的问题,答案是主观的。任何有能力的布局管理器都可以轻松地进行布局—
GridBagLayout
GroupLayout
BoxLayout
(通过嵌套)或
MigLayout


MigLayout是迄今为止最有能力的布局管理器。

对于“哪个布局管理器最适合此任务”的问题,答案是主观的。任何有能力的布局管理器都可以轻松地进行布局—
GridBagLayout
GroupLayout
BoxLayout
(通过嵌套)或
MigLayout


MigLayout是迄今为止最有能力的布局管理器。

对于“哪个布局管理器最适合此任务”的问题,答案是主观的。任何有能力的布局管理器都可以轻松地进行布局—
GridBagLayout
GroupLayout
BoxLayout
(通过嵌套)或
MigLayout


MigLayout是迄今为止最有能力的布局管理器。

对于“哪个布局管理器最适合此任务”的问题,答案是主观的。任何有能力的布局管理器都可以轻松地进行布局—
GridBagLayout
GroupLayout
BoxLayout
(通过嵌套)或
MigLayout


MigLayout是迄今为止最有能力的布局管理器。

您尝试过SpringLayout()吗?使用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);          

}

你试过SpringLayout()吗?使用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);          

}

你试过SpringLayout()吗?使用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);          

}

你试过SpringLayout()吗?使用SpringLayout,可以将组件的边附着到其他组件的边。例如,此语句将
textArea
的西边缘附加到包含面板的西边缘,偏移量为5像素:

layout.putConstraint(SpringLayout.WEST, textArea, 5,SpringLayout.WEST, this);
你可以去艾尔