获取具有固定宽度的多行文本的高度,以使对话框正确调整大小 [英] Get height of multi line text with fixed width to make dialog resize properly

查看:107
本文介绍了获取具有固定宽度的多行文本的高度,以使对话框正确调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个对话框,其中包含某种文本元素(JLabel/JTextArea等),该文本元素为多行并包装单词.我希望对话框具有固定的宽度,但根据文本的大小来调整高度.我有以下代码:

I want to create a dialog that contains some kind of text element (JLabel/JTextArea etc) that is multi lined and wrap the words. I want the dialog to be of a fixed width but adapt the height depending on how big the text is. I have this code:

import static javax.swing.GroupLayout.DEFAULT_SIZE;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TextSizeProblem extends JFrame {
  public TextSizeProblem() {

    String dummyString = "";
    for (int i = 0; i < 100; i++) {
      dummyString += " word" + i;  //Create a long text
    }
    JLabel text = new JLabel();
    text.setText("<html>" + dummyString + "</html>");

    JButton packMeButton = new JButton("pack");
    packMeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pack();
      }
    });

    GroupLayout layout = new GroupLayout(this.getContentPane());
    getContentPane().setLayout(layout);
    layout.setVerticalGroup(layout.createParallelGroup()
        .addComponent(packMeButton)
        .addComponent(text)
    );
    layout.setHorizontalGroup(layout.createSequentialGroup()
        .addComponent(packMeButton)
        .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400
    );

    pack();
  }

  public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new TextSizeProblem();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}

运行程序时,它看起来像这样:
(来源: lesc.se )

When running the program it looks like this:
(source: lesc.se)

但是我希望对话框看起来像这样(当您按下pack按钮时):
(来源: lesc.se )

But I would like the dialog to look like this (as when you press the pack-button):
(source: lesc.se)

我猜测问题是布局管理器在将文本显示到屏幕之前无法确定文本的正确高度.我已经尝试过各种validate(),invalidate(),validateTree()等,但没有成功.

I'm guessing that the problem is that the layout manager had not been able to determine the proper height of the text before displaying it to the screen. I have tried various validate(), invalidate(), validateTree() etc but have not succeed.

推荐答案

我找到了解决问题的方法.通过用JTextArea替换JLabel:

I found a solution to my problem. By replacing the JLabel with a JTextArea:

JTextArea text = new JTextArea();
text.setText(dummyString);
text.setLineWrap(true);
text.setWrapStyleWord(true);

并调用pack(),然后调用布局管理器以再次布局组件,再调用另一个pack:

And calling pack() followed by an invocation to the layout manager to layout the components again followed by another pack:

pack();
layout.invalidateLayout(this.getContentPane());
pack();

这将使布局管理器适应宽度.

This will cause the layout manager to adapt to the width.

完整代码:

import static javax.swing.GroupLayout.DEFAULT_SIZE;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TextSizeProblem3 extends JFrame {
  public TextSizeProblem3() {

    String dummyString = "";
    for (int i = 0; i < 100; i++) {
      dummyString += " word" + i;  //Create a long text
    }
    JTextArea text = new JTextArea();
    text.setText(dummyString);
    text.setLineWrap(true);
    text.setWrapStyleWord(true);

    JButton packMeButton = new JButton("pack");
    packMeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pack();
      }
    });

    GroupLayout layout = new GroupLayout(this.getContentPane());
    getContentPane().setLayout(layout);
    layout.setVerticalGroup(layout.createParallelGroup()
        .addComponent(packMeButton)
        .addComponent(text)
    );
    layout.setHorizontalGroup(layout.createSequentialGroup()
        .addComponent(packMeButton)
        .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400
    );

    pack();
    layout.invalidateLayout(this.getContentPane());
    pack();
  }

  public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new TextSizeProblem3();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}

(您可以添加一些自定义项(边框,颜色等),使其看起来像JLabel,但我省略了它)

(you can add some customization (border, color etc) so it looks just like the JLabel but I have omitted that)

这篇关于获取具有固定宽度的多行文本的高度,以使对话框正确调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆