miglayout:如何忽略组件高度

发布于 2024-10-17 10:38:29 字数 1462 浏览 4 评论 0原文

有没有办法告诉 MigLayout 忽略组件的高度?

这是一个测试示例:

在此处输入图像描述

我有一个超大组件(右上角的“大”按钮)第一行,和一个 JPanel,它有一个或多或少的三角形形状,横跨整个第二行。

我想让 MigLayout 在选择第一行的大小时忽略“大”按钮的高度,因为我知道它可以与我的第二行组件重叠。

我该怎么做?

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

public class IgnoreComponentHeight {
    public static void main(String[] args) {
        JFrame jf = new JFrame("ignore component height test");
        JPanel p = new JPanel();

        p.setLayout(new MigLayout("","[] [] [] []", ""));
        p.add(new JButton("one"), "");
        p.add(new JButton("two"), "");
        p.add(new JButton("three"), "");
        JButton big = new JButton("big");
        big.setPreferredSize(new Dimension(40,80));
        p.add(big, "wrap");

        JPanel tripanel = new JPanel();
        tripanel.setLayout(new MigLayout("","[] [] [] []", ""));
        int k = 0;
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j <= i; ++j)
            {
                tripanel.add(new JButton("tri"+k), j == i ? "wrap" : "");
                ++k;
            }
        }

        p.add(tripanel, "span,wrap");

        jf.setContentPane(p);
        jf.pack();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);        
    }
}

Is there any way to tell MigLayout to ignore a component's height?

Here's a test example:

enter image description here

I have an oversized component (the "big" button in the upper right) in the first row, and a JPanel which has a more-or-less triangular shape that spans the whole 2nd row.

I would like to have MigLayout ignore the height of the "big" button when choosing a size for the first row, because I know it's ok for it to overlap with my 2nd-row component.

How can I do this?

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

public class IgnoreComponentHeight {
    public static void main(String[] args) {
        JFrame jf = new JFrame("ignore component height test");
        JPanel p = new JPanel();

        p.setLayout(new MigLayout("","[] [] [] []", ""));
        p.add(new JButton("one"), "");
        p.add(new JButton("two"), "");
        p.add(new JButton("three"), "");
        JButton big = new JButton("big");
        big.setPreferredSize(new Dimension(40,80));
        p.add(big, "wrap");

        JPanel tripanel = new JPanel();
        tripanel.setLayout(new MigLayout("","[] [] [] []", ""));
        int k = 0;
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j <= i; ++j)
            {
                tripanel.add(new JButton("tri"+k), j == i ? "wrap" : "");
                ++k;
            }
        }

        p.add(tripanel, "span,wrap");

        jf.setContentPane(p);
        jf.pack();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-10-24 10:38:29

也许您正在寻找的是“spany 2”或绝对定位,例如“pos Visual.x2-pref-5 5”。

查看 MigLayout 网页并加载 swing 演示。然后进入绝对定位部分。您可以右键单击组件并尝试其中的约束,这将帮助您找到真正想要的东西。

p.add(new JButton("three"), "wrap");
JButton big = new JButton("big");
big.setPreferredSize(new Dimension(40, 80));
p.add(big, "pos visual.x2-pref-5 5");

Maybe what you're looking for is "spany 2" or absolute positioning like "pos visual.x2-pref-5 5".

Check out the MigLayout web page and load up the swing demo. Then go to the absolute positioning section. You can right click on the components and experiment with the constraints there, which will help you dial into what you really want.

p.add(new JButton("three"), "wrap");
JButton big = new JButton("big");
big.setPreferredSize(new Dimension(40, 80));
p.add(big, "pos visual.x2-pref-5 5");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文