How to remove JPanel padding in MigLayout?

2019-07-20 20:44发布

Following situation: when I add a JLabel to a panel, i get unwanted padding/space. How can i remove it? See left side, i want it like the right side of the image shows.

enter image description here

here's my short test-code, that produces the output shown on the left side of the image above:

setLayout(new MigLayout("gapy 0, debug"));
JPanel line1 = new JPanel();
JPanel line2 = new JPanel();;
line1.add(new JLabel("Text 1"));
line2.add(new JLabel("Text 2"));
add(line1, "wrap, align left");
add(line2);

1条回答
我只想做你的唯一
2楼-- · 2019-07-20 21:25

That happens because you add labels to JPanel which used FlowLayout with gaps as default. To fix that you can use next:

    JPanel line1 = new JPanel(new FlowLayout(FlowLayout.CENTER,0,0));
    JPanel line2 = new JPanel(new BorderLayout());

enter image description here

查看更多
登录 后发表回答