2013-12-09 28 views
0

我想尝试使用miglayout,因为它更灵活。 我尝试添加日期和一些按钮,但在使用换行后,按钮之间的差距是库存和事务变得很远,但在事务按钮和添加项按钮之间没问题。MigLayout对齐

这是我的代码:

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月9日15年9月13日
[库存] [交易] [添加项目]基于

+1

请出示演示行为的SSCCE(与你想要达到的目标) – kleopatra

回答

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(); 
      } 
     }); 
    } 
} 

目前您的顶部面板看起来是这样的:

enter image description here

有几种方式来实现自己的目标,你只需要玩的约束。举例来说,你可以做如下:

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")); 

,你会看到这样的事情:

enter image description here

或者你可以在第一行定义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

+0

thankx :)它解决我的问题.. –

+0

@HazimAli欢迎您:)如果这个答案适用于你,那么不要忘记将其标记为已接受:** [接受答案如何工作?](http://meta.stackexchange.com/a/5235/233365)** – dic19