意思是当在读文件太大时,能跳出一个对话框来显示读的进度,能给个大概的例子吗?我是JDK1.5的.

解决方案 »

  1.   

    //给你一个玩玩,这个是读一大的文本文件,并可以显示给你看,同时有进程条出来。import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;public class DidoleoPM implements ActionListener,Runnable{ JFrame f=null;
    JLabel label=null;
    JTextArea textarea=null;
    JFileChooser fileChooser=null;

    public DidoleoPM(){
    f=new JFrame("");
    Container contentPane=f.getContentPane();
    textarea=new JTextArea();
    JScrollPane scrollPane=new JScrollPane(textarea);
    scrollPane.setPreferredSize(new Dimension(350,350));
    JButton b=new JButton("读取文件");
    b.addActionListener(this);
    label=new JLabel("",JLabel.CENTER);

    fileChooser=new JFileChooser();
    contentPane.add(label,BorderLayout.NORTH);
    contentPane.add(scrollPane,BorderLayout.CENTER);
    contentPane.add(b,BorderLayout.SOUTH);

    f.pack();
    f.setVisible(true);

    f.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    }

    );

    }


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

    Thread athread;

    public void actionPerformed(ActionEvent e){
    athread=new Thread(this);
    athread.start();
    }

    public void run(){

    File file=null;
    int result=fileChooser.showOpenDialog(f);

    textarea.setText("");


    if(result==JFileChooser.APPROVE_OPTION){
    file=fileChooser.getSelectedFile();
    label.setText("你选择的文件名称为"+file.getName());
    }else if(result==JFileChooser.CANCEL_OPTION){
    label.setText("你还没有选择任何文件");
    }

    FileInputStream inputStream=null;

    if(file!=null){
    try{
    inputStream=new FileInputStream(file);
    }catch(FileNotFoundException fe){
    label.setText("file not found");
    return;
    }
    }

    ProgressMonitorInputStream pmInputStream=new
    ProgressMonitorInputStream(f,"get file...",inputStream);
    ProgressMonitor pMonitor=
    pmInputStream.getProgressMonitor();
    pMonitor.setMillisToDecideToPopup(10);
    pMonitor.setMillisToPopup(0);
    int readbyte;

    try{
    while((readbyte=pmInputStream.read())!=-1){
    textarea.append(String.valueOf((char) readbyte));
    try{
    Thread.sleep(10);
    }catch(InterruptedException ie){

    }

    if(pMonitor.isCanceled()){
    textarea.append("\n\n读取文件中断");
    }
    }

    }catch(IOException ioe){
    label.setText("读取文件错误");
    }finally{
    try{
    if(pmInputStream!=null){
    pmInputStream.close();
    }
    }catch(IOException ioe2){

    }
    }

    }

    }
      

  2.   

    多谢楼上的,这个我试过了,这个必须是大的文件才会显示进度条,我需要的是无论大小
    文件都能显示的方法.能用线程度产生一个JDialog,在这个上面添加进度条,再由读文件
    的线程必变进度条值,但我使用后总是可以弹出窗口,但我加在上面的进度条和按钮,总是在读
    文件完成后才出现,在读的时候只有对话框,而上面什么都看不到.有什么方法能解决吗?
      

  3.   

    楼主,根据你说的现象,问题的原因是:修改进度条进度的操作必须是单独一个线程(独立于jdialog!)也就是说你需要建立一个线程(不是JDialog)去读文件修改进度条
      

  4.   

    楼主大哥,你没看我的代码吧,
    pMonitor.setMillisToDecideToPopup(10);
    改成pMonitor.setMillisToDecideToPopup(0);
    那么再小的文件也会有进成条出来的.
      

  5.   

    http://www.jroller.com/page/santhosh?entry=show_progress_in_modal_dialog
      

  6.   

    didoleo(冷月无声) ,不好意思,真是没看清楚,我试下,谢了!