Miglayout将组件对齐到单元格顶部

时间:2014-07-06 18:53:33

标签: miglayout

我有一组按钮,它们只停留在面板中间。我试图将它们放置在面板顶部的流动布局约束中。非常感谢任何帮助实现这一目标。我试过停靠并对齐但没有做任何事情。

private class ProductPanel extends JPanel {

    private JLabel lblProd;
    private JButton butAdd;
    private JButton butRemove;
    private JButton butEdit;
    private Product_Table_Model ptm;
    private JScrollPane scroll;
    private JPanel buttonPanel;
    private JTable table;

    ProductPanel() {

        setLayout(new MigLayout("debug"));

        ptm = new Product_Table_Model(
                (ArrayList<Product>) client
                        .receiveObject("Get_Product_Data"));

        initComponents();

    }

    public void initComponents() {

        lblProd = new JLabel ("Product List: ");

        buttonPanel = new JPanel (new MigLayout());
        butAdd = new JButton ("Add");
        butRemove = new JButton ("Remove");
        butEdit = new JButton ("Edit");
        buttonPanel.add(butAdd, "cell 0 0");
        buttonPanel.add(butRemove, "cell 0 1");
        buttonPanel.add(butEdit, "cell 0 2");

        butAdd.setPreferredSize(new Dimension(40, 50));

        add(lblProd, "wrap");

        table = new JTable(ptm);
        table.setFillsViewportHeight(true);
        scroll = new JScrollPane(table);

        add(scroll);
        add(buttonPanel);


    }

}

1 个答案:

答案 0 :(得分:2)

按预期,按钮与顶部对齐。小组本身就是这样 没有在其细胞内对齐。因此,以下将解决问题:

add(buttonPanel, "top");

您不应该使用setPrefferedSize()方法设置优先大小。 相反,请使用大小组。

MigLayout是非常强大的布局管理器,您不需要创建两个 面板有两个布局管理器。可以更轻松地创建布局。 以下示例就是一个这样的解决方案:

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import net.miginfocom.swing.MigLayout;

public class ProductPanel2 extends JPanel {

    private JLabel lblProd;
    private JButton butAdd;
    private JButton butRemove;
    private JButton butEdit;
    private JScrollPane scroll;
    private JTable table;

    public ProductPanel2() {

        initComponents();
    }

    private void initComponents() {

        setLayout(new MigLayout());

        lblProd = new JLabel("Product List: ");

        butAdd = new JButton("Add");
        butRemove = new JButton("Remove");
        butEdit = new JButton("Edit");

        table = new JTable();
        table.setFillsViewportHeight(true);
        scroll = new JScrollPane(table);        

        add(lblProd, "wrap");
        add(scroll);
        add(butAdd, "split 3, flowy, top, sgx");
        add(butRemove, "sgx");
        add(butEdit, "sgx");        
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                ProductPanel2 pane = new ProductPanel2();
                frame.setContentPane(pane);
                frame.setSize(350, 250);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });        

    }
}

表格旁边的单元格分为三个子单元格。三个按钮是 插入这些子电池。使用flowy设置垂直流模式 约束。 top约束将按钮对齐到顶部。最后, sgx约束使按钮大小相同。

Snapshot of the example