简体   繁体   English

使用JGoodies FormLayout时如何使大型组件不破坏布局?

[英]How to make large components not to blow up the layout when using JGoodies FormLayout?

I'm using JGoodies Forms 1.8.0 我正在使用JGoodies Forms 1.8.0

I've been having a problem when a single oversized label causes the entire layout to no longer fit in a window. 当单个超大标签导致整个布局不再适合窗口时,我一直遇到问题。 I would like that text to be visually trimmed, so it's obvious for the user that the text doesn't fit, but the rest of the GUI should still keep fitting. 我希望对文本进行视觉修整,因此对于用户来说很明显文本不合适,但是GUI的其余部分仍应保持合适。

I prepared a simplified example that exhibits the same behaviour. 我准备了一个表现出相同行为的简化示例。 Here everything works fine because the window is large enough: 这里的一切都很好,因为窗口足够大:

And here the same window, but resized: 而这里是相同的窗口,但是调整了大小:

在此处输入图片说明

Notice that the rightmost columns are no longer visible. 请注意,最右边的列不再可见。

The desired effect is as following: 预期效果如下:

  • if the text fits, it should be displayed in its entirety 如果文本合适,则应完整显示

  • if the text doesn't fit, then the end of it should be cut off 如果文本不合适,则应将其结尾剪掉

  • text should be left-aligned 文字应左对齐

  • all buttons should be visible, all the time 所有按钮应始终可见

  • button 100 should be in the very corner of the window 按钮100应该在窗口的一角

Here is the code for the screenshots: 这是屏幕截图的代码:

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

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

public class Test extends JFrame {

    Test() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(900, 600);

        JPanel left = new JPanel();
        left.setBackground(Color.BLUE);

        JPanel right = new JPanel();

        JLabel fox = new JLabel("The quick brown fox jumps over the lazy dog.");
        fox.setFont(new Font(null, 0, 50));

        JPanel rightBottom = new JPanel();
        rightBottom.setLayout(new GridLayout(10, 10));
        for (int i = 1; i <= 100; i++) {
            rightBottom.add(new JButton("butt" + i));
        }

        CellConstraints cc = new CellConstraints();
        this.setLayout(new FormLayout("100dlu,p:g", "f:p:g"));
        this.add(left, cc.xy(1, 1));
        this.add(right, cc.xy(2, 1));
        right.setLayout(new FormLayout("f:p:g", "p,5dlu,f:p:g"));
        right.add(fox, cc.xy(1, 1));
        right.add(rightBottom, cc.xy(1, 3));

    }

    public static void main(String[] args) {
        new Test().setVisible(true);
    }
}

BorderLayout ... didn't help BorderLayout ...没有帮助

Works fine for me: 对我来说效果很好:

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

public class Test8 extends JFrame {

    Test8() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(900, 600);

        JPanel left = new JPanel();
        left.setBackground(Color.BLUE);

        JPanel right = new JPanel(new BorderLayout());

        JLabel fox = new JLabel("The quick brown fox jumps over the lazy dog.");
        fox.setFont(new Font(null, 0, 50));

        JPanel rightBottom = new JPanel();
        rightBottom.setLayout(new GridLayout(10, 10));
        for (int i = 1; i <= 100; i++) {
            rightBottom.add(new JButton("butt" + i));
        }

        right.add(fox, BorderLayout.NORTH);
        right.add(rightBottom, BorderLayout.CENTER);
        add(right);

    }

    public static void main(String[] args) {
        new Test8().setVisible(true);
    }
}

If you don't like the dots showing in the label, then try a non-editable text field. 如果您不喜欢标签上显示的点,请尝试使用不可编辑的文本字段。

When something doesn't work, then post the code you tried. 如果无法解决问题,请发布您尝试的代码。 A verbal explanation doesn't help. 口头解释无济于事。

The specification of preferred size makes the layout to display buttons at their preferred size. 首选大小的规范使布局以首选大小显示按钮。 As a result some of the buttons do not fit. 结果,某些按钮不适合。 Try specifying a different constraint. 尝试指定其他约束。 For example, use a constant size with a combination of grow and fill. 例如,将恒定大小与增长和填充结合使用。 For example try these: 例如,尝试以下操作:

this.setLayout(new FormLayout("100dlu,1dlu:g", "f:1dlu:g"));

right.setLayout(new FormLayout("f:1dlu:g", "p,5dlu,f:1dlu:g"));

At certain sizes button titles will not fit though. 在某些尺寸下,按钮标题不适合。

Here is a result: 结果如下:

在此处输入图片说明

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

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