MigLayout具有绝对像元坐标和跨度的意外布局

伊波达米亚

我有一个JPanel,可以在其中动态添加和删除不同大小的子面板。因此,我将MigLayout与绝对单元格坐标一起使用。在以下情况下,结果不是我期望的。

说明问题的我的代码的简化版本如下:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class MigLayoutDemo
{
    private static final int CELL_WIDTH = 60;
    private static final int CELL_HEIGHT = 50;

    public static JPanel createBigPanel()
    {
        JPanel bigSubPanel = new JPanel();
        bigSubPanel.setPreferredSize(new Dimension(3 * CELL_WIDTH, 2 * CELL_HEIGHT));      
        bigSubPanel.setBorder(BorderFactory.createLineBorder(Color.black));       
        bigSubPanel.setBackground(Color.LIGHT_GRAY);
        return bigSubPanel;
    }

    public static JPanel createSmallPanel()
    {
        JPanel smallSubPanel = new JPanel();
        smallSubPanel.setPreferredSize(new Dimension(2 * CELL_WIDTH, CELL_HEIGHT));      
        smallSubPanel.setBorder(BorderFactory.createLineBorder(Color.black));     
        smallSubPanel.setBackground(Color.LIGHT_GRAY);
        return smallSubPanel;
    }

    public static JPanel createCellPanel()
    {
        JPanel cellSubPanel = new JPanel();
        cellSubPanel.setPreferredSize(new Dimension(CELL_WIDTH, CELL_HEIGHT));      
        cellSubPanel.setBackground(Color.LIGHT_GRAY);
        return cellSubPanel;
    }

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

        // main panel where the sub-panels are placed in
        JPanel mainPanel = new JPanel();
        MigLayout mg = new MigLayout("debug, gap 0 0");        
        mainPanel.setLayout(mg);

        mainPanel.add(createBigPanel(), "cell 0 0 3 2"); // [col row [span x [span y]]] 
        mainPanel.add(createBigPanel(), "cell 3 0 3 2");
        mainPanel.add(createSmallPanel(), "cell 6 0 2 1, align left top");
        mainPanel.add(createSmallPanel(), "cell 6 1 2 1, align left top");

        mainPanel.add(createSmallPanel(), "cell 0 2 2 1, align left top");
        mainPanel.add(createBigPanel(), "cell 2 2 3 2");       
        // problem occurs
        mainPanel.add(createBigPanel(), "cell 5 2 3 2");

//        mainPanel.add(createCellPanel(), "cell 0 4 1 1");
//        mainPanel.add(createCellPanel(), "cell 1 4 1 1");
//        mainPanel.add(createCellPanel(), "cell 2 4 1 1");
//        mainPanel.add(createCellPanel(), "cell 3 4 1 1");
//        mainPanel.add(createCellPanel(), "cell 4 4 1 1");
//        mainPanel.add(createCellPanel(), "cell 5 4 1 1");
//        mainPanel.add(createCellPanel(), "cell 6 4 1 1");
//        mainPanel.add(createCellPanel(), "cell 7 4 1 1");

        frame.getContentPane().add(mainPanel);
        frame.setResizable(false);
        frame.pack();
    }


    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

如果我删除代码行,则mainPanel.add(createBigPanel(), "cell 5 2 3 2");一切都与我预期的一样。但是,当我在第5列和第2行中添加面板时,看起来该面板位于第5列和第6列之间的额外列中。

如果我也添加代码行

mainPanel.add(createCellPanel(), "cell 0 4 1 1");
mainPanel.add(createCellPanel(), "cell 1 4 1 1");
mainPanel.add(createCellPanel(), "cell 2 4 1 1");
mainPanel.add(createCellPanel(), "cell 3 4 1 1");
mainPanel.add(createCellPanel(), "cell 4 4 1 1");
mainPanel.add(createCellPanel(), "cell 5 4 1 1");
mainPanel.add(createCellPanel(), "cell 6 4 1 1");
mainPanel.add(createCellPanel(), "cell 7 4 1 1");

该问题似乎已“解决”,并且所有内容都放置在正确的列和行中。

我怀疑我的问题与细胞的跨度有关。我是否缺少一些MigLayout约束或参数?还是MigLayout库中的错误?

我目前在Java 7中使用MigLayout V4.0。我还使用V5.0快照测试了我的代码,但结果是相同的。

伊波达米亚

对我有用的解决方案是在MigLayout中使用绝对定位,即采用以下形式的组件约束pos x y [x2] [y2]

import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;


public class MigLayoutDemo4 extends JFrame {

    private static final int CELL_WIDTH = 60;
    private static final int CELL_HEIGHT = 50;

    public MigLayoutDemo4() {

        initUI();
    }

    private void initUI() {

        JPanel pnl = new JPanel(new MigLayout("gap 0 0"));


        pnl.add(createLabel("One"), "pos 0 0"); 
        pnl.add(createLabel("Two"), "pos "+(CELL_WIDTH * 3)+" "+(CELL_HEIGHT * 0));
        pnl.add(createLabel("Three"), "pos "+(CELL_WIDTH * 6)+" "+(CELL_HEIGHT * 0));
        pnl.add(createLabel("Four"), "pos "+(CELL_WIDTH * 6)+" "+(CELL_HEIGHT * 1));
        pnl.add(createLabel("Five"), "pos "+(CELL_WIDTH * 0)+" "+(CELL_HEIGHT * 2));
        pnl.add(createLabel("Six"),"pos "+(CELL_WIDTH * 2)+" "+(CELL_HEIGHT * 2));       
        pnl.add(createLabel("Seven"), "pos "+(CELL_WIDTH * 5)+" "+(CELL_HEIGHT * 2));

        add(pnl);
        pack();

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }

    private JLabel createLabel(String text) {

        JLabel lbl = new JLabel(text, JLabel.CENTER);
        lbl.setBorder(BorderFactory.createEtchedBorder());

        return lbl;

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigLayoutDemo3 ex = new MigLayoutDemo3();
                ex.setVisible(true);
            }
        });       
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从LatLng获取绝对像素坐标

绝对锚标签与具有绝对跨度的锚标签

具有四元数和欧拉角,如何计算测量设备的绝对角(使用python)?

kivy:固定布局,仅具有绝对定位

具有位置的引导容器:绝对会丢失内部布局

具有位置的CSS3布局:绝对压低内容

具有本征和三元运算符的意外/意外编译器魔术师

具有相对和绝对定位的元素

如何找到具有初始坐标和距离的新坐标?

具有相同宽度和高度的跨度内的中心字符

具有不同 x 跨度和较小尺寸标记的 Matplotlib

跨度应具有子img的宽度和高度

具有Listview和布局的QuickReturnPattern

如何在具有相对位置的身体特定坐标中定位绝对元素?

具有TextInputlayout布局的EditText具有提示和边框

具有绝对位置的图像在 flex 布局中没有宽度

R:从数据框创建栅格对象,该栅格对象具有栅格像元的x和y坐标的最小值和最大值

进入iPhone的绝对像素位置

具有绝对定位元素的列之间的意外间距

如果使用绝对路径运行,Bash脚本将具有意外行为

的回波具有三元运算符,该运算符是跨度

SVG,静态,绝对,相对和坐标

行跨度和列跨度最佳设计布局

具有隐藏溢出的 flex 布局中的绝对定位按钮(带滚动)

具有数字的跨度和具有相同行高的浮动文本

具有跨度的内联LI

ITK中具有稀疏和连续坐标的图像

生成具有最小距离的随机x和y坐标

ggplot排序轴具有翻转的坐标和多面图