聊天应用程序出错

编程入门 行业动态 更新时间:2024-10-27 00:30:41
本文介绍了聊天应用程序出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是java的新手。我尝试制作聊天应用程序,但是当我运行单个客户端时会出现一些错误。为什么文本区域和文本字段不显示。我得到的是由于接受函数而发生的。当编译器达到接受函数时,应用程序变得忙碌。即应用程序的屏幕不显示。

I am newbie to java. I tried to make chat app but some error occur when i run even single client .Why the Text Area and Text Field does not show. What i get is this occurs due to accept function .When compiler reaches to accept function the app become busy. i.e the screen of app show nothing.

客户代码一:

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.*; public class ChatAppOne{ JFrame jframe; JTextArea jtextarea; JTextField jtextfield; JButton jbutton; ServerSocket server; Socket sSocket,cSocket; InputStream inStream; ObjectInputStream objInStream; OutputStream outStream; ObjectOutputStream objOutStream; ChatAppOne(){ jframe=new JFrame(); jframe.setLayout(new FlowLayout(FlowLayout.LEFT)); jtextarea=new JTextArea("",28,49); jtextarea.setEditable(false); jframe.add(new JScrollPane(jtextarea)); jtextfield=new JTextField(); jtextfield.setPreferredSize(new Dimension(440,30)); jframe.add(jtextfield); jbutton=new JButton("Send"); jbutton.setPreferredSize(new Dimension(100,35)); jframe.add(jbutton); jbutton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cSocket=new Socket("localhost",1340); outStream=cSocket.getOutputStream(); objOutStream=new ObjectOutputStream(outStream); objOutStream.writeObject(jtextfield.getText()); jtextarea.setText(jtextarea.getText() +" \n " +"Me: " +jtextfield.getText()); jtextfield.setText(""); cSocket.close(); outStream.close(); objOutStream.close(); }catch(Exception e){ JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE); } } }); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setSize(600,600); jframe.setTitle("Chat Application"); jframe.setVisible(true); startServer(); } void startServer(){ try{ server=new ServerSocket(1550); }catch(Exception e){ JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE); } while(true){ try{ sSocket=server.accept(); inStream=sSocket.getInputStream(); objInStream=new ObjectInputStream(inStream); String msg=(String) objInStream.readObject(); jtextarea.setText("App Two : "+ msg); sSocket.close(); inStream.close(); objInStream.close(); }catch(Exception e){ JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE); } } } public static void main(String args[]){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new ChatAppOne(); } }); } }

客户2与客户相同,除非服务器和客户端的端口差异。

Client Two is same as Client except port difference in server and client.

客户端代码2

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.*; public class ChatAppTwo{ JFrame jframe; JTextArea jtextarea; JTextField jtextfield; JButton jbutton; ServerSocket server; Socket sSocket,cSocket; InputStream inStream; ObjectInputStream objInStream; OutputStream outStream; ObjectOutputStream objOutStream; ChatAppTwo(){ jframe=new JFrame(); jframe.setLayout(new FlowLayout(FlowLayout.LEFT)); jtextarea=new JTextArea("",28,49); jtextarea.setEditable(false); jframe.add(new JScrollPane(jtextarea)); jtextfield=new JTextField(); jtextfield.setPreferredSize(new Dimension(440,30)); jframe.add(jtextfield); jbutton=new JButton("Send"); jbutton.setPreferredSize(new Dimension(100,35)); jframe.add(jbutton); jbutton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ try{ cSocket=new Socket("localhost",1550); outStream=cSocket.getOutputStream(); objOutStream=new ObjectOutputStream(outStream); objOutStream.writeObject(jtextfield.getText()); jtextarea.setText(jtextarea.getText() +" \n " +"Me: " +jtextfield.getText()); jtextfield.setText(""); cSocket.close(); outStream.close(); objOutStream.close(); }catch(Exception e){ JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE); } } }); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setSize(600,600); jframe.setTitle("Chat Application"); jframe.setVisible(true); startServer(); } void startServer(){ try{ server=new ServerSocket(1340); JOptionPane.showMessageDialog(null, "Server Started", "Error", JOptionPane.PLAIN_MESSAGE); }catch(Exception e){ JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE); } while(true){ try{ sSocket=server.accept(); inStream=sSocket.getInputStream(); objInStream=new ObjectInputStream(inStream); String msg=(String) objInStream.readObject(); jtextarea.setText("App Two : "+ msg); sSocket.close(); inStream.close(); objInStream.close(); }catch(Exception e){ JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE); } } } public static void main(String args[]){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new ChatAppTwo(); } }); } }

推荐答案

您的ServerSocket代码在事件调度线程(EDT)上阻塞,等待来自服务器的响应,因此GUI无法重新绘制自身。您需要在单独的线程上执行该代码,以便EDT可以自由重新绘制并响应用户事件。

Your ServerSocket code is blocking on the Event Dispatch Thread (EDT) waiting for a response from the server so the GUI can't repaint itself. You need to execute that code on a separate Thread so the EDT if free to repaint itself and respond to user events.

阅读 Swing中的并发以获取更多信息和示例。您可能会发现使用 SwingWorker 这是一个简单的解决方案,因为它为您管理线程。

Read the section from the Swing tutorial on Concurrency in Swing for more information and examples. You may find using a SwingWorker the easy solution as it manages the Thread for you.

更多推荐

聊天应用程序出错

本文发布于:2023-05-26 10:16:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/252256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file 'D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!