简体   繁体   中英

Java positioning GUI component with/without layout manager

I'm trying to position some components to align to the left.

   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); 


    }

But this is all I'm getting:

在此处输入图片说明

I need the buttons positioned to the left,a the labels (not visible) positioned to the right. What layout manager would be best for this and how can I manually position any components using x/y coords?

Have you tried SpringLayout ( http://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html )? With SpringLayout, you attach the component's edges to other component's edges. For example, this statement attaches the west edge of the textArea to the west edge of the containing panel, with an offset of 5 pixels:

layout.putConstraint(SpringLayout.WEST, textArea, 5,SpringLayout.WEST, this);

You can also attach the north edge of the same component to the containing panel:

layout.putConstraint(SpringLayout.NORTH, textArea, 5, SpringLayout.NORTH, this);

You can add these constraints to all of your components. It's a little tedious, but you have a lot of control over where you place the components. Here's a picture:

在此处输入图片说明

And here's the code example showing how to use 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);          

}

You can manually position them by using a null layout and using setBounds(x,y):

setLayout(null);
setBounds(0,0);

I would reference you to this guide at oracle.com about layout managers. Gridbag layout is one of the most powerful but hard to learn.

i think you need to make your frame width larger to be able to fit all your component on one ligne ,because FLowLayout align component by default unless there is no more space it's jump to the next ligne. or you can use gridLayout one ligne and many columns ,or GridBagLayout but it's painful to use.

The answer to the question "Which layout manager is best for this task" is subjective. Any capable layout manager can do your layout easily— GridBagLayout , GroupLayout , BoxLayout (by nesting), or MigLayout .

The MigLayout is by far the most capable layout manager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM