How do you make MiGLayout behave like Wrap Layout?

2019-02-17 05:31发布

I'd like to replicate the example shown here:

Wrap Layout

using MiGLayout. I have tried some combinations, but I'm having a hard time making the buttons wrap automatically to new rows as the container shrinks.

Could someone please provide a working example doing this?

Edit: Here is a shell for the program:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;


public class MiGTest extends JFrame{
    private JPanel jPanel;
    private JButton jButton;

    public static void main(String[] args) {
        new MiGTest().setVisible(true);
    }

    public MiGTest(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new MigLayout("debug"));

        initComponents();
        addComponents();
        pack();
    }


    private void addComponents() {      
        add(jPanel);{
            for (int i = 0; i < 10; i++) {
                jPanel.add(new JButton("" + i));
            }
        }
    }

    private void initComponents() {
        jPanel = new JPanel(new MigLayout("debug"));
        jButton = new JButton("Test");  
    }
}

2条回答
甜甜的少女心
2楼-- · 2019-02-17 06:19
We Are One
3楼-- · 2019-02-17 06:24

I've been declaring the .width("xx%") and then calling wrap when the row's widths add up to 100%. This works if you can declare each component to be a certain percentage of a row (they don't all have to be the same percentage).

查看更多
登录 后发表回答