我正在尝试开发类似的应用程序布局:
* +------------------+--------+
* | | |
* | | |
* | | |
* | 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
更长。我错过了什么吗?
提前致谢!
答案 0 :(得分:5)
您的问题是对约束语法的误解:
grow 70
是单一约束,该数字定义了列获得额外空间的渴望的相对权重。要在示例中查看它,请增加框架的宽度:附加宽度在两列之间以70:30分布。
grow, 70%
是两个约束,第一个定义增长行为(默认权重为100),第二个定义宽度为百分比。