自动刷新JPanel面板
我正在开发一个程序,该程序应该根据我从文件或数组读取的值自动调整栏。我已将两个 JPanel 放入其中,其中一个是静态的,另一个应该从数组/文件中获取其值。我的问题是当您读取数组或文件时我想更新 JPanel 的参数。下面是我的示例代码。
int myvalues[] = {120,130,140,150,160,170,180};
for(int i = 0; i <= myvalues.length-1; i++){
guagePanel.setSize(10,myvalues[i]);
Thread.sleep(1000);
}
但上面的内容并没有让页面刷新。
I am working on a program that should auto adjust a bar depending on the value I read from a file or array. I have placed two JPanel inside one another one is static and the other should get its values from the array/file. My problem is as you read the array or file I want to update the parameters of the JPanel. below is my example code.
int myvalues[] = {120,130,140,150,160,170,180};
for(int i = 0; i <= myvalues.length-1; i++){
guagePanel.setSize(10,myvalues[i]);
Thread.sleep(1000);
}
But the above is not getting the page refreshed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在事件调度线程上睡觉。相反,应将
SwingWorker
与JProgressBar
结合使用,如 如何使用进度条。如果您正在读取文件,ProgressMonitorInputStream
可能会很方便。Don't sleep on the event dispatch thread. Instead, use a
SwingWorker
in combination with aJProgressBar
, as shown in How to Use Progress Bars. If you're reading from a file,ProgressMonitorInputStream
may be convenient.您应该更新事件调度线程上的面板。如果您还在 EDT 上调用 Thread.sleep,则无法重新绘制面板,因为线程正在休眠。
如果您想每秒更新大小,您可以考虑使用
Timer
(Swing 版本,而不是 java.util 版本) 并更新该 Timer 的ActionListener
中面板的大小。不确定您的代码中是否存在拼写错误,您的意思是“gauge”iso“guage”,但在这种情况下,有 网络上提供的一些非常好的仪表示例
You should update your panel on the Event Dispatch Thread. If you also call
Thread.sleep
on the EDT, your panel can not be repainted since the Thread is sleeping.If you want to update the size each second you could consider using a
Timer
(the Swing version, not the java.util version) and update the size of your panel in theActionListener
of that Timer.Not sure whether it is a typo in your code and you meant 'gauge' iso 'guage', but in that case there are some really nice examples of gauges available on the web