求问在java编程中出现这个问题怎么解决啊,求具体解决方法,谢谢
Exceptioninthread"AWT-EventQueue-0"java.lang.ClassCastException:javax.swing.JTextFiel...
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTextField cannot be cast to java.awt.TextField
at djr.chess.client.FIRClient.keyPressed(FIRClient.java:308)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) 展开
at djr.chess.client.FIRClient.keyPressed(FIRClient.java:308)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) 展开
展开全部
看错误啊Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTextField cannot be cast to java.awt.TextField
,类型转换错误,应该是导错包了
,类型转换错误,应该是导错包了
追问
新手求具体指导,怎么改过来啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
javax.swing.JTextField cannot be cast to java.awt.TextField看到这句话了吗?你把两个实例弄混淆了,javax.swing.JTextField 只可以用javax.swing.JTextField 来实例化, java.awt.TextField只能用 java.awt.TextField来实例化,不要弄错了.
下面的博客是对两个的比较,还是蛮不错的
http://blog.csdn.net/iamluole/article/details/8142257
下面的博客是对两个的比较,还是蛮不错的
http://blog.csdn.net/iamluole/article/details/8142257
更多追问追答
追问
求问该怎么改正啊,谢谢
追答
更正的话,如果程序都是用的awt,建议吧java.swing.JTextField删除;反之,如果用的是swing,则吧java.awt.JTextField删除,不过swing是更好的,因为可以跨平台
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JTextField不是继承自TextField而是
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
不能强转
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
不能强转
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你找一下,定义的 JTextFiled 类型的输入框 是不能强制转为TextField 类型的输入框,前者是JDK1.4之后才推出的,在java.swing包中,后者在java.awt包中;
最可能出现问题的地方:
(TextFiled)tf ; 将JTextField 类型变量tf强制转化为TextField;
传递参数类型不对;应该传递TexitField类型的参数,而传递成了TextField
具体的错误,要把代码贴出来!
追问
public void keyPressed(KeyEvent e)
{
TextField inputwords = (TextField) e.getSource();
这是显示错误部分的代码,求怎么改正啊,谢谢
追答
TextField inputwords = (TextField) e.getSource();
改为:
JTextField inputwords = (JTextField) e.getSource();
//注解:
e.getSource(); 是获取发生事件的组件;
根据上下文,估计是你定义了一个JTextField 类型的输入框,然后给这个输入框添加了一个键盘看下的事件。所以,e.getSource();获取的组件应该是JTextField 类型的,而不是TextField。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
别用强制类型转换
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询