MigLayout column constraint

2019-06-07 21:34发布

问题:

I'm trying to develop an application layout like that:

 * +------------------+--------+
 * |                  |        |
 * |                  |        |
 * |                  |        |
 * |       70%        |  30%   |
 * |                  |        |
 * |                  |        |
 * |                  |        |
 * |                  |        |
 * |                  |        |
 * +------------------+--------+

To do this, I'm using MigLayout. According to this cheat sheet, to set the column weight we shold use [weight]. However, it isn't working somehow. Take a look at my code:

package com.test;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import net.miginfocom.swing.MigLayout;

public class MainWindow {

private JFrame frame;
private JTextField iterationsTextField;
private JTextField populationSizeTextField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new MigLayout("", "[grow 70][grow 30]", "[grow]"));

    JPanel canvasPanel = new JPanel();
    frame.getContentPane().add(canvasPanel, "cell 0 0,grow");
    canvasPanel.setLayout(new MigLayout("", "[]", "[]"));

    JPanel settingsPanel = new JPanel();
    settingsPanel.setBorder(new TitledBorder(null, "Settings", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    frame.getContentPane().add(settingsPanel, "cell 1 0,grow");
    settingsPanel.setLayout(new MigLayout("", "[][grow]", "[][]"));

    JLabel iterationsLabel = new JLabel("Iterations");
    settingsPanel.add(iterationsLabel, "cell 0 0,alignx trailing");

    iterationsTextField = new JTextField();
    iterationsTextField.setText("1000");
    settingsPanel.add(iterationsTextField, "cell 1 0,growx");
    iterationsTextField.setColumns(10);

    JLabel populationSizeLabel = new JLabel("Population");
    settingsPanel.add(populationSizeLabel, "cell 0 1,alignx trailing");

    populationSizeTextField = new JTextField();
    settingsPanel.add(populationSizeTextField, "cell 1 1,growx");
    populationSizeTextField.setColumns(10);
}

}

I think that setting the frame using this configuration "[grow 70][grow 30]" the Panel canvasPanel should be 70% of the screen width, while settingsPanel should be 30%. Take a look at the final result:

As you can see, the settingsPanel is WAY longer than the canvasPanel. Am I missing something?

Thanks in advance!

回答1:

Your problem is a misunderstanding of constraint syntax:

grow 70

is single constraint, the number defines a relative weight of the column's eagerness to get extra space. To see it in your example, increase the width of the frame: the additional width gets distributed 70 : 30 between the two columns.

grow, 70%

are two constraints, the first defining the grow behaviour (with a default weight of 100) and the second defining the width as a percentage.