API有方法吗?
我的办法是取出String然后根据"\n"来截取。
下面是我的代码 // .....
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String shownText = textArea.getText();
int lineCount = textArea.getLineCount();
List<String> shownTxts = new ArrayList<String>();
Pattern p = Pattern.compile("[^(\n)]+\n");
Matcher m = p.matcher(shownText);
while(m.find()) {
shownTxts.add(m.group());
}
System.out.println("lineCount:" + lineCount);
if(!shownText.endsWith("\n")) {
try {
int lastLine = lineCount - 1;
int endLineStartPos = textArea.getLineStartOffset(lastLine);
int lastLineEndPos = textArea.getLineEndOffset(lastLine);
shownTxts.add(shownText.substring(endLineStartPos, lastLineEndPos));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
for(String txt : shownTxts) {
System.out.print(txt);
}
}
});有更好的办法吗谢谢各位
我的办法是取出String然后根据"\n"来截取。
下面是我的代码 // .....
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String shownText = textArea.getText();
int lineCount = textArea.getLineCount();
List<String> shownTxts = new ArrayList<String>();
Pattern p = Pattern.compile("[^(\n)]+\n");
Matcher m = p.matcher(shownText);
while(m.find()) {
shownTxts.add(m.group());
}
System.out.println("lineCount:" + lineCount);
if(!shownText.endsWith("\n")) {
try {
int lastLine = lineCount - 1;
int endLineStartPos = textArea.getLineStartOffset(lastLine);
int lastLineEndPos = textArea.getLineEndOffset(lastLine);
shownTxts.add(shownText.substring(endLineStartPos, lastLineEndPos));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
for(String txt : shownTxts) {
System.out.print(txt);
}
}
});有更好的办法吗谢谢各位
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货