如何让MiGLayout表现得像Wrap Layout?

问题描述 投票:6回答:2

我想复制这里显示的示例:

Wrap Layout

使用MiGLayout。我尝试了一些组合,但是当容器缩小时,我很难将按钮自动换行到新行。

有人可以提供这样做的工作示例吗?

这是程序的shell:

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");  
    }
}
java swing layout miglayout
2个回答

0
投票

我一直在声明.width(“xx%”)然后在行的宽度加起来为100%时调用wrap。如果您可以将每个组件声明为一行的某个百分比(它们不必都是相同的百分比),则此方法有效。

© www.soinside.com 2019 - 2024. All rights reserved.