我是JProgressBar
的新手,也是OOPs的新手(学习它),阅读了很多文章,并尽我最大的努力学习它。所示的所有示例都有一个开始执行进度的按钮,但是如何制作一个方法,以便在调用该方法时开始进度?
这是我的代码优先
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class ProgressDemo
{
JProgressBar ProgressBar;
Task task;
boolean done = false;
class Task extends SwingWorker<Void, Void>
{
@Override
public Void doInBackground()
{
Random random = new Random();
int progress = 0;
setProgress(0);
while (progress < 100)
{
try
{
Thread.sleep(random.nextInt(1000));
}
catch (InterruptedException ignore){}
progress += random.nextInt(10);
setProgress(Math.min(progress, 100));
}
return null;
}
@Override
public void done()
{
done = true;
}
}
public void propertyChange(PropertyChangeEvent evt)
{
if(!done)
{
int progress = task.getProgress();
if(progress==0)
{
ProgressBar.setIndeterminate(true);
}
else
{
ProgressBar.setIndeterminate(false);
ProgressBar.setString(null);
ProgressBar.setValue(progress);
}
}
}
//I want to make a method here, so that it can be called when ever I want
public void ExecuteTask()
{
task = new Task();
task.execute();
}
public static void main(String[] args)
{
JFrame MainScreen = new JFrame();
JPanel MainPanel = new JPanel();
ProgressBar = new JProgressBar(0, 100);
ProgressBar.setValue(0);
ProgressBar.setStringPainted(false);
MainPanel.add(ProgressBar);
MainScreen.getContentPane().add(MainPanel);
MainScreen.setVisible(true);
}
}
调用ExecuteTask()方法时,我的
jProgressBar
没有监听。我该如何调用ExecuteTask()并执行任务,然后jProgressBar监听它? 最佳答案
您永远不会将PropertyChangeListener添加到任何内容,因此,由于没有人在侦听SwingWorker的更改,因此它的更改永远不会传输到JProgressBar。解决方案:将PropertyChangeListener添加到SwingWorker之后。实际上,您没有PropertyChangeListener,只是从一个您未使用的方法(??)借用的方法。
其他问题:学习OOP,至少要了解Swing的基础知识。您不应该像在这里那样使用静态字段。
一些体面的链接:
Cant get JProgressBar to update from SwingWorker class
Understanding Java ExecutorService:我最近的最爱。
JProgressBar not working properly:检查MadProgrammer的几个链接。他是这方面的专家,几乎是任何Swing
叹
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class ProgressDemo extends JPanel {
private JProgressBar progressBar = new JProgressBar(0, 100);
private Task task;
boolean done = false;
public ProgressDemo() {
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
add(progressBar);
}
class Task extends SwingWorker<Void, Void> {
@Override
public Void doInBackground() {
Random random = new Random();
int progress = 0;
setProgress(0);
while (progress < 100) {
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {
}
progress += random.nextInt(10);
setProgress(Math.min(progress, 100));
}
return null;
}
@Override
public void done() {
done = true;
}
}
private class TaskListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
int progress = task.getProgress();
progressBar.setIndeterminate(false);
progressBar.setString(null);
progressBar.setValue(progress);
}
if (SwingWorker.StateValue.DONE == evt.getNewValue()) {
// always need to know when the SW is done
// so we can call get() and trap exceptions
try {
task.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
// I want to make a method here, so that it can be called when ever I want
public void executeTask() {
task = new Task();
task.addPropertyChangeListener(new TaskListener());
task.execute();
}
public static void main(String[] args) {
JFrame mainScreen = new JFrame();
mainScreen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final ProgressDemo demoPanel = new ProgressDemo();
mainScreen.getContentPane().add(demoPanel);
mainScreen.pack();
mainScreen.setLocationByPlatform(true);
mainScreen.setVisible(true);
// just for yucks, let's wait 3 seconds before calling executeTask()
int delay = 3 * 1000;
new Timer(delay, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
demoPanel.executeTask();
// stop timer
((Timer) e.getSource()).stop();
}
}).start();
}
}
关于java - 没有JButton和PropertyChangeListener的JProgressBar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26005435/