java - 获取多行流布局的首选大小

标签 java swing resize jgoodies flowlayout

我想不出一种方法来调整 swing GUI 中某些组件的大小。一些自定义标签被添加到 FlowLayout 中,这在调整对话框大小时不正常。 该面板是使用 jgoodies 表单框架构建的。

如果使用这个,FlowLayout 被添加到 xy(3, y)

FormLayout layout = new FormLayout("r:d, 5px, f:d:g", // columns
            "p, p, 5px, p, 5px, p") // rows

FlowLayout 展开并显示滚动条
tags_scroll

如果使用

FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns
            "p, p, 5px, p, 5px, p") // rows

FlowLayout 使用可用空间,第二行的项目消失
tags_vanish

我想将包含 FlowLayout 的每一行的高度扩展到组件的当前高度。不幸的是,首选大小始终对应于单行的高度。
另一种布局会更合适吗?左侧的粗体文本应右对齐,然后是 FlowLayout。

Sources

[edit] 在数周以来一直试图弄清楚如何做到这一点之后,实际问题可以恢复为:
正在将一组标签添加到 JPanel。此 JPanel 应水平使用所有可用空间(对话大小减去标签名称标签的宽度)并根据需要垂直扩展。如果 JPanel 的高度大于对话框,则应该显示垂直滚动条(水平滚动条永远不可见)。 对话可以显示多个 JPanel,它们将一个接一个(垂直)显示。

这是使用 GridBagLayout 和 WrapLayout 的尝试:

public class GridBagLayoutTagPanel extends JPanel {
private static final long serialVersionUID = -441746014057882848L;
private final int NB_TAGS = 5;

public GridBagLayoutTagPanel() {
    setLayout(new GridLayout());

    JPanel pTags = new JPanel(new GridBagLayout());
    pTags.setBackground(Color.ORANGE);
    GridBagConstraints c = new GridBagConstraints();
    c.ipadx = 5;
    c.ipady = 5;

    int rowIndex = 0;
    for (int i = 0; i < NB_TAGS; i++) {
        //add tag name
        JLabel lTagName = new JLabel(String.format("Tag %s:", i));
        lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = rowIndex++;
        pTags.add(lTagName, c);

        //add tag values
        JPanel pTag = new JPanel(new BorderLayout());
        pTag.add(new JLabel("+"), BorderLayout.LINE_START); //label used to add new tags
        pTag.add(getWrapPanel(), BorderLayout.CENTER); //the list of tag values
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        pTags.add(pTag, c);
    }

    //JScrollPane sp = new JScrollPane(pTags);
    //sp.setBorder(BorderFactory.createEmptyBorder());

    add(pTags);
    }

private static JPanel getWrapPanel() {
   JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));

   for (int i = 0; i < 50; i++) {
           p.add(new JLabel("t" + i));
   }
   return p;
}

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new GridBagLayoutTagPanel());
    f.setSize(new Dimension(500, 300));
    f.setVisible(true);
}
}

最佳答案

I'd like to expand the height of each row containing a FlowLayout to the current height of the component. Unfortunately the preferred size always corresponds to the hight for a single row.

Wrap Layout处理这个。

关于java - 获取多行流布局的首选大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8840299/

相关文章:

java - 原子引用的保证

java - 如何将 JComboBox(Object[] items) 添加到 jComboBox1.addItem(String items);

java - 无法让图像显示在 Swing 中

java - 过滤组合框上 ActionPerformed 的源

iphone - 浏览器大小 html css

java - 如何解析具有两种类型值的 JSON,一种是整数,另一种是 JSON 对象?

java - 在JAVA字符串中保存大文件

java - 如何从字符串中解析日期并保持其格式?

java - 自动调整 JButton 图标大小

c++ - 禁用窗口大小调整 Win32