在我的应用程序中,我有两个不同的 Swing worker (他们从事两项不同的工作)。第一个完美地工作,而后者则不然。
我可以做什么来解决这个问题?
class worker extends SwingWorker<Boolean, String> {
@Override
protected Boolean doInBackground() throws Exception {
getstub().registration(name.getText(), surname.getText());
return true;
}
protected void done() {
boolean status;
try {
status = get();
regstatus.setText("registration status: " + status);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
}
}
class worker1 extends SwingWorker<Boolean, String> {
@Override
protected Boolean doInBackground() throws Exception {
int i = Integer.parseInt(id.getText());
double r = Double.parseDouble(range.getText());
double la = Double.parseDouble(lat.getText());
Coordinate lat = new DegreeCoordinate(la);
double lo = Double.parseDouble(lon.getText());
Coordinate lon = new DegreeCoordinate(lo);
System.out.println("id: "+i+" lat: "+la+ " lon: "+lo+" type: "+type.getText()+ " range: "+r);
getstub().request(i, lat ,lon ,type.getText(), r);
return true;
}
protected void done() {
boolean status;
try {
status = get();
reqstatus.setText("request status: " + status);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
}
}
和两个启动worker的jbutton
private JButton register = new JButton("Register");
register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new worker().execute();
}
});
private JButton search = new JButton("Search now");
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
reqstatus.setText("I'm searching...");
new worker1().execute();
}
});
最佳答案
大范围的问题
看起来您正在使用 ninja-exception-logging(不可见的):
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
在那里进行一些实际的日志记录,您很可能会发现问题。至少使用 System.out.println();更好的选择是 log4j .
} catch (InterruptedException e) {
//a thread exception, quite unlikely to happen
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
特定错误
启用日志记录后,发现包装在 ExecutionException
中的 NumberFormatException
。
关于java - Swing worker 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30832173/