我正在尝试开发这样的应用程序布局:
* +------------------+--------+
* | | |
* | | |
* | | |
* | 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]"
Panel canvasPanel
应该是屏幕宽度的 70%,而 settingsPanel
应该是 30%。看看最终结果:
如您所见,settingsPanel
比 canvasPanel
长很多。我错过了什么吗?
提前致谢!
最佳答案
你的问题是对约束语法的误解:
grow 70
是单一约束,数字定义列渴望获得额外 空间的相对权重。要在您的示例中看到它,请增加框架的宽度:额外的宽度在两列之间以 70 : 30 的比例分布。
grow, 70%
有两个约束,第一个定义增长行为(默认权重为 100),第二个定义宽度百分比。
关于java - MigLayout 列约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001696/