【问题标题】:Java file a progress barJava文件进度条
【发布时间】:2011-06-21 18:48:21
【问题描述】:

我是 Java 新手。 我想编写一个程序来生成文件 我想使用进度条来显示压缩进度 有我的代码,但它不起作用 工作完成后仅显示 100%。

        getContentPane().add(progressBar);
    progressBar.setBorder(new CompoundBorder(null, null));
    springLayout.putConstraint(SpringLayout.EAST, progressBar, 0, SpringLayout.EAST, messageLabel);
    springLayout.putConstraint(SpringLayout.WEST, progressBar, 0, SpringLayout.WEST, messageLabel);
    springLayout.putConstraint(SpringLayout.SOUTH, progressBar, 165, SpringLayout.NORTH, getContentPane());
    springLayout.putConstraint(SpringLayout.NORTH, progressBar, 5, SpringLayout.SOUTH, uploadLocalButton);
    progressBar.setValue(0);
    progressBar.setMaximum(100);
    progressBar.setMinimum(0);
    progressBar.setStringPainted(true); 
...
 public void doZip(String filename,String outFilename) 
 {
     try {
        byte[] dataBytes = new byte[1024];
        File file = new File(outFilename);
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        ZipOutputStream zos = new ZipOutputStream(fos);
        File fil = new File(filename); //"Users/you/image.jpg"
        long length= fil.length();
        System.out.println(length);
        zos.putNextEntry(new ZipEntry(fil.getName()));//image.jpg
        FileInputStream in = new FileInputStream(filename);
        int current=0;
        int len;

        while ((len = in.read(dataBytes)) > 0) 
        { current += len; 
        messageLabel.setText(Integer.toString(current));
        System.out.println(current);
        final double progres=(((double)current/(double)length)*100);
        progressBar.setValue((int)progres);             
            zos.write(dataBytes, 0, dataBytes.length);
        }

        zos.closeEntry();
        zos.close();
        in.close();
 }catch (IOException e) {
        JOptionPane.showMessageDialog(null,
                "Access denied. File or patch not exist or in currently in use.","Error",
                JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();

    }}

也许有人知道问题出在哪里?


我按下按钮调用 doZip。 我试过这个

 while ((len = in.read(dataBytes)) > 0) 
        { current += len; 
        zos.write(dataBytes, 0, dataBytes.length);
        final double progres=(((double)current/(double)length)*100);
        try{
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                progressBar.setValue((int)progres);
                }
            });
            java.lang.Thread.sleep(100);
        }catch(InterruptedException e){;}

但它也不起作用,只会使拉链操作的过程更长。

【问题讨论】:

  • 尝试将您对 doZip 的调用包装在一个线程中应该是诀窍

标签: java progress-bar zipfile jprogressbar


【解决方案1】:

我假设您是从 AWT 线程中的某处调用 doZip(例如,作为对单击或某事的响应,由动作处理程序直接调用(in))。因此,在操作(压缩)完成之前,UI 不会更新,即使您在两者之间相应地设置了进度..

一种解决方案是在单独的线程中启动doZip(一般来说,避免在 AWT 线程中进行任何代价高昂的操作是一个很好的建议,但只进行基本的“动作处理”并从那里触发长时间运行的操作。 ..)

【讨论】:

    猜你喜欢
    • 2010-09-20
    • 1970-01-01
    • 2013-02-18
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多