问题
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.
回答1:
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.
回答2:
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));
}
}
}
来源:https://stackoverflow.com/questions/8950885/java-vertical-layout