在循环(线程)中使 JProgressBar 更新值时出现问题
我试图让我的程序在执行某些操作时在方法内不断更新进度条值。然而,这种情况直到最后才发生,用户界面冻结了。
在查看了与我的问题类似的问题后,我尝试实现已接受的解决方案(使用线程),但我无法让它正常工作。就像他们不在那里一样。
我的程序包含多个类,Main
是 Netbeans 在 JFrame Design 模式下自动创建的类,因此存在某些内容,例如 static void main< /code> 和
public Main
不太确定其中的某些内容。下面我将把这些方法的片段以及我的线程实现放在一起。
public class Main extends javax.swing.JFrame implements ActionListener, Runnable{
// I added implements ActLis, Runn.....
...
static Main _this; // I included this variable
...
public static void main(String args[]) {
Main m = new Main(); // Added by me
new Thread(m).start(); // Added by me
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
...
public Main() {
initComponents();
_this = this; // Added by me
}
...
// I also included these 2 methods in the class
public void actionPerformed(ActionEvent e) {
synchronized(this){
notifyAll();
}
}
public void run() {
try{synchronized(this){wait();}}
catch (InterruptedException e){}
progressBar.setValue(50);
}
...
private void buttonPressed(java.awt.event.MouseEvent evt) {
for(int i=0; i<=100; i++) {
for(int j=0; j<=5; j++) {
// does some work
}
run();
}
}
我评论为我添加...
的所有内容都是我根据我在网上看到的教程和答案放置的内容,但似乎没有任何效果,感觉我已经尝试了近一百万次不同的组合...
提前感谢您的帮助。
Am trying to get my program to update the progress bar values constantly within a method while performing some operations. However this does not happen until the end, and the UI freezes.
After looking around to similar questions with my problems, I tried to implement the accepted solutions (Using threads) however I cannot get it to work correctly. Is just like if they where not there.
My program contains several classes, the Main
being the one automatically created by netbeans on the JFrame Design mode, so there are certain things such as the static void main
and the public Main
that am not really sure of some of its contents. Under i will put the snippets of those methods, together with my thread implementation.
public class Main extends javax.swing.JFrame implements ActionListener, Runnable{
// I added implements ActLis, Runn.....
...
static Main _this; // I included this variable
...
public static void main(String args[]) {
Main m = new Main(); // Added by me
new Thread(m).start(); // Added by me
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
...
public Main() {
initComponents();
_this = this; // Added by me
}
...
// I also included these 2 methods in the class
public void actionPerformed(ActionEvent e) {
synchronized(this){
notifyAll();
}
}
public void run() {
try{synchronized(this){wait();}}
catch (InterruptedException e){}
progressBar.setValue(50);
}
...
private void buttonPressed(java.awt.event.MouseEvent evt) {
for(int i=0; i<=100; i++) {
for(int j=0; j<=5; j++) {
// does some work
}
run();
}
}
All the things that I commented as I added...
are things that I putted according to tutorials and answers I have seen online, but nothing seems to work and it feels like I have tried close to a million different combinations...
Thanks in advance for helping out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些基础供您查看,如果您可以研究它并理解为什么每段代码在那里,那么我认为这会有所帮助。请随意在评论中提出问题(尽管我现在就要睡觉了!)
示例:
解决问题的两种不同方法,使用 SwingWorker:
SwingWorker 示例 1:
SwingWorker 示例 2(不太好,但仍然很有趣):
Here's some basis for you to look at, if you can study it and understand why every piece of code is there then I think it will help. Feel free to ask questions in a comment (although I'm going to bed right now!)
Example:
Two different ways to approach the problem, using SwingWorker instead:
SwingWorker Example 1:
SwingWorker Example 2 (not so nice, but interesting nonetheless):
您的代码有一些问题:(
忘记教程,从头开始编写代码,只添加您理解的内容。
There are a few things wrong with your code :(
forget tutorials, write the code from scratch and only add things to it you understand.