更新
突然意识到我可能已经把这个问题从字面上理解了
也许你的意思更像...
String text = // Text from the file...
int letterCount = text.length();
int wordCount = text.split("\\s").length;
String result = "[insert file name here] has " + letterCount + " characters and " + wordCount + " words";
// A JTextArea you created eailer
textArea.setText(result);
原答案
但我想要的是将 textArea 中的文本替换为“fileName 有 x 个单词”
我不太确定这正是你的意思,但是...
最好的起点(对于这个要求)是DocumentFilter
它将允许您过滤应用于Document/JTextArea 的文本并将其替换为您想要的任何文本。
也许是这样的......
public class WordCounterDocumentFilter extends DocumentFilter {
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
int letterCount = text.length();
int wordCount = text.split("\\s").length;
String result = "[insert file name here] has " + letterCount + " characters and " + wordCount + " words";
super.replace(fb, 0, fb.getDocument().getLength(), result, attrs);
}
}
然后可以使用这样的东西......
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextArea ta = new JTextArea(10, 20);
add(new JScrollPane(ta));
ta.setEditable(false);
((AbstractDocument)ta.getDocument()).setDocumentFilter(new WordCounterDocumentFilter());
ta.setText("This is a bunch of text");
}
}
public class WordCounterDocumentFilter extends DocumentFilter {
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
int letterCount = text.length();
int wordCount = text.split("\\s").length;
String result = "[insert file name here] has " + letterCount + " characters and " + wordCount + " words";
super.replace(fb, 0, fb.getDocument().getLength(), result, attrs);
}
}
}
会输出这样的东西...