一个关于JTextPane的setText函数的问题

pacosh 2009-09-07 11:35:29
一个关于JTextPane的setText函数的问题

如果字符串参数的长度超出JTextPane的原先文本长度,大约33200-34000时
JTextPane就一片空白,即使原来有内容,也变成了空白
大家碰到过吗
如何应对?
...全文
259 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pacosh 2009-09-07
  • 打赏
  • 举报
回复
我用JtextPane作为LOG的输出
有新LOG内容时,希望JtextPane中旧内容自动上滚
不要手动翻页
java_object 2009-09-07
  • 打赏
  • 举报
回复
超过长度可以 加... 啊

或者你再弄一个 JtextPane 来翻页 显示
pacosh 2009-09-07
  • 打赏
  • 举报
回复 1
for(int i=1; i<2000;i++)
{
msg+="1A2B3C4D5E6F7G8H9I0J\n";
}

String txt = textPane.getText();
textPane.setText(txt + msg);
justinavril 2009-09-07
  • 打赏
  • 举报
回复
贴一下你的代码
justinavril 2009-09-07
  • 打赏
  • 举报
回复
当你频繁做字符串连接的时候,最好用StringBuffer或者StringBuilder 不要用String
justinavril 2009-09-07
  • 打赏
  • 举报
回复
刚睡醒给你写完了。功能是:开始在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();
}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧