Java Vertical Layout?

*爱你&永不变心* 提交于 2019-12-08 14:46:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!