JTextArea中的行 [英] Rows in JTextArea
问题描述
有没有一种可靠的方法来计算字符串在JTextArea中如何划分成行?
Is there a reliable way to calculate how is a string divided into rows in JTextArea?
我有一个固定宽度的JTextArea,当它被填充时,一个新的行被添加并且它会垂直扩展。
I have a JTextArea with fixed width and when it gets filled, a new row is added and it expands vertically.
现在,我需要确切地知道哪一行是哪一行。我可以使用字体指标添加单个字符宽度,但我不知道这是否可靠,或者是否有更好的方法。
Now, I need to know exactly which characters are in which row. I could add up single character widths using font metrics, but I don't know if that is reliable, or if there may be a better method.
是否为字体指标技巧唯一的方法?
Is the font metrics "trick" the only way?
推荐答案
所有 JTextComponent 有 modelToView(...)
和 viewToModel(...)
可以提供帮助的方法,但可能更好的方法是 javax.swing.text.Utilities 类,包括 getRowStart(...)
和 getRowEnd(...)
All JTextComponents have modelToView(...)
and viewToModel(...)
methods that can help, but perhaps even better are the methods in the javax.swing.text.Utilities class including getRowStart(...)
and getRowEnd(...)
这篇关于JTextArea中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!