ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何获取JTextPane中输入文本的高度?

2019-09-01 12:00:49  阅读:194  来源: 互联网

标签:jtextpane java swing


有没有办法在JTextPane中获取输入文本的高度?
像素将是伟大的,但任何可以代表它的价值都可以.
例如,我输入了JTextPane:

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 leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s with
the release of Letraset sheets containing Lorem Ipsum passages, and
more recently with desktop publishing software like Aldus PageMaker
including versions of Lorem Ipsum.

有没有办法获得它的长度值(不是它有多少行)?
我需要这个,因为如果我在其中一行中输入图片,总高度会发生变化,我希望能够知道它的确切变化程度.我使用的JTextPane有一个StyledDocument并且没有html.我制作了一个SSCCE但是我使用了两种不同的字体来代替图像,以显示2个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的高度?

解决方法:

使用here中描述的方法测量JEditorPane / JTextPane的固定宽度内容高度.

标签:jtextpane,java,swing
来源: https://codeday.me/bug/20190901/1783092.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有