如何在JTextPane上设置固定宽度但动态高度? [英] How to set fixed width but dynamic height on JTextPane?

查看:129
本文介绍了如何在JTextPane上设置固定宽度但动态高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用固定宽度但动态高度的 JTextpane ,这也应该允许换行。高度应随用户添加或删除文本而更改。我会使用 JTextArea ,但文本的样式必须不同。有一个简单的方法来解决这个问题吗?

I want to use a JTextpane with fixed width but dynamic height which should also allow wrapping. The height should change as the user adds or removes text. I would've used a JTextArea but the text has to be styled differently. Is there an easy way to go about this problem?

推荐答案

我已经为类似的东西写了一个答案。查看此处

I already wrote an answer for something similar. Check here.

你必须明白JTextPane返回的preferredSize是:

You have to understand that the preferredSize returned by the JTextPane is:


  • 如果未设置宽度,则getPreferredSize返回Dimension(width,height),其中 width 将是最长行的宽度,如果没有包装,则 height ,显示宽度无限的窗格中所有行所需的高度。

  • if the width is not set, getPreferredSize returns Dimension(width,height) where width would be the width of the longest line if there where no wrapping, and height, the height needed to display all the lines in a pane with infinite width.

如果设置了宽度,getPrefferedSize返回Dimension(width,height),其中 width 将是最长行的宽度,如果没有包装, height 是显示整个文本所需的高度,如果它以当前宽度包装。

if the width is set, getPrefferedSize returns Dimension(width,height) where width would be the width of the longest line if there where no wrapping, and height is the height needed to display the whole text if it was wrapped at the current width.

为什么会出现这种情况?

这很简单。如果您不知道可以使用的宽度,则无法计算表示文本所需的高度。

This is very simple. You can't calculate the height needed to represent a text if you don't know the width you can use.

如何动态地执行此操作?

最好的方法是将DocumentListener添加到JTextPane的Document中。然后,在每次更改时,只需调用

The best would be to use add DocumentListener to your JTextPane's Document. Then, on every change, just call

myJTextPane.setSize(myJTextPane.getWidth(),myJtextPane.getPreferredSize().height);

举例说明

享受此代码的乐趣:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class HelloWorld {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame mainFrame = new JFrame("test");
                mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final JTextPane field = new JTextPane();
                field.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse accumsan magna vel libero bibendum, quis hendrerit nisi rutrum. Cras placerat erat eget dictum ornare. Sed eget nisl quis nibh vehicula mollis. Vestibulum non iaculis erat, quis pulvinar magna. Suspendisse ac rhoncus purus. Quisque finibus, dolor varius tincidunt aliquet, mauris felis condimentum neque, at viverra felis nulla at justo. Duis ut dui velit. Integer vitae mollis leo. Cras quis urna odio. Suspendisse tempus, urna sed maximus fringilla, ante velit finibus massa, id commodo libero quam non ipsum. Sed id augue vitae sapien sagittis imperdiet in eget nibh. Nam semper posuere nisl, dictum efficitur ipsum aliquet ac. Phasellus eros massa, fringilla et neque maximus, pretium tempor magna.");

                mainFrame.getContentPane().setLayout(null);
                mainFrame.getContentPane().add(field,BorderLayout.CENTER);
                field.setLocation(0, 0);
                field.setSize(200,40);
                field.setSize(200, field.getPreferredSize().height);
                mainFrame.setSize(300,500);
                mainFrame.setVisible(true);
                System.out.println(field.getPreferredSize().width+" , "+field.getPreferredSize().height);
            }
        });
    }
}

这篇关于如何在JTextPane上设置固定宽度但动态高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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