繁体   English   中英

如何设置我的JProgressbar以显示程序加载的进度

[英]How can I set up my JProgressbar to show the progress of my program loading

当我的程序启动时,我的JProgressBar会按照我想要的方式运行,并显示我的程序加载进度,但是我具有数据库的重新启动功能,因此必须使用dispose()重置所有组件,从而重新加载所有组件。

重新启动过程与启动过程完全相同,但是在这种情况下,JProgressbar根本不加载(只是一个空白窗口),整个类都不想工作。

通过以下代码运行:If语句测试数据库文件是否存在,从那里加载进度条类并设置i的max参数和值。 在数据库和GUI加载时,它会增加进度栏的值,并且在GUI完成加载时,它使用dispose()关闭JProgressBar使用的窗口。 继续进行下去,如果不存在,它将使用新的JProgressBar重新启动它。

我所知道的是,当它第一次加载进度条时,它将起作用。 但是,当我第二次需要它时,它会失败。 即使我重新设置了值,它仍然不想工作。

GUIBackEnd()
    {
        if(ec.hasDatabase())
        {
            pbc = new ProgressBarController(23, "Initializing E.M.M.A.", true);
            pbc.setProgressText("Start-up");

            update();

            pbc.increaseBar();
            pbc.setProgressText("Loading Equipment");

            equipmentData = ec.viewEquipment();
            pbc.increaseBar();
            pbc.setProgressText("Loading Customers");

            customerData = cc.viewCustomer();
            pbc.increaseBar();
            pbc.setProgressText("Loading Services");

            serviceData = mc.viewService();
            pbc.increaseBar();
            pbc.setProgressText("Loading GUI");

            frameGen();

            pbc.dispose();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Main database was not found. \nE.M.M.A. will attempt to load the backup...", "WARNING", 
                    JOptionPane.WARNING_MESSAGE);

            String feed = ec.loadMainBackup();

            JOptionPane.showMessageDialog(null, feed, "Loading", 
                    JOptionPane.INFORMATION_MESSAGE);

            if(!EquipmentController.hasLoaded)
            {                
                JOptionPane.showMessageDialog(null, EquipmentController.mainLoaded, "RESTART", JOptionPane.PLAIN_MESSAGE);

                restartGUI(true);
            }
            else
            {
                pbc = new ProgressBarController(23, "Initializing E.M.M.A.", true);
                pbc.setProgressText("Start-up");

                update();

                pbc.increaseBar();
                pbc.setProgressText("Loading Equipment");

                equipmentData = ec.viewEquipment();
                pbc.increaseBar();
                pbc.setProgressText("Loading Customers");

                customerData = cc.viewCustomer();
                pbc.increaseBar();
                pbc.setProgressText("Loading Services");

                serviceData = mc.viewService();
                pbc.increaseBar();
                pbc.setProgressText("Loading GUI");

                frameGen();

                pbc.dispose();
            }

        }
    }

我需要的是,我应该能够在程序执行过程中随时调用JProgressBar,并让其向用户显示程序在后台工作时正在取得的进展...原因是,随着数据库的增长或何时导入已完成,这需要时间,并且程序必须显示它正在执行任务,而不仅仅是闲着。

可能的原因:

第一次,上面的代码是从main方法调用的,因此不会从Swing事件线程调用,因此不会阻塞该线程,从而允许它执行其工作,包括绘制JProgressBar和GUI的其余部分。

第二次调用该代码时,很可能是通过Swing事件调用的,而可能不是从SwingWorker或其他后台线程中专门调用的,因此这次是在GUI事件线程上调用的,因此确实会阻塞该线程,表示进度条不会以图形方式更新

解决方案:了解并使用SwingWorker进行长时间的后台工作,更新worker的progress属性(允许从0到100),将PropertyChangeListener附加到同一worker列表以进行进度更改,并更新您的此属性内的JProgressBar更改侦听器。

请阅读Swing中的并发性,以更好地了解Swing事件线程的工作原理。

暂无
暂无

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

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