繁体   English   中英

JProgressBar无法正常工作

[英]JProgressBar not working properly

因此,我设置的JProgressBar无法按照我想要的方式工作。 因此,每当我运行该程序时,它就会立即从0变为100。 我尝试使用ProgressMonitor ,Task,并尝试了SwingWorker但没有任何尝试。

这是我的程序:

int max = 10;
for (int i = 0; i <= max; i++) {
        final int progress = (int)Math.round(
            100.0 * ((double)i / (double)max)
        );

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
                }
                jProgressBar2.setValue(progress);
            }
        });
    }

@MadProgrammer这是我尝试做一名摆动工作人员并将每个名称写入文档并更新进度栏的尝试。 该程序将达到86%左右并停止,从不创建完成的文档。 该程序将创建一个空白文档。 这是我首先创建的SwingWorker对象,这是两种方法:

public class GreenWorker extends SwingWorker<Object, Object> {
    @Override
    protected Object doInBackground() throws Exception {
        int max = greenList.size();
        XWPFParagraph tmpParagraph;
        XWPFRun tmpRun;
        FileInputStream file = 
                new FileInputStream(location + "GreenBandList.docx");
        gDocument = new XWPFDocument(OPCPackage.open(file));
        for (int i = 0; i < max; i++) {
            tmpParagraph = gDocument.getParagraphs().get(0);
            tmpRun = tmpParagraph.createRun();
            if (greenList.get(i).length() == 1) {
                    tmpRun.setBold(true);
                    tmpRun.setText(greenList.get(i));
                    tmpRun.setBold(false);
            } else {
                    tmpRun.setText(greenList.get(i));//Write the names to the Word Doc
            }
            int progress = Math.round(((float) i / max) * 100f);
            setProgress(progress);
        }
        return null;
  }        
}

这是启动它并具有我的属性更改事件的按钮的代码。

private void GenerateGreenList() throws IOException, InterruptedException {
    //Need to fix the bug that removes the Letter Header in Yellow Band list
    //********************************************************************\\

    //Delete the old list and make a new one
    File templateFile = new File(location + "\\backup\\GreenTemplate.docx");
    FileUtils.deleteQuietly(new File(location + "GreenBandList.docx"));
    FileUtils.copyFile(templateFile, new File(location + 
            "GreenBandList.docx"));
    //Get the New Entries
    String[] entries = jTextPane3.getText().split("\n");
    for (String s : entries) {
        if (s != null) {
            greenList.add(s);
        }
    }
    //Resort the list
    Collections.sort(greenList);
    //Write the names to the document
    GreenWorker worker = new GreenWorker();
    worker.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equals(evt.getPropertyName())) {
                jProgressBar2.setValue((Integer) evt.getNewValue());
            }
        }
    });
    worker.execute();
    if (worker.isDone()) {
        try {
            gDocument.write(new FileOutputStream(new File(location + "GreenBandList.docx")));
            ////////////////////////////////////////////////////////////
        } catch (IOException ex) {
            Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Green Band List Created!");
        jProgressBar2.setValue(0);
    }
}

我从其他一篇文章中使用了属性更改侦听器,但是我真的不明白您写的内容是什么还是一般情况下的内容?

Swing是一个单线程环境,也就是说,只有一个线程负责处理系统内发生的所有事件,包括重绘事件。 如果任何东西由于某种原因阻塞了该线程,它将阻止Swing处理任何新事件,包括重绘事件...

所以这一切...

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);        }
        jProgressBar2.setValue(progress);
    }
});

一直在暂停事件调度线程,以防止它实际进行任何更新(或至少将它们随机隔开)...

您的外循环也很可能是在EDT上下文中运行的,这意味着直到存在外循环,事件队列中的任何内容都不会被处理。 您所有的重涂请求都将合并为一个重涂请求,瞧,即时填充进度栏...

您确实应该使用SwingWorker我知道您说过您尝试过一个,但是您没有在此方面显示任何代码来进行尝试,因此很难知道为什么它不起作用。

如果我们之前没说过几次,请原谅我:P

您正在EvokeLater内部EvokeLater Thread.sleep ,这意味着它正在for循环之外的其他线程上运行。 即,您的for循环是瞬时完成的(嗯,但是从1循环到100,这几乎是瞬时需要很长时间)。

Thread.sleep EvokeLater之外,它应该可以正常工作。

    int max = 10;
    for (int i = 0; i <= max; i++) {
        final int progress = (int)Math.round(
            100.0 * ((double)i / (double)max)
        );

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                jProgressBar2.setValue(progress);
            }
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

编辑:同意@MadProgrammer。 看来这只是一个说明性问题,但是您应该确保使用SwingWorker进行此处想要完成的任何事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM