Miglayout 按钮溢出约束

发布于 2024-09-07 03:41:33 字数 1635 浏览 12 评论 0原文

我正在使用 Miglayout 为我的一个面板创建类似表格的布局。我需要所有面板的固定宽度为 200 像素。当我在面板中添加组件时,一切正常,但是当我尝试插入具有长文本的按钮(因此需要超过 200 px 的空间来显示)时,按钮会溢出其单元格并与相邻按钮重叠。这段代码应该说明我的问题:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;


/**
 * @author Savvas Dalkitsis
 */
public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JPanel content = new JPanel(new MigLayout("wrap 5","[200!]","[50!]"));
        JButton b = new JButton("Button 1");
        content.add(b,"growx");
        b = new JButton("Button 2");
        content.add(b,"growx");
        b = new JButton("Button with a very long text which should not be visible");
        content.add(b,"growx");
        b = new JButton("Button 4");
        content.add(b,"growx");
        b = new JButton("Button 5");
        content.add(b,"growx");
        b = new JButton("Button 6");
        content.add(b,"growx");
        b = new JButton("Button 7");
        content.add(b,"growx");
        b = new JButton("Button 8");
        content.add(b,"growx");
        b = new JButton("Button 9");
        content.add(b,"growx");
        b = new JButton("Button 10");
        content.add(b,"growx");
        f.setContentPane(content);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

我想要的是按钮来显示它可以容纳在 200 像素中的所有文本,然后可能是一些尾随句点,例如“带有版本的按钮...”

有没有人知道如何达到这个目的?

(您可以从这里获取miglayout进行测试)

I am using a Miglayout to create a table-like layout for one of my panels. I need all my panels to have a fixed width of 200 pixels. When i add components in the panel everything works OK but when i try to insert a button that has a long text (and therefore needs more space than 200 px to display) the button overflows its cell and overlaps neighboring buttons. This code should demonstrate my problem:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;


/**
 * @author Savvas Dalkitsis
 */
public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JPanel content = new JPanel(new MigLayout("wrap 5","[200!]","[50!]"));
        JButton b = new JButton("Button 1");
        content.add(b,"growx");
        b = new JButton("Button 2");
        content.add(b,"growx");
        b = new JButton("Button with a very long text which should not be visible");
        content.add(b,"growx");
        b = new JButton("Button 4");
        content.add(b,"growx");
        b = new JButton("Button 5");
        content.add(b,"growx");
        b = new JButton("Button 6");
        content.add(b,"growx");
        b = new JButton("Button 7");
        content.add(b,"growx");
        b = new JButton("Button 8");
        content.add(b,"growx");
        b = new JButton("Button 9");
        content.add(b,"growx");
        b = new JButton("Button 10");
        content.add(b,"growx");
        f.setContentPane(content);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

What i would like is the button to display all the text it can fit in the 200 pixels and then maybe some trailing periods like "Button with ver..."

Does any one have an idea on how to achieve this?

(you can get miglayout from here for testing)

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

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

发布评论

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

评论(1

不如归去 2024-09-14 03:41:33

刚刚下载了布局来查看。您的解决方案是否只是:

    b = new JButton("Button with a very long text which should not be visible");
    content.add(b,"growx, wmax 200");

它对我有用。

Just downloaded the layout to check it out. Is your solution just:

    b = new JButton("Button with a very long text which should not be visible");
    content.add(b,"growx, wmax 200");

It worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文