2016-05-16 23 views
0

我的程序用各種消息填充JTextPane,有些擴展JTextPane爲2行以適應文本,並且只有一行。我希望窗格始終是兩行。有沒有辦法返回JTextPane當前佔用的文本行數?檢測JTextPane中的行數

+0

的[javax.swing.text.Utilities](HTTP:/ /docs.oracle.com/javase/8/docs/api/javax/swing/text/Utilities.html)課程可能會幫助您解決問題。 –

+0

此問題最初是作爲以下副本而封閉的:http://stackoverflow.com/questions/14002238/trying-to-get-the-start-and-end-point-of-each-line-within-a-text -pane。不知道這個帖子如何回答這個問題,因爲可能有一行文本包裝到第二行。 – camickr

回答

4

有沒有辦法返回JTextPane當前佔用的文本行數?

結賬Text Utilities,這與評論中提供的鏈接不同。

您應該可以使用getWrappedLines(...)方法。

0

這將使在的JTextPane線的數目:

第一選項:

int lineCount = textPane.getText().split("\n").length + 1; 

第二個選項:

Document doc = textPane.getDocument(); 
int lineCount = new JTextArea(doc).getLineCount();