java聊天室出错,怎么解决

public void keyPressed(KeyEvent e)
{
TextField inputwords = (TextField) e.getSource();
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{ // 处理回车按键事件
if (userInputPad.userChoice.getSelectedItem().equals("所有用户"))
{ // 给所有人发信息
try
{
// 发送信息
outputStream.writeUTF(inputwords.getText());
inputwords.setText("");
}
catch (Exception ea)
{
userChatPad.chatTextArea
.setText("不能连接到服务器!\n");
userListPad.userList.removeAll();
userInputPad.userChoice.removeAll();
inputwords.setText("");
userControlPad.connectButton.setEnabled(true);
}
}
else
{ // 给指定人发信息
try
{
outputStream.writeUTF("/" + userInputPad.userChoice.getSelectedItem()
+ " " + inputwords.getText());
inputwords.setText("");
}
catch (Exception ea)
{
userChatPad.chatTextArea
.setText("不能连接到服务器!\n");
userListPad.userList.removeAll();
userInputPad.userChoice.removeAll();
inputwords.setText("");
userControlPad.connectButton.setEnabled(true);
}
}
}
}


当输入聊天信息时会出现如下错误
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTextField cannot be cast to java.awt.TextField
at wuziqi.FIRClient.keyPressed(FIRClient.java:305)
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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

1个回答

TextField inputwords = (TextField) e.getSource(); 应该是这一行强转失败,改成 JTextField inputwords = (JTextField) e.getSource(); 这样就行了吧

weixin_36825926
weixin_36825926 是的,谢谢,解决了
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!