java GUI简易聊天程序.docx_第1页
java GUI简易聊天程序.docx_第2页
java GUI简易聊天程序.docx_第3页
java GUI简易聊天程序.docx_第4页
java GUI简易聊天程序.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

简易聊天程序,java GUI。(先运行服务器端,后运行客户端)服务器端/* * To change this template, choose Tools | Templates * and open the template in the editor. */package TCPServer;import .*;import java.util.Date;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class TCPServer extends JFrame String str1; JLabel l1, l2; JTextField jtf1; JTextArea jt1, jt2; JButton jb1, jb2; Container con = this.getContentPane(); Socket s; public TCPServer() throws IOException this.setBounds(100, 100, 400, 500); this.setTitle(服务端); con.setLayout(new FlowLayout(); l1 = new JLabel(请输入聊天内容,按发送按钮发送聊天信息,无用户时不可聊天); l2 = new JLabel(输入路径); jtf1 = new JTextField(10); jt1 = new JTextArea(3, 24); jt2 = new JTextArea(18, 30); jt1.setLineWrap(true); jt2.setLineWrap(true); jt1.setBackground(Color.pink); jt2.setBackground(Color.pink); jt1.setEditable(false); jt2.setEditable(false); jb1 = new JButton(发送); jb2 = new JButton(保存聊天记录); jb1.setEnabled(false); JScrollPane jsp = new JScrollPane(jt2); jb1.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) OutputStream os; DataOutputStream dos = null; Date date = new Date(); int hour = date.getHours(); int minute = date.getMinutes(); int second = date.getSeconds(); String min = minute 9 ? (minute + ) : (0 + minute); String sec = second 9 ? (second + ) : (0 + second); try os = s.getOutputStream(); dos = new DataOutputStream(os); catch (IOException e2) String str = jt1.getText(); jt1.setText(); jt2.setEditable(true); jt2.append(服务端 + + hour + : + min + : + sec + rn + str + rn); jt2.setEditable(false); try dos.writeUTF(str); dos.flush(); catch (IOException e1) ); jb2.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) if (jtf1.getText().trim().equals() JOptionPane.showMessageDialog(null, 您还未输入保存路径!); else String url = jtf1.getText(); File f = new File(url, 与 + str1 + 聊天记录.txt); try BufferedWriter bw = new BufferedWriter(new FileWriter(f); bw.write(jt2.getText(); bw.flush(); bw.close(); JOptionPane.showMessageDialog(null, 保存成功); catch (IOException e1) ); con.add(l1); con.add(jt1); con.add(jb1); con.add(jsp); con.add(l2); con.add(jtf1); con.add(jb2); this.setResizable(false); this.setVisible(true); ServerSocket ss = new ServerSocket(8889); while (true) s = ss.accept(); System.out.println(A client has been connected.); DataInputStream dis = new DataInputStream(s.getInputStream(); System.out.println(dis.readUTF(); str1 = dis.readUTF(); this.setTitle(服务端与 + str1 + 聊天中); jb1.setEnabled(true); jt1.setEditable(true); while (true) Date date = new Date(); int hour = date.getHours(); int minute = date.getMinutes(); int second = date.getSeconds(); String min = minute 9 ? (minute + ) : (0 + minute); String sec = second 9 ? (second + ) : (0 + second); String str = jt1.getText(); jt2.append(str1 + + hour + : + min + : + sec + rn + dis.readUTF() + rn);/一次只能打印一个,采用死循环 public static void main(String args) throws IOException TCPServer t1 = new TCPServer(); t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我是分隔线客户端/* * To change this template, choose Tools | Templates * and open the template in the editor. */package TCPClient;import .*;import java.util.Date;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class TCPClient extends JFrame String str1 = ; while (true) str1 = JOptionPane.showInputDialog(请输入你的网名:); if (str1 != null) break; JLabel l1, l2; JTextField jtf1; JTextArea jt1, jt2; JButton jb1, jb2; Container con = this.getContentPane(); Socket s = new Socket(, 8889); OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(连接成功!n你好,服务端,我是: + str1); dos.writeUTF(str1); dos.flush(); public TCPClient() throws Exception this.setBounds(600, 100, 400, 500); this.setTitle(用户: + str1); con.setLayout(new FlowLayout(); l1 = new JLabel(请输入聊天内容,按发送按钮发送聊天信息,无用户时不可聊天); l2 = new JLabel(输入路径); jtf1 = new JTextField(10); jt1 = new JTextArea(3, 24); jt2 = new JTextArea(18, 30); jt1.setLineWrap(true); jt2.setLineWrap(true); jt1.setBackground(Color.pink); jt2.setBackground(Color.pink); jt2.setEditable(false); jb1 = new JButton(发送); jb2 = new JButton(保存聊天记录); JScrollPane jsp = new JScrollPane(jt2); con.add(l1); con.add(jt1); con.add(jb1); con.add(jsp); con.add(l2); con.add(jtf1); con.add(jb2); this.setResizable(false); this.setVisible(true); jb1.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) Date date = new Date(); int hour = date.getHours(); int minute = date.getMinutes(); int second = date.getSeconds(); String min = minute 9 ? (minute + ) : (0 + minute); String sec = second 9 ? (second + ) : (0 + second); String str = jt1.getText(); jt1.setText(); jt2.setEditable(true); jt2.append(str1 + + hour + : + min + : + sec + rn + str + rn); jt2.setEditable(false); try dos.writeUTF(str); dos.flush(); catch (IOException e1) ); jb2.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) if (jtf1.getText().trim().equals() JOptionPane.showMessageDialog(null, 您还未输入保存路径!); else String url = jtf1.getText(); File f = new File(url, 与服务端聊天记录.txt); try BufferedWriter bw = new BufferedWriter(new FileWriter(f); bw.write(jt2.getText(); bw.flush(); bw.close(); JOptionPane.showMessageDialog(null, 保存成功); catch (IOException e1) ); while (true) DataInputStream dis = new DataInputStream(s.getInputStream(); while (true) Date date = new Date(); int hour = date.getHours(); int minute = date.getMinutes(); int second = date.getSeconds(); String min = minute 9 ? (minute + ) : (0 + minute); String se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论