How to control general panel size limits using MigLayout?

I am testing MigLayout for a project, and I cannot understand how MigLayout controls the size of the entire panel that is laid out. I am fitting an example into a document in MigLayout format . In addition, I write in Python and use Jython instead of writing in Java. However, here is my current code.

layout = MigLayout("fillx",
                   # Column constraints
                   "[right]rel[grow,fill]",
                   # Row constraints
                   "[]10[]10[]")
panel = JPanel(layout)

panel.add(JLabel("Enter size:"),
          "")
panel.add(JTextField(""),
          "wrap, width 150:250")
panel.add(JLabel("Enter weight:"),
          "")
panel.add(JTextField(""),
          "wrap, width 150:250")
panel.add(JButton("Ok"),
          "span 2, align center, width 100:150")

JFrame . , . , , , , , . , ( ) .

, , - ( -!), , . ?

+3
3

, (, (), . - , , . , , , - .

, - . - , , , ?

, , , MIG Layout, .

: , MIG Layout , MatrixLayout, , . MIG . MatrixLayout, .

+2

pack???

Update:

, . , 90%

  • ( )

  • Windows, , (, 800x600)

, ( ), , ( 10% )

Java Swing ( ), .

setPrefferedSize setMinimum , .

+2

, - .

  • JPanel panel , contentPane. contentPane. contentPane, .

  • panel , .. , , , / .

  • panel. .

  • panel contentPane: getContentPane().add(panel); contentPane.

  • , pack(), setVisible(true) . setSize(), setBounds() .. MigLayout. Viola!

A SCCEE:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class InsetsAndBorder extends JFrame {
    public InsetsAndBorder() {
        begin();
    }

    private void begin() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new MigLayout("insets 2 2 2 2, fillx, debug", "3[]3[]3[]3", "5[]5[]5[]5"));

        JLabel label1 = new JLabel("1");
        JLabel label2 = new JLabel("2");

        JButton button = new JButton("No way!");

        panel.add(label1, "cell 1 2, grow");

        panel.add(label2, "cell 2 2, grow");

        panel.add(button, "cell 0 1, grow");

        getContentPane().add(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                InsetsAndBorder frame = new InsetsAndBorder();

            }

        });
    }
}
0

Source: https://habr.com/ru/post/1714616/


All Articles