有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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%。看看最终结果:

enter image description here

如您所见settingsPanelcanvasPanel长得多。我错过什么了吗

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

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

    grow 70
    

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

    grow, 70%
    

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