gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:36 26 4
gpt4 key购买 nike

是否有可能获取 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 内容的高度。

关于java - 如何获取 JTextPane 中输入文本的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14040586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com