摇摆工人不工作
A swingworker does not work
在我的应用程序中,我有两个不同的 swingworkers(他们做两个不同的工作)。第一个完美,而后者不完美。
我能做些什么来解决它?
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 的 jbuttons
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);
}
特别的错误
启用日志记录后,发现 NumberFormatException
,包裹在 ExecutionException
中。
在我的应用程序中,我有两个不同的 swingworkers(他们做两个不同的工作)。第一个完美,而后者不完美。
我能做些什么来解决它?
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 的 jbuttons
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);
}
特别的错误
启用日志记录后,发现 NumberFormatException
,包裹在 ExecutionException
中。