MigLayout列约束 [英] MigLayout column constraint

查看:130
本文介绍了MigLayout列约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发类似的应用程序布局:

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

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

为此,我正在使用MigLayout。根据这个备忘单,要设置列重,我们应该使用 【重量】。但是,它不能以某种方式工作。看看我的代码:

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);
}

}

我认为使用此配置设置框架[grow 70] [grow 30] Panel canvasPanel 应该是屏幕宽度的70%,而 settingsPanel 应为30%。看看最终结果:

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:

如您所见, settingsPanel canvasPanel更长。我错过了什么吗?

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

提前致谢!

推荐答案

你的问题是对约束语法的误解:

Your problem is a misunderstanding of constraint syntax:

grow 70

是单一约束,该数字定义了列获得额外空间的渴望的相对权重。要在您的示例中查看它,请增加框架的宽度:附加宽度在两列之间以70:30分布。

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%

是两个约束,第一个定义增长行为(默认权重为100)和第二个将宽度定义为百分比。

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

这篇关于MigLayout列约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆