简体   繁体   English

MigLayout,为什么这个单元格使用了额外的空间?

[英]MigLayout, why is this cell the one using the extra space?

I have this panel which is using MigLayout to create cells for my content.我有这个面板,它使用 MigLayout 为我的内容创建单元格。 Here's the code portion:这是代码部分:

/ ====Layout====
        setLayout(new MigLayout("hidemode 3", "[400][0][400]", "[][][][][][]"));

        Border border = BorderFactory.createLineBorder(Color.WHITE, 0);
        //setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));
        // ====Components====

        // ===Information Displays===
        JLabel invName = new JLabel("inventory name");
        invName.setBorder(border);
        invName.setEnabled(false);
        add(invName, "cell 0 0, growx");
        JTextField displayName = new JTextField("display name");
        add(displayName, "cell 0 1, growx");
        JComboBox category = new JComboBox();
        category.setEditable(true);
        category.setModel(new DefaultComboBoxModel<>(new String[] {"Tops", "Skirts", "Pants", "Dresses", "Shorts"}));
        add(category, "cell 0 2, growx");
        JButton updateFields = new JButton("Update");
        add(updateFields, "cell 0 3");
        JLabel itemsLabel = new JLabel("Items in this Outfit:");
        itemsLabel.setBorder(border);
        add(itemsLabel, "cell 0 4, bottom, growy");
        JList<String> itemsInOutfit = new JList<>();
        itemsInOutfit.setVisibleRowCount(8);
        itemsInOutfit.setModel(new AbstractListModel<String>() {
            String values[] = {"item 1",
                                "item 2",
                                "item 3",
                                "item 4",
                                "item 5",
                                "item 6",
                                "item 7",
                                "item 8",
                                "item 9",
                                "item 10"
                                };
            @Override
            public int getSize() {
                return values.length;
            }

            @Override
            public String getElementAt(int index) {
                return values[index];
            }
        });
        JScrollPane scrollPane = new JScrollPane(itemsInOutfit);
        add(scrollPane, "cell 0 5, growx, bottom");
        JButton goToItem = new JButton("Go to Item");
        add(goToItem,"cell 0 6");

        // ===Items in Outfit JList===

        // ===Picture Box===

                // ---Picture Box buttons---

                JToolBar picturesBar = new JToolBar();

                JButton leftArrowBtn = new JButton();
                leftArrowBtn.setToolTipText("Previous Image");
                //leftArrowBtn.setIcon(new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/back.svg"));
                picturesBar.add(leftArrowBtn);

                JButton rightArrowBtn = new JButton();
                rightArrowBtn.setToolTipText("Next Image");
                //rightArrowBtn.setIcon(new FlatSVGIcon("com/formdev/flatlaf/demo/icons/forward.svg"));
                picturesBar.add(leftArrowBtn);

                JButton addImageBtn = new JButton();
                addImageBtn.setToolTipText("Add Image");
                addImageBtn.setIcon(UIManager.getIcon("Tree.openIcon"));
                picturesBar.add(addImageBtn);

                JButton deleteImageBtn = new JButton();
                deleteImageBtn.setToolTipText("Delete Image");
                deleteImageBtn.setIcon(UIManager.getIcon("Tree.openIcon"));
                picturesBar.add(deleteImageBtn);
                picturesBar.setFloatable(false);
                add(picturesBar, "cell 2 6, bottom, span");

                // ---Picture Box Display---
                JLabel pictureGallery = new JLabel();
                pictureGallery.setBorder(border);
                pictureGallery.setIcon(new ImageIcon(new ImageIcon("C:/users/saman/documents/projects/MyCloset/src/main/resources/the_storm_couple.jpg").getImage().getScaledInstance(400, -1, Image.SCALE_DEFAULT)));
                add(pictureGallery,"cell 2 0, span 1 6");

And here is how it is being rendered:以下是它的渲染方式:

I want the "Items in this Outfit:" label to sit right above the JList.我希望“这件衣服中的物品:”label 位于 JList 的正上方。 However, the cell that has the JList is the one that is the "big" one, so my attempt to tell the label to align bottom and go by that list has failed.但是,具有 JList 的单元格是“大”单元格,因此我试图通过该列表告诉 label 对齐底部和 go 失败了。 I've tried to manually set the heights of the rows and for the components when I add them to the panel with the cell code.当我使用单元格代码将它们添加到面板时,我尝试手动设置行和组件的高度。 Nothing seems to work.似乎没有任何效果。 And I also can't figure out how to put a border on each cell just to confirm my suspicions that it is in fact the JList cell being tall that is causing the problem.而且我也无法弄清楚如何在每个单元格上设置边框,只是为了确认我的怀疑,即实际上是 JList 单元格太高导致了问题。

What am I doing wrong here, how might I fix it?我在这里做错了什么,我该如何解决?

Thanks!谢谢!

I would suggest我会建议

setLayout(new MigLayout("hidemode 3", "[400][0][400]", "[][][][]push[][]"));

and remove the , bottom, growy for your itemsLabel.并删除您的 itemsLabel 的, bottom, growy 。

I haven't tested it, so no warranty;-) Also adding debug could help:我还没有测试过,所以没有保修;-) 添加调试也有帮助:

new MigLayout("debug, hidemode 3", ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM