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