package cn.dom; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; //客户端 public class ClientChat extends JFrame implements ActionListener{ private JButton btnSend;//发送按钮 private JTextArea jtaRecord;//聊天记录 private JTextField jtfMessage;//消息框 private ObjectOutputStream out;//输出流对象 private ObjectInputStream in;//输入流对象 private String msg=""; private String chatSerer; private Socket client; public ClientChat() { super("客户端"); setSize(400,320); setLocation(100,100); setResizable(false); Container container=getContentPane(); JPanel pnlTop=new JPanel(); pnlTop.setLayout(new FlowLayout(FlowLayout.LEFT)); container.add(pnlTop,BorderLayout.NORTH); JPanel pnlRecord=new JPanel(); pnlTop.add(new JLabel("聊天内容:")); pnlTop.add(pnlRecord); jtaRecord=new JTextArea(12,30); jtaRecord.setEditable(false); jtaRecord.setLineWrap(true); pnlRecord.add(jtaRecord); JScrollPane scroll=new JScrollPane(jtaRecord); scroll.setSize(336,113); pnlRecord.add(scroll); JPanel pnlBottom=new JPanel(); pnlBottom.setLayout(new FlowLayout(FlowLayout.LEFT)); container.add(pnlBottom,BorderLayout.SOUTH); pnlBottom.add(new JLabel("消息")); jtfMessage=new JTextField(25); pnlBottom.add(jtfMessage); btnSend=new JButton("发送"); pnlBottom.add(btnSend); btnSend.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } //启动客户端 public void runClient() { try { connectServer(); getStream(); communication(); }catch(EOFException e) { System.out.println("服务器中止了连接!!!"); }catch(IOException e) { e.printStackTrace(); }finally { closeConnection(); } } private void connectServer() throws IOException{ displayMessage("正在尝试连接....\n"); client=new Socket(InetAddress.getByName(chatSerer),9999); displayMessage("已连接到:"+client.getInetAddress().getHostName()+"\n"); } //读取流信息 private void getStream() throws IOException{ out=new ObjectOutputStream(client.getOutputStream()); out.flush(); in=new ObjectInputStream(client.getInputStream()); } //连接成功进行聊天 private void communication() throws IOException{ setTextFieldEditable(true); do { try { msg=(String) in.readObject(); displayMessage("\n"+msg); }catch(ClassNotFoundException e){ displayMessage("\n收到异常对象类型!\n"); } }while(!msg.equals("服务器:stop")); } //关闭连接 private void closeConnection() { displayMessage("\n关闭连接!!!\n"); setTextFieldEditable(false); try { out.close(); in.close(); client.close(); }catch(IOException e) { e.printStackTrace(); } } //发送数据 private void sendData(String msg) { try { out.writeObject("客户端:"+msg); out.flush(); displayMessage("\n客户端:"+msg); }catch(IOException e) { jtaRecord.append("\n发生写入错误!!!"); } } //显示消息 private void displayMessage(final String msg) { SwingUtilities.invokeLater(new Runnable() { public void run() { jtaRecord.append(msg); jtaRecord.setCaretPosition(jtaRecord.getText().length()); } }); } //设置文本框状态 private void setTextFieldEditable(final boolean editable) { SwingUtilities.invokeLater(new Runnable() { public void run() { jtfMessage.setEditable(editable); } }); } @ Override public void actionPerformed(ActionEvent event) { sendData(jtfMessage.getText()); String label=event.getActionCommand(); switch (label) { case "发送": jtfMessage.setText(""); break; } } public static void main(String[] args) { new ClientChat().runClient(); } }
最近下载更多
zeng1206 LV5
2023年12月28日
testuser1234567 LV24
2022年5月31日
g1679456349 LV1
2022年5月24日
liys1234 LV9
2022年4月22日
1265260263 LV4
2022年4月8日
sfdasdgdgaf LV2
2022年3月19日
1612769136 LV1
2021年12月31日
Demo1111 LV30
2021年12月10日
胡萝北呀 LV3
2021年11月26日
曹思辰 LV6
2021年11月21日
最近浏览更多
taoshen95 LV14
1月19日
zeng1206 LV5
2023年12月28日
lshlsh
2023年12月25日
暂无贡献等级
微信网友_6762641760833536
2023年12月4日
暂无贡献等级
jkjfdgbkl LV2
2023年11月1日
fuyouou LV5
2023年6月29日
2017143155 LV12
2023年6月24日
lclclclccc
2023年6月2日
暂无贡献等级
青柠编程 LV13
2023年4月20日
your namelll
2023年2月20日
暂无贡献等级