哦,给出的代码有点错误,应该是:public void showText(){
int a=0;
while(a<10000){
a++;
setDocs("asdfasdfsadf",Color.red,false,10);
}
} public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
if(bold==true){
StyleConstants.setBold(attrSet, true);
}
StyleConstants.setFontSize(attrSet, fontSize);
insert(str, attrSet);
} public void insert(String str, AttributeSet attrSet) {
Document doc = show.getDocument();
str = str + "\n" ;
try {
doc.insertString(doc.getLength(), str, attrSet);
show.repaint();
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
}
int a=0;
while(a<10000){
a++;
setDocs("asdfasdfsadf",Color.red,false,10);
}
} public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
if(bold==true){
StyleConstants.setBold(attrSet, true);
}
StyleConstants.setFontSize(attrSet, fontSize);
insert(str, attrSet);
} public void insert(String str, AttributeSet attrSet) {
Document doc = show.getDocument();
str = str + "\n" ;
try {
doc.insertString(doc.getLength(), str, attrSet);
show.repaint();
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
}
解决方案 »
- eclipse如何在控制台发出一些文件操作的命令呢
- 有人做过中国移动B接口,如何使用Java实现(急)
- 怎样设定java.awt.Color的颜色java.awt.Font的字体(急在线等)
- 一个菜鸟容易犯的错误
- 征集几个java的基本概念的简单文字描述,用于面试。
- 该怎么样做一个JAVA程序员
- 运行竟然只有窗口 没看到图形
- 为什么我运行j2se程序,那文本框不能显示?
- 急!如何解决 out of environment memory问题?
- websphere adminserver启动不了?
- 什么地方有原代码可以学习的呀?菜鸟虚心请教。。。
- 高难问题:applet里面如何取得session?因为要对applet的操作进行控制
import java.awt.Color;
import java.awt.Container;import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class PanelTest {
public static void main(String[] args) {
TestFrama frame = new TestFrama();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TestFrame extends JFrame {
public TestFrame() {
setTitle("test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
writer wt = new writer();
Container contentPane = getContentPane();
show = new JTextPane();
contentPane.add(show, BorderLayout.SOUTH); wt.start();
}
public void showText(){
setDocs("asdfasdfsadf",Color.red,false,10);
} public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
if(bold==true){
StyleConstants.setBold(attrSet, true);
}
StyleConstants.setFontSize(attrSet, fontSize);
insert(str, attrSet);
} public void insert(String str, AttributeSet attrSet) {
Document doc = show.getDocument();
str = str + "\n" ;
try {
doc.insertString(doc.getLength(), str, attrSet);
show.repaint();
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
}
private class writer extends Thread{
public void run(){
while(true){
TestFrame.this.showText();
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 300;
private JTextPane show; private JTextPane buttonPanel;}