0
我寫了一個方法,在JTextPane中寫入hello world!\ n 10000次。當我使用hello world時,我認識到 明顯的性能下降!沒有換行。當文本包含換行符時,JTextPane中的顯着性能差異
例子:
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.text.*;
public class JTextPaneTest {
JTextPane textPane = new JTextPane();
Document doc = new DefaultStyledDocument();
//constructor
JTextPaneTest() {
for(int i=0;i<10000;i++) {
try {
doc.insertString(doc.getLength(), i+" hello world!", null);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
textPane.setDocument(doc);
createWindow();
}
public void createWindow() {
JFrame frame = new JFrame();
frame = new JFrame("frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(new JScrollPane(textPane), BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args) {
System.out.println("start...");
float startTime = System.nanoTime();
new JTextPaneTest();
float stopTime = System.nanoTime() - startTime;
System.out.println("elapsed time main: "+stopTime/1000000000+ "s");
}
}
,這可能是該現象的原因是什麼?有任何想法嗎?
http://java-sl.com/JEditorPanePerformance.html最後一節提出了一種修復方法 – StanislavL 2014-12-05 13:11:50
@StanislavL:謝謝! – Ramses 2014-12-09 08:13:27