且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MigLayout:相对于屏幕尺寸调整组件的大小

更新时间:2021-09-22 22:23:56

可以在 MigLayout 中直接使用 % width 试试这个例子:

It's possible to use directly % width inside MigLayout try this example :

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;

public class Main
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 500, 500);

        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new MigLayout("insets 4 4 4 4",
                "[fill,30%][fill,40%][fill,30%]", "[fill,grow]"));

        JMenuBar menubar = new JMenuBar();
        JMenu menu = new JMenu("File");
        JMenuItem item = new JMenuItem("Open");
        menu.add(item);
        menubar.add(menu);
        frame.setJMenuBar(menubar);

        contentPanel.add(new JScrollPane());
        contentPanel.add(new JScrollPane(new JEditorPane("text/plain", "Hello World")));
        contentPanel.add(new JScrollPane());
        frame.setContentPane(contentPanel);
        frame.setVisible(true);
    }
}

要完全理解如何使用 MigLayout,请按照此 白皮书.

To fully undestand how to use MigLayout follow this Whitepaper.