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.
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.
The java. awt package provides five layout managers: FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout.
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.
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.
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));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With