重写 SwingWorker 中的 process() 方法
Overriding the process() Method in SwingWorker
我正在尝试使用 String
动态更新 GUI。任何人都可以看到为什么 process 方法没有正确覆盖吗?我查看了其他问题,但仍然看不出我如何没有正确地覆盖它。
public class WorkerDemo extends JFrame {
private JLabel counterLabel = new JLabel("Not started");
private Worker worker = new Worker();
private JButton startButton = new JButton(new AbstractAction("Start") {
@Override
public void actionPerformed(ActionEvent arg0) {
worker = new Worker();
worker.execute();
}
});
private JButton stopButton = new JButton(new AbstractAction("Stop") {
@Override
public void actionPerformed(ActionEvent arg0) {
worker.cancel(true);
}
});
public WorkerDemo() {
add(startButton, BorderLayout.WEST);
add(counterLabel, BorderLayout.CENTER);
add(stopButton, BorderLayout.EAST);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class Worker extends SwingWorker<Void, Integer> {
int counter = 0;
String abc = "abc";
@Override
protected Void doInBackground() throws Exception {
while(true) {
abc += abc;
publish(abc);
Thread.sleep(60);
}
}
@Override
protected void process(List<Object> chunk) {
// get last result
String to_return = (String) chunk.get(chunk.size()-1);
counterLabel.setText(to_return);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new WorkerDemo();
}
});
}
}
既然你有SwingWorker<Void, Integer>
,你就定义了Integer
作为一种类型,通过publish和process方法进行中间结果。这意味着正确的 publish()
和 process()
方法应该使用 Integer
:
@Override
protected Void doInBackground() throws Exception {
int someResult = 0;
...
publish(someResult);
...
}
@Override
protected void process(List<Integer> chunk) {
}
有关详细信息,请参阅 SwingWorker。
我正在尝试使用 String
动态更新 GUI。任何人都可以看到为什么 process 方法没有正确覆盖吗?我查看了其他问题,但仍然看不出我如何没有正确地覆盖它。
public class WorkerDemo extends JFrame {
private JLabel counterLabel = new JLabel("Not started");
private Worker worker = new Worker();
private JButton startButton = new JButton(new AbstractAction("Start") {
@Override
public void actionPerformed(ActionEvent arg0) {
worker = new Worker();
worker.execute();
}
});
private JButton stopButton = new JButton(new AbstractAction("Stop") {
@Override
public void actionPerformed(ActionEvent arg0) {
worker.cancel(true);
}
});
public WorkerDemo() {
add(startButton, BorderLayout.WEST);
add(counterLabel, BorderLayout.CENTER);
add(stopButton, BorderLayout.EAST);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class Worker extends SwingWorker<Void, Integer> {
int counter = 0;
String abc = "abc";
@Override
protected Void doInBackground() throws Exception {
while(true) {
abc += abc;
publish(abc);
Thread.sleep(60);
}
}
@Override
protected void process(List<Object> chunk) {
// get last result
String to_return = (String) chunk.get(chunk.size()-1);
counterLabel.setText(to_return);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new WorkerDemo();
}
});
}
}
既然你有SwingWorker<Void, Integer>
,你就定义了Integer
作为一种类型,通过publish和process方法进行中间结果。这意味着正确的 publish()
和 process()
方法应该使用 Integer
:
@Override
protected Void doInBackground() throws Exception {
int someResult = 0;
...
publish(someResult);
...
}
@Override
protected void process(List<Integer> chunk) {
}
有关详细信息,请参阅 SwingWorker。