【问题标题】:MigLayout alignmentMigLayout 对齐
【发布时间】:2013-12-26 03:37:39
【问题描述】:

我想尝试使用 miglayout,因为它更灵活。 我尝试添加日期和一些按钮,但是在我使用 wrap 之后,按钮之间的差距是库存和交易变得很远,但交易按钮和添加项目按钮之间是可以的。

这是我的代码:

    top = new JPanel();
    top.setLayout(new MigLayout("","",""));
    center = new JPanel();
    bottom = new JPanel();
    right = new JPanel();
    left = new JPanel();

    inventory = new JButton("Inventory");
    transaction = new JButton("Transaction");
    addItem = new JButton("Add Item");

    date = new Date();
    dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    dateTime = new JLabel(dateFormat.format(date));

    top.add(dateTime,"wrap");
    dateTime.setBorder(BorderFactory.createLineBorder(Color.red));
    top.add(inventory);
    inventory.setBorder(BorderFactory.createLineBorder(Color.red));
    top.add(transaction);
    transaction.setBorder(BorderFactory.createLineBorder(Color.red));
    top.add(addItem);
    addItem.setBorder(BorderFactory.createLineBorder(Color.red));
    add(top,BorderLayout.NORTH);

显示:

---------------------------------------------- 2013/12/09 13.09.15
[库存] [交易] [添加项目]

【问题讨论】:

  • 请展示一个展示行为的SSCCE(与您想要实现的目标相比)

标签: java swing awt miglayout


【解决方案1】:

根据您的代码,我创建了SSCCE 以轻松显示您的问题。下次这个任务就交给你了。 注意:我已经删除了自定义边框和其他面板,因为它们与问题无关。

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;

public class Demo {    

    private void initGUI() {        
        JPanel top = new JPanel();
        top.setLayout(new MigLayout("","",""));

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        JLabel dateTime = new JLabel(dateFormat.format(new Date()));

        top.add(dateTime, "wrap");
        top.add(new JButton("Inventory"));
        top.add(new JButton("Transaction"));
        top.add(new JButton("Add Item"));

        JFrame frame = new JFrame("Demo");
        frame.add(top,BorderLayout.NORTH);        
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

目前您的顶部面板如下所示:

有几种方法可以实现你的目标,你只需要玩弄约束。例如,您可以执行以下操作:

top.add(dateTime, "wrap");
top.add(new JButton("Inventory"), "split 3"); // split the column in 3 cells here
top.add(new JButton("Transaction"));
top.add(new JButton("Add Item"));

你会看到这样的东西:

也可以在第一行定义3列,跨越3个单元格,如下:

JPanel top = new JPanel();
top.setLayout(new MigLayout("","[][][]")); // Use column constraints here
...
top.add(dateTime, "span 3, wrap"); // span 3 cells and then wrap
top.add(new JButton("Inventory"));
top.add(new JButton("Transaction"));
top.add(new JButton("Add Item"));

结果将与上一个相似。

欲了解更多信息,请查看Quick Start guide

【讨论】:

猜你喜欢
  • 2014-02-19
  • 2012-11-20
  • 2013-12-11
  • 2011-06-09
  • 2016-04-26
  • 1970-01-01
  • 2014-12-11
  • 2011-03-15
  • 2015-08-29
相关资源
最近更新 更多