进度条不变
我知道所有其他类似的问题,但我似乎无法解决问题。我尝试过“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
jButton1ActionPerformed
中,您设置shutdownProgress
。然而,这是一个新的 JProgressBar,而不是在initComponents()
中创建的。因此,您稍后对 shutdownProgress 所做的任何更改都会对未显示的 JProgressBar 进行。其次,从 EDT 以外的线程对 Swing 组件进行更改是一个主要禁忌。
使用 SwingUtilities 。调用稍后。
In
jButton1ActionPerformed
you setshutdownProgress
. However, this is a new JProgressBar, not the one created ininitComponents()
. So, any changes you later make toshutdownProgress
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.