进度条不变

发布于 2024-11-16 11:25:31 字数 1374 浏览 3 评论 0原文

我知道所有其他类似的问题,但我似乎无法解决问题。我尝试过“bar.repaint();”和“bar.update(bar.getGraphics());”但它们似乎都不起作用。

如果有人有时间快速浏览一下,我将非常感激!这真的很困扰我,我已经尝试解决它好几个小时了。

它基本上是一个关闭定时器。您输入小时和秒,它就会倒计时,直到关闭计算机。它还有一个进度条,这就是问题所在。它似乎不想在每一秒后重新绘制自己。

代码很多,所以我决定上传所有文件(2 个文件)。

speedyshare.com/files/29072975/files.zip

提前致谢!

编辑:

不起作用的代码片段:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    try{
        if(varHours.getText() != null && varMins.getText() != null){
            Thread countDownThread = new Thread(new Countdown(Integer.parseInt(varHours.getText()), Integer.parseInt(varMins.getText())));
            int totalSecs = (Integer.parseInt(varHours.getText())*60*60) + (Integer.parseInt(varMins.getText())*60);
            shutdownProgress = new javax.swing.JProgressBar(0, totalSecs);
            countDownThread.start();
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Please supply both fields!", "One or more fields were not supplied", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }catch(Exception ex){
        javax.swing.JOptionPane.showMessageDialog(null, "Error!\nCould not launch method countDown!", "Error!", javax.swing.JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButton1ActionPerformed

I'm aware of all the other similar questions but i cannot seem to fix the problem. I've tried "bar.repaint();" and "bar.update(bar.getGraphics());" but none of them seems to work.

If anyone has time to take a quick look at it i'd really appreciate it! It's really bugging me, i've been trying to solve it for hours now.

It's basically a shutdown timer. You input hours and seconds and it counts down until it shuts down the computer. It also has a progressbar, which is the problem. It doesn't seem to want to repaint itself after each second.

It's a lot of code, so i decided to just upload all of the files (2 files).

speedyshare.com/files/29072975/files.zip

Thanks in advance!

EDIT:

Snippet of the code that isn't working:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    try{
        if(varHours.getText() != null && varMins.getText() != null){
            Thread countDownThread = new Thread(new Countdown(Integer.parseInt(varHours.getText()), Integer.parseInt(varMins.getText())));
            int totalSecs = (Integer.parseInt(varHours.getText())*60*60) + (Integer.parseInt(varMins.getText())*60);
            shutdownProgress = new javax.swing.JProgressBar(0, totalSecs);
            countDownThread.start();
        }else{
            javax.swing.JOptionPane.showMessageDialog(null, "Please supply both fields!", "One or more fields were not supplied", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }catch(Exception ex){
        javax.swing.JOptionPane.showMessageDialog(null, "Error!\nCould not launch method countDown!", "Error!", javax.swing.JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButton1ActionPerformed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-11-23 11:25:31

jButton1ActionPerformed中,您设置shutdownProgress。然而,这是一个新的 JProgressBar,而不是在 initComponents() 中创建的。因此,您稍后对 shutdownProgress 所做的任何更改都会对未显示的 JProgressBar 进行。

其次,从 EDT 以外的线程对 Swing 组件进行更改是一个主要禁忌。
使用 SwingUtilities 。调用稍后

In jButton1ActionPerformed you set shutdownProgress. However, this is a new JProgressBar, not the one created in initComponents(). So, any changes you later make to shutdownProgress are made to a JProgressBar that is not displayed.

Secondly, making changes to a Swing component from a thread other than the EDT is a major no-no.
Use SwingUtilitities .invokeLater.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文