SwingWorker:类型的非法开始
这是一段代码导致了我的问题:
SwingWorker <Vector,void> sw=new SwingWorker <Vector,void>(){
@Override
protected Vector doInBackground() throws Exception {
TvaJpaController tjc =new TvaJpaController(emf);
Vector l_tva=null;
try{
l_tva= (Vector) tjc.findTvaEntities();
}
catch(javax.persistence.PersistenceException e)
{
javax.swing.JOptionPane.showMessageDialog(null,"please check your internet connecting");
}
return l_tva;
}
@Override
protected void done() {
Vector l_tva=null;
try {
l_tva=get();
} catch (InterruptedException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
int n = l_tva.size();
for(int i=0;i<n;i++){
Tva tva =(Tva)l_tva.elementAt(i);
tva_article.addItem(tva.getIdtva());
}
}
};
sw.execute();
这一行:
SwingWorker <Vector,void> sw=new SwingWorker <Vector,void>()
给出错误:非法开始类型... 我认为我的问题是由于“向量”引起的,但我不知道如何解决.. 有什么帮助吗?
it is a piece of code that caused my problem :
SwingWorker <Vector,void> sw=new SwingWorker <Vector,void>(){
@Override
protected Vector doInBackground() throws Exception {
TvaJpaController tjc =new TvaJpaController(emf);
Vector l_tva=null;
try{
l_tva= (Vector) tjc.findTvaEntities();
}
catch(javax.persistence.PersistenceException e)
{
javax.swing.JOptionPane.showMessageDialog(null,"please check your internet connecting");
}
return l_tva;
}
@Override
protected void done() {
Vector l_tva=null;
try {
l_tva=get();
} catch (InterruptedException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
int n = l_tva.size();
for(int i=0;i<n;i++){
Tva tva =(Tva)l_tva.elementAt(i);
tva_article.addItem(tva.getIdtva());
}
}
};
sw.execute();
this line :
SwingWorker <Vector,void> sw=new SwingWorker <Vector,void>()
gives an error :illegal start of type...
I think my problem was due to "vector",but I do not know how to solve..
Any Helps ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,问题是使用了
void
,它不是有效的类型参数。不过,您可以使用SwingWorker
。 (请注意 Java 关键字void
和引用java.lang.Void
类型的Void
之间的区别。)建议优先使用
List
而不是显式使用Vector
,并在可能的情况下一般使用它,并使用ArrayList
作为实施而不是Vector
,但这是一个单独的问题 - 只有void
/Void
会导致您立即出现问题。No, the problem is the use of
void
, which isn't a valid type argument. You can useSwingWorker<Vector, Void>
though. (Note the difference betweenvoid
, which is a Java keyword, andVoid
which refers to thejava.lang.Void
type.)Personally I'd suggest using
List<E>
in preference to explicitly usingVector
, and using it generically if possible, withArrayList<E>
as an implementation rather thanVector
, but that's a separate matter - it's only thevoid
/Void
which is causing you immediate problems.第二个问题是关于 SwingWorker 的
void done()
中方法get()
的错误使用,因为仅返回来自 SwingWorker(且仅当存在异常时),有关方法get()
在我的问题and second issue is that about wrong usage of method
get()
into SwingWorker'svoid done()
, because returns only exception from SwingWorker (and only if exception exists), more about usage of methodget()
in my question