主题:关于JTextPane问题
小弟刚学swing ,在用JTextPane 时出现了问题
是把一个文件显示在JTextPane中,可是文件大了,就不能运行
具体代码如下:
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
public class ReadDocContent implements Runnable
{
JFrame mainframe;
JTextPane textpane=null;
JInternalFrame internalframe=null;
JFileChooser filechooser=null;
Thread athread;
SimpleAttributeSet attrset=new SimpleAttributeSet();
public ReadDocContent(JTextPane pane,JFrame mf,JInternalFrame current)
{
this.mainframe=mf;
this.textpane=pane;
this.internalframe=current;
filechooser=new JFileChooser();
athread=new Thread(this);
athread.start();
}
public void run()
{
File file=null;
int result=filechooser.showOpenDialog(mainframe);
textpane.setText("");
if(result==JFileChooser.APPROVE_OPTION)
{
file=filechooser.getSelectedFile();
internalframe.setTitle(file.getName());
}
else
{
internalframe.setTitle("you choose nothing");
}
BufferedReader in=null;
if(file!=null)
{
try
{
in=new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException e)
{
System.out.println("error");
internalframe.setTitle("File Not Found");
return;
}
Document doc=textpane.getDocument();
String str;
try
{
while((str=in.readLine())!=null)
{
System.out.println(doc.getLength());
doc.insertString(doc.getLength(),str+"\n",attrset);
}
}
catch(IOException e)
{
System.out.println("error");
internalframe.setTitle("io error");
}
catch(BadLocationException ble)
{
System.out.println("error");
System.out.println(ble.getMessage());
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
System.out.println("error");
}
}
}
}
}
是把一个文件显示在JTextPane中,可是文件大了,就不能运行
具体代码如下:
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
public class ReadDocContent implements Runnable
{
JFrame mainframe;
JTextPane textpane=null;
JInternalFrame internalframe=null;
JFileChooser filechooser=null;
Thread athread;
SimpleAttributeSet attrset=new SimpleAttributeSet();
public ReadDocContent(JTextPane pane,JFrame mf,JInternalFrame current)
{
this.mainframe=mf;
this.textpane=pane;
this.internalframe=current;
filechooser=new JFileChooser();
athread=new Thread(this);
athread.start();
}
public void run()
{
File file=null;
int result=filechooser.showOpenDialog(mainframe);
textpane.setText("");
if(result==JFileChooser.APPROVE_OPTION)
{
file=filechooser.getSelectedFile();
internalframe.setTitle(file.getName());
}
else
{
internalframe.setTitle("you choose nothing");
}
BufferedReader in=null;
if(file!=null)
{
try
{
in=new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException e)
{
System.out.println("error");
internalframe.setTitle("File Not Found");
return;
}
Document doc=textpane.getDocument();
String str;
try
{
while((str=in.readLine())!=null)
{
System.out.println(doc.getLength());
doc.insertString(doc.getLength(),str+"\n",attrset);
}
}
catch(IOException e)
{
System.out.println("error");
internalframe.setTitle("io error");
}
catch(BadLocationException ble)
{
System.out.println("error");
System.out.println(ble.getMessage());
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
System.out.println("error");
}
}
}
}
}