2014-04-11 54 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),第二個定義寬度爲百分比。