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%。看看最後的結果:
正如你所看到的,settingsPanel
比canvasPanel
程較長。我錯過了什麼嗎?
在此先感謝!
你在開發一個遺傳算法應用嗎? :) – HCarrasko
@神父阿哈哈,是的! :) – Doon
如果我沒有記錯,它是'[grow,70%]' – kleopatra