JTextPane出现排列格式错误(附源码)
下面的代码可直接编译执行;
程序大概的意思就是,一个线程向多个Tab页中的文本框插入文本内容,
奇怪的现象是,总有几个Tab页中的文本框显示的文本内容格式排列有问题,
明明是一行一条内容,但是显示的却是某些部分一行几条内容 :(
而且格式混乱的得还有点规则,:P
只是那些地方的文本不能选择、编辑。
本人在1.3和1.4中都试过,一样的现象,另外系统是XP。
各位大侠帮帮忙,分析分析呵。。。。。。
/*
* Created on 2005-11-21
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.freedom.test;
/**
* @author Freedom.Fanglh
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class FRTextTest extends JFrame {
JTabbedPane tabPane = new JTabbedPane();
public static void main(String[] args)
{
FRTextTest f = new FRTextTest();
f.show();
}
public FRTextTest() {
init();
threadTest();
}
private void init() {
getContentPane().add(tabPane);
initTexts(10);
}
private void initTexts(int num) {
for (int i = 0; i < num; i++) {
tabPane.addTab("TAB " + i,new FRTabPage());
}
}
private void threadTest()
{
new Thread(){
public void run()
{
int nums = tabPane.getComponentCount();
for(int j=0;j<200;j++){
try{
Thread.sleep(100);
}
catch(Exception ex){}
for(int i=0;i<nums;i++)
{
FRTabPage page = (FRTabPage)tabPane.getComponentAt(i);
try{
Document doc = page.getTextPane().getDocument();
String content = "I Love This Game\t" + j + "\n";
doc.insertString(doc.getLength(),content,null);
}
catch(BadLocationException ex)
{
}
int maxSV = page.getScrollPane().getVerticalScrollBar().getMaximum();
page.getScrollPane().getVerticalScrollBar().setValue(maxSV);
}
}
}
}.start();
}
public class FRTabPage extends JPanel {
JTextPane textPane = new JTextPane();
JScrollPane scrPane = new JScrollPane();
public FRTabPage() {
initPage();
}
private void initPage() {
scrPane.getViewport().add(textPane);
JSplitPane splitPane =
new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
true,
scrPane,
new JTextPane());
splitPane.setDividerLocation(300);
splitPane.setDividerSize(5);
setLayout(new BorderLayout());
add(splitPane);
}
public JTextPane getTextPane() {
return textPane;
}
public JScrollPane getScrollPane() {
return scrPane;
}
}
}