2014-04-11 55 views
1

我想开发一个应用程序布局类似: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

你在开发一个遗传算法应用吗? :) – HCarrasko

+2

@神父阿哈哈,是的! :) – Doon

+2

如果我没有记错,它是'[grow,70%]' – kleopatra

回答

5

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

grow 70 

是一个约束,数定义列的热心的相对权重来获得额外的空间。为了在你的例子中看到它,增加框架的宽度:额外的宽度在两列之间分配70:30。

grow, 70% 

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