【问题标题】:how to use pos in MigLayout?如何在 MigLayout 中使用 pos?
【发布时间】:2014-06-10 04:05:45
【问题描述】:

这是我的代码:

public class InfoPanel extends JPanel{
... ... ...
... ... ...

public InfoPanel(){
... ... ...
... ... ...
MigLayout lManager = new MigLayout();
setLayout(lManager);

add(lblName,new CC().pos("50", "80"));
add(txtName, new CC().pos("90","80").width("170").wrap().gap("r"));
add(lblOccupation,"pos 20 100");
add(txtOccupation,"pos 91 100,w 170,wrap,gap r");

它有正确的输出。 但我想使用任何内置代码为txtName、txtOccupation 计算pos()。 任何人都可以帮助我做到这一点。

【问题讨论】:

  • “内置代码”是什么意思?通常你不需要从 MigLayout 调用方法。您只需将约束添加为字符串,其余的由 MigLayout 完成。你能更准确地描述你想要什么吗?
  • 是的,对于 txtName(JTextField) 我使用 pos 90 80,我为 x 手动计算 90,是否可以不手动计算。例如,我们可以在 SpringLayout 中做到这一点。就是这样。
  • 真的要使用绝对定位吗?因为通常您只需将组件彼此相对放置,而 layoutManager 会完成其余的工作。我总是对绝对定位有不好的体验,因为它很难维护。描述你想要的结果,也许没有绝对定位有更好的解决方案。
  • 是的,你说得对。正如您所说,“因为通常您只需将组件彼此相对放置,其余部分由 layoutManager 完成”。你能写一些代码来完成我已经写过的工作吗?谢谢。
  • 当然可以。但是我需要更多关于你想要什么的信息。我看到的是某些绝对位置的 4 个组件。

标签: miglayout


【解决方案1】:

以图片为模板:

public class MigLayoutTest {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new MigLayoutTest().start();
        }
    });
}

private void start() {
    JFrame frame = new JFrame();

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new MigLayout("wrap 4, debug", "[][fill, grow 1][][fill, grow 4]", ""));


    //Declaration
    JLabel nameLabel, employmenLabel, ssnLabel, taxIDLabel;
    JTextField nameField, ssnField, taxIDField;
    JComboBox<String> employmentBox;
    JCheckBox checkbox;
    JButton save, cancel, upload;
    JPanel imagePlaceholder;


    //Initialization
    nameLabel = new JLabel("Name");
    employmenLabel = new JLabel("Employment");
    ssnLabel = new JLabel("Social Security Number");
    taxIDLabel = new JLabel("tax ID");
    nameField = new JTextField();
    ssnField = new JTextField();
    taxIDField = new JTextField();
    save = new JButton("Save");
    cancel = new JButton("Cancel");
    upload = new JButton("Upload");
    checkbox = new JCheckBox("Checkbox");
    imagePlaceholder = new JPanel();
    employmentBox = new JComboBox<String>();


    //Some editing
    imagePlaceholder.setBackground(Color.GREEN);

    employmentBox.addItem("Some");
    employmentBox.addItem("items");
    employmentBox.addItem("to");
    employmentBox.addItem("fill");
    employmentBox.addItem("this");


    //Adding to contentPane
    contentPane.add(nameLabel, "alignx right");
    contentPane.add(nameField, "");
    contentPane.add(ssnLabel, "alignx right");
    contentPane.add(ssnField, "");
    contentPane.add(employmenLabel, "alignx right");
    contentPane.add(employmentBox, "");
    contentPane.add(upload, "");
    contentPane.add(imagePlaceholder, "spany 3, grow");
    contentPane.add(checkbox, "split 2");
    contentPane.add(taxIDLabel, "");
    contentPane.add(taxIDField, "wrap");

    contentPane.add(save, "");
    contentPane.add(cancel, "growx 0, wrap");



    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2014-01-13
    • 1970-01-01
    • 2012-03-01
    • 2011-01-04
    • 1970-01-01
    • 2011-05-27
    • 2014-02-16
    • 1970-01-01
    相关资源
    最近更新 更多