【问题标题】:How to get the height of the entered text in a JTextPane?如何在 JTextPane 中获取输入文本的高度?
【发布时间】:2012-12-26 12:28:49
【问题描述】:

有没有办法在JTextPane 中获取输入文本的高度? 像素会很棒,但任何可以代表它的值都可以。 比如我在JTextPane中输入了这个:

Lorem Ipsum 只是打印和排版的虚拟文本 行业。 Lorem Ipsum 一直是业界标准的虚拟文本 自 1500 年代以来,当一位不知名的印刷商采用了一种类型的厨房和 争先恐后地制作了一本类型样本书。它不仅活了下来 五个世纪,也是电子排版的飞跃, 基本保持不变。它在 1960 年代流行于 发布包含 Lorem Ipsum 段落的 Letraset 表,以及 最近使用 Aldus PageMaker 等桌面排版软件 包括 Lorem Ipsum 的版本。

有没有办法获取它的长度(不是多少行)? 我需要这个,因为如果我在其中一行中输入图片,总高度会发生变化,我希望能够知道它到底变化了多少。我使用的JTextPane 有一个StyledDocument 并且没有html。我制作了一个SSCCE,但我使用了两种不同的字体大小来显示两个JTextPanes之间的高度差异,而不是图像。

public class Fonts extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Fonts frame = new Fonts();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Fonts() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.setFont(new Font("Tahoma", Font.PLAIN, 11));
        StyledDocument doc = textPane.getStyledDocument();
        JTextPane textPane_1 = new JTextPane();
        textPane_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textPane_1.setEditable(false);

        StyledDocument doc1 = textPane_1.getStyledDocument();
        String s = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." +
                " Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown" +
                " printer took a galley of type and scrambled it to make a type specimen book. It has survived not" +
                " only five centuries, but also the ";
        try {
            doc.insertString(0, s, null);
            doc1.insertString(0, s, null);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(textPane_1, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
                        .addComponent(textPane_1, Alignment.LEADING)
                        .addComponent(textPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                    .addContainerGap())
        );
        contentPane.setLayout(gl_contentPane);
    }
}

那么有没有办法获得 2 个 JTextPanes 的高度?

【问题讨论】:

  • “JTextPane 有一个 StyledDocument,但没有 html。” JTextPane 确实支持 HTML。
  • 是的,身高和体重也是可能的,为了更好的帮助,请尽快发布SSCCE,简短,可运行,可编译,大约JTextPane,并在您的问题中插入文本,否则请查看swing.TextUtilities

标签: java swing jtextpane


【解决方案1】:

使用here 描述的方法来测量 JEditorPane/JTextPane 的内容高度,以实现固定宽度。

【讨论】:

  • 非常感谢!这帮助我解决了一个我已经工作了 2 周的问题。我非常感激!
猜你喜欢
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多