一个关于JTextPane的setText函数的问题如果字符串参数的长度超出JTextPane的原先文本长度,大约33200-34000时
JTextPane就一片空白,即使原来有内容,也变成了空白
大家碰到过吗
如何应对?

解决方案 »

  1.   

      for(int i=1; i<2000;i++)
      {
    msg+="1A2B3C4D5E6F7G8H9I0J\n";
      }
      
        String txt = textPane.getText(); 
        textPane.setText(txt + msg);
      

  2.   

    超过长度可以 加... 啊 或者你再弄一个 JtextPane 来翻页 显示 
      

  3.   

    我用JtextPane作为LOG的输出
    有新LOG内容时,希望JtextPane中旧内容自动上滚
    不要手动翻页
      

  4.   

    刚睡醒给你写完了。功能是:开始在textPane中输入今天的时间,然后当你按下那个button之后,添加2000行的字符串。你看看行么。
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;public class Test extends JFrame{
    private JPanel panel;
    private JTextPane textPane;
    private JScrollPane scrollPane;
    private JButton button;
    private StringBuffer strBuffer = new StringBuffer();;

    public Test(){
    super("Test Add Text");

    panel = new JPanel();
    textPane = new JTextPane();
    textPane.setText(new Date().toString() +": ");
    textPane.setEditable(false);

    scrollPane = new JScrollPane(textPane);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    button = new JButton("Add Text");
    button.setPreferredSize(new Dimension(50, 25));
    button.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    for(int i=1; i <2000;i++) { 
    strBuffer.append("1A2B3C4D5E6F7G8H9I0J\n"); 

    String text = textPane.getText();
    textPane.setText(text + strBuffer.toString());
    }
    }
    ); panel.setLayout(new BorderLayout());
    panel.add(scrollPane, BorderLayout.CENTER);
    panel.add(button, BorderLayout.SOUTH);

    setLayout(new BorderLayout());
    add(panel, BorderLayout.CENTER);

    setSize(500, 200);
    setVisible(true);
    }

    public static void main(String args[]){ 
        new Test();
    }
    }
      

  5.   

    当你频繁做字符串连接的时候,最好用StringBuffer或者StringBuilder 不要用String