我试图利用MigLayout布局管理器创建一个GUI,该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();
}
});
}
}
编辑以包括所需图形的图像。
最佳答案
通过将其添加到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");
关于java - Java MigLayout垂直对齐,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20222610/