Java MigLayout垂直对齐

时间:2013-11-26 16:29:57

标签: java swing vertical-alignment miglayout

我正在尝试利用MigLayout布局管理器创建一个GUI,允许用户将项目从一个列表移动到另一个列表。我需要箭头靠近(垂直)。我遇到的问题是顶部箭头位于单元格的顶部,我试图将其向下移动到底部是不成功的。我正在尝试MigLayout的Cell功能,但我将使用任何有用的功能。谢谢。

public class StackCode extends JPanel{
public StackCode(){
    super(new MigLayout());
    JButton run = new JButton("Okay");
    JButton cancel = new JButton("Cancel");

    JList uList = new JList(new String[]{"test1","test2","test3","test4","test5"});
    JList nList = new JList();
    uList.setBorder(new LineBorder(Color.black));
    nList.setBorder(new LineBorder(Color.black));
    uList.setPreferredSize(new Dimension(100,150));
    nList.setPreferredSize(new Dimension(100,150));

    add(run,"cell 0 0");
    add(cancel,"cell 1 0 4 1");
    add(new JLabel("List1"),"cell 0 1 2 1"); // List1 title label -- cell column row width height
    add(new JLabel("List2"),"cell 4 1 2 1"); // List2 title label
    add(uList,"cell 0 2 2 5"); // JList1
    add(nList,"cell 4 2 2 5"); // JList 2
    add(new JLabel("-->"),"cell 3 3 1 3, align center"); // eventually import arrow image
    add(new JLabel("<--"),"cell 3 6 1 1, align center"); // eventually import arrow image

}
private static void createAndShowGUI(){
    //Create and set up the window.
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    StackCode newContentPane = new StackCode();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

Desired Graphics 编辑为包含所需图形的图像。

1 个答案:

答案 0 :(得分:3)

我能够通过将其添加到super(new MigLayout())片段

来弄清楚如何实现这一目标
super(new MigLayout(
            "",
            "",
            "[center][center][b][top]"
            ));
// This sets the 1st/2nd row to center aligned, 3rd row to bottom aligned and the
// 4th row to top aligned.

并改变这一点:

add(new JLabel("-->"),"cell 3 3 1 3, align center");
add(new JLabel("<--"),"cell 3 6 1 1, align center");

要:

add(new JLabel("-->"),"cell 3 3 1 1, align center");
add(new JLabel("<--"),"cell 3 4 1 1, align center");