MigLayout跨越从单元溢出的组件

时间:2014-04-02 02:20:29

标签: java swing layout-manager miglayout

我尝试在MigLayout中创建类似于以下内容的布局:

基本上是3列:一个按钮,一个标签,以及一个列表(跨越2列)的其他组件和一个文本区域(应该与第三列中的其他组件对齐)位于底部。诀窍是按钮列跨越最后两行的所有行,并将其分成8(每个按钮一个)。但是我最终得到了与列表重叠的按钮,并且通过调试可以看到按钮实际上已经从它们的单元格溢出。当我将按钮放在另一个面板中然后将其添加到主面板时,仍会发生这种情况。

添加胶水(或其他一些实际上不可见的组件)会导致在调整窗口大小时按钮和底部组件之间出现间隙(我希望底部组件占用所有额外空间)。

是否有某种方法可以将底部组件推到按钮下方,以便从调整大小中获得额外的空间?

会发布截图但我的第一篇帖子所以我没有代表感谢mKorbel!)

enter image description here

代码:

import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class MigLayoutTest extends JPanel
{
    private MigLayoutTest()
    {
        JFrame frame = new JFrame("MigLayout Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(new JScrollPane(getPage()));
        frame.getContentPane().setMinimumSize(new Dimension(650, 336));
        frame.getContentPane().setPreferredSize(new Dimension(890, 562));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel getPage()
    {
        JPanel panel = new JPanel(new MigLayout("fill, wrap 3, debug", "[][][grow, fill]"));
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));

        // To add buttons directly to panel uncomment the commented out lines below and comment out each line that references listButtonPanel
        JPanel listButtonPanel = new JPanel(new MigLayout("ins 0, wrap 1, aligny top"));
        Dimension btnSize = new Dimension(105, 25);
        JButton addBtn = new JButton("Add");
        addBtn.setPreferredSize(btnSize);
        listButtonPanel.add(addBtn);
        // panel.add(addBtn, "spany 4, split 8, flowy");

        JButton removeBtn = new JButton("Remove");
        removeBtn.setPreferredSize(btnSize);
        listButtonPanel.add(removeBtn);
        // panel.add(removeBtn);

        JButton copyBtn = new JButton("Copy");
        copyBtn.setPreferredSize(btnSize);
        listButtonPanel.add(copyBtn);
        // panel.add(copyBtn)

        panel.add(listButtonPanel, "spany 2, aligny top, hmax 100%");

        JTextField txtField = new JTextField();
        JLabel label = new JLabel("Property 1");
        label.setLabelFor(txtField);
        panel.add(label, "alignx right");
        panel.add(txtField);

        JComboBox comboBox = new JComboBox(new String[] {"cbx itm 1", "cbx itm 2", "cbx itm 3"});
        comboBox.setEditable(true);
        comboBox.setSelectedItem("");
        label = new JLabel("ComboBox Property");
        label.setLabelFor(comboBox);
        panel.add(label, "alignx right");
        panel.add(comboBox);

        panel.add(new JLabel("A big JList"), "spanx 2, grow");
        panel.add(new JLabel("A big JTextArea"));

        JList list = new JList(new DefaultListModel());
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setVisibleRowCount(-1);
        String[] names = new String[] {"Stuff to", "fill this", "JList..."};
        DefaultListModel model = (DefaultListModel)list.getModel();
        for (String name : names)
            model.addElement(name);
        JScrollPane scroller = new JScrollPane(list);
        scroller.setMinimumSize(new Dimension(213, 100));
        scroller.setPreferredSize(new Dimension(213, 100));
        panel.add(scroller, "spanx 2, grow, pushy");

        JTextArea textArea = new JTextArea();
        scroller = new JScrollPane(textArea);
        scroller.setPreferredSize(new Dimension(100, 100));
        panel.add(scroller, "grow, pushy");

        return panel;
    }

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

2 个答案:

答案 0 :(得分:1)

只要面板具有静态行数,托马斯的答案就会起作用,遗憾的是我们的程序没有。为了解决这个问题,我会跟踪我要创建的行数,然后在最后动态生成行约束。 (这有点乱,但没有我想象的那么糟糕。如果有办法避免行限制,我会相应地更新代码。)我还在按钮面板旁边的组件和列表和文本字段之间使用了一个虚拟行在底部占据额外的空间,保持一切正确对齐而不需要调整大小。

import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class MigLayoutTest extends JPanel
{
    private MigLayoutTest(boolean addExtraRow1, boolean addExtraRow2)
    {
        JFrame frame = new JFrame("MigLayout Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(new JScrollPane(getPage(addExtraRow1, addExtraRow2)));
        frame.getContentPane().setMinimumSize(new Dimension(650, 336));
        frame.getContentPane().setPreferredSize(new Dimension(890, 562));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel getPage(boolean addExtraRow1, boolean addExtraRow2)
    {
        // Create buttons panel
        JPanel listButtonPanel = new JPanel(new MigLayout("ins 0, wrap 1, aligny top"));
        JButton addBtn = new JButton("Add");
        listButtonPanel.add(addBtn, "w 105px, h 25px, sg btns");

        JButton removeBtn = new JButton("Remove");
        listButtonPanel.add(removeBtn, "sg btns");

        JButton copyBtn = new JButton("Copy");
        listButtonPanel.add(copyBtn, "sg btns");

        // Create other components
        int rowCount = 1; // make a dummy row at the bottom to push all the components beside the button panel up

        JTextField txtField = new JTextField();
        rowCount++;

        JComboBox comboBox = new JComboBox(new String[] {"cbx itm 1", "cbx itm 2", "cbx itm 3"});
        comboBox.setEditable(true);
        comboBox.setSelectedItem("");
        rowCount++;

        JCheckBox checkBox = null;
        if (addExtraRow1)
        {
            checkBox = new JCheckBox();
            rowCount++;

        }

        JTextField optTxtField = null;
        if (addExtraRow2)
        {
            optTxtField = new JTextField();
            rowCount++;
        }

        rowCount++;

        JList list = new JList(new DefaultListModel());
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setVisibleRowCount(-1);
        String[] names = new String[] {"Stuff to", "fill this", "JList..."};
        DefaultListModel model = (DefaultListModel)list.getModel();
        for (String name : names)
            model.addElement(name);

        JTextArea textArea = new JTextArea();

        // Generate row constraints
        StringBuilder rowConstraints = new StringBuilder();
        for (int i=0; i<rowCount; i++)
            rowConstraints.append("[]");
        rowConstraints.append("[grow, fill]");

        // Create main panel and add components
        JPanel panel = new JPanel(new MigLayout("fill, wrap 3, debug", "[][][grow, fill, align left]", rowConstraints.toString()));
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));

        panel.add(listButtonPanel, "spany " + --rowCount + ", aligny top, hmax 100%"); // decrement rowCount because the buttons should be above the bottom components' labels

        addComponent(panel, new JLabel("Property 1"), txtField);
        addComponent(panel, new JLabel("ComboBox Property"), comboBox);
        if (checkBox != null)
            addComponent(panel, new JLabel("Extra comp 1"), checkBox);
        if (optTxtField != null)
            addComponent(panel, new JLabel("Extra comp 2"), optTxtField);

        panel.add(new JLabel("A big JList"), "skip 3, spanx 2, grow"); // skip the dummy row before adding this
        panel.add(new JLabel("A big JTextArea"));

        panel.add(new JScrollPane(list), "hmin 100px, spanx 2, grow");
        panel.add(new JScrollPane(textArea), "hmin 100px, grow");

        return panel;
    }

    private void addComponent(JPanel panel, JLabel label, Component component)
    {
        label.setLabelFor(component);
        panel.add(label, "align right");
        panel.add(component);
    }

    public static void main(final String[] args)
    {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() { new MigLayoutTest(args.length > 1, args.length == 2); }
        });
    }
}

答案 1 :(得分:0)

我认为“分裂和跨度单元按钮面板”与推动混合起来了。对不起,我不知道为什么。一些可选提示:

  • 我喜欢单独声明,初始化和添加所有组件。我认为它更易读,更易于维护。
  • 避免使用硬编码的setSize函数。我不记得我曾经得到过我想要的东西。我喜欢与MigLayout的sizegroup约束。
  • 不要将变量用于两个对象实例(例如滚动条)。它通常会导致使用对象引用的错误。

这是另一种可能的解决方案(我希望这仍然满足您的要求):

    private JPanel getPage() {
    JPanel panel = new JPanel(new MigLayout("wrap 3, debug", "[][][grow, fill]", "[][][][grow, fill]"));
    panel.setBorder(new EmptyBorder(10, 10, 10, 10));

    JButton addBtn, removeBtn, copyBtn;
    JTextField txtField;
    JTextArea textArea;
    JComboBox<String> comboBox;
    JList<String> list;
    JLabel property, comboboxLabel, listLabel, textAreaLabel;
    JScrollPane listScroller, textFieldScroller;

    addBtn = new JButton("Add");
    removeBtn = new JButton("Remove");
    copyBtn = new JButton("Copy");
    txtField = new JTextField();
    textArea = new JTextArea();
    property = new JLabel("Property 1");
    listLabel = new JLabel("A big JList");
    textAreaLabel = new JLabel("A big JTextArea");
    comboboxLabel = new JLabel("ComboBox Property");


    comboBox = new JComboBox<String>(new String[] { "cbx itm 1", "cbx itm 2", "cbx itm 3" });
    comboBox.setEditable(true);
    comboBox.setSelectedItem("");

    list = new JList<String>();
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setVisibleRowCount(-1);

    String[] names = new String[] { "Stuff to", "fill this", "JList..." };
    DefaultListModel<String> model = new DefaultListModel<String>();
    for (String name : names) {
        model.addElement(name);
    }
    list.setModel(model);


    listScroller = new JScrollPane(list);
    textFieldScroller = new JScrollPane(textArea);

    property.setLabelFor(txtField);
    comboboxLabel.setLabelFor(comboBox);
    textAreaLabel.setLabelFor(textArea);
    listLabel.setLabelFor(list);


    panel.add(addBtn, "split 3, flowy, spany 2, sizegroup buttons");
    panel.add(removeBtn, "sizegroup buttons");
    panel.add(copyBtn, "sizegroup buttons");

    panel.add(property, "al right top, sgy components");
    panel.add(txtField, "al right top, sgy components");
    panel.add(comboboxLabel, "al right top, sgy components");
    panel.add(comboBox, "al right top, sgy components");

    panel.add(listLabel, "spanx 2, sgy components");
    panel.add(textAreaLabel, "sgy components");
    panel.add(listScroller, "spanx 2, grow");
    panel.add(textFieldScroller, "");

    return panel;
}