Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Vertical Layout?

I need to position a JLabel over some JButtons, vertically, like a game menu. They should all be centered. I already downloaded MigLayout, but I'm not sure how to use that, so I just want a way to position my components vertically and centered, MigLayout or not. Also, I don't want to use a IDE GUI designer.

like image 612
Jimmt Avatar asked Jan 21 '12 05:01

Jimmt


People also ask

What is a vertical layout?

The Vertical Layout control is used to hold content inside it vertically (stacked on-top of one another). Note that this control can be bound to a list which then becomes a repeating section. The Vertical Layout along with the Horizontal Layout are among the most common controls used for designing a user interface.

What are Java layout types?

The java. awt package provides five layout managers: FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout.

What is BoxLayout in Java?

The BoxLayout class is used to arrange the components either vertically (along Y-axis) or horizontally (along X-axis). In BoxLayout class, the components are put either in a single row or a single column.


2 Answers

You might use a (single column) GridLayout or BoxLayout for this. See Using Layout Managers & A Visual Guide to Layout Managers for more tips, ideas and working source.

like image 179
Andrew Thompson Avatar answered Oct 23 '22 00:10

Andrew Thompson


You should use BoxLayout. Here a basic example

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class VerticalPanel extends JPanel {

    public VerticalPanel() {
        super();
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        for (int i = 0; i < 10; i++) {
            add(new JLabel("Label n°" + i));
        }
    }

}
like image 42
alexandre-rousseau Avatar answered Oct 23 '22 00:10

alexandre-rousseau