其他分享
首页 > 其他分享> > 如何获取JTextPane中输入文本的高度?

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

作者:互联网

有没有办法在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