java - MigLayout列约束

原文 标签 java swing layout-manager miglayout

我正在尝试开发这样的应用程序布局:

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


为此,我正在使用MigLayout。根据此cheat sheet,要设置列权,请使用[weight]。但是,它不起作用。看一下我的代码:

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]"面板canvasPanel设置框架应该是屏幕宽度的70%,而settingsPanel应该是屏幕宽度的30%。看一下最终结果:



如您所见,settingsPanelcanvasPanel长。我想念什么吗?

提前致谢!

最佳答案

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

grow 70


是单个约束,该数字定义列渴望获得更多空间的相对权重。要在您的示例中看到它,请增加框架的宽度:附加宽度在两列之间分配为70:30。

grow, 70%


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

相关文章:

java - 应该抽象类中LinkedHashMap的初始容量始终为零吗?

java - NetBeans java.lang.ClassNotFoundException

java - Java Swing-Motif L&F中自定义组合框渲染器中的文本消失

java - GridBagLayout中图像大小不正确

java - getOpenSessions()给出空的[]值

java - 对HashSet使用float加载因子有什么作用?

java - 在HTML样式的JtextPane的文本中使所选单词加粗吗?

java - 如何在JScrollPane中立即显示滚动条

java - 如何使用BoxLayout使组件适合父级宽度?

java - JScrollPane没有出现