一、程序设计方面的说明
程序分为三个类:
- ChatFrame类
- ChatClientAPP类
- ChatServerAPP类
二、各个类介绍
1、ChatFrame类
package com.chat; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatFrame extends JFrame{ private static final long serialVersionUID = -499796932332494480L; public JTextArea jta; public JScrollPane jsp; public JPanel jp; public JTextField jtf; public JButton jb; public String title; public ChatFrame(String title){ this.title = title; jta = new JTextArea(); jta.setEditable(false); jsp = new JScrollPane(jta); jp = new JPanel(); jtf = new JTextField(10); jb = new JButton("发送"); jp.add(jtf); jp.add(jb); this.add(jsp, BorderLayout.CENTER); this.add(jp, BorderLayout.SOUTH); this.setTitle(title); this.setSize(300, 300); this.setLocation(500, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } }
2、ChatClientAPP类
客户端类
package com.chat; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.UIManager; public class ChatClientAPP implements ActionListener, KeyListener { public static void main(String[] args) throws Exception { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); new ChatClientAPP(new ChatFrame("繁星聊天客户端")); } private BufferedWriter bh = null; private JTextField jtf; private JTextArea jta; public ChatClientAPP(ChatFrame chatFrame) { this.jtf = chatFrame.jtf; this.jta = chatFrame.jta; chatFrame.jb.addActionListener(this); jtf.addKeyListener(this); try { Socket Socket = new Socket("127.0.0.1", 8888); // InputStream in=Socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(Socket.getInputStream())); bh = new BufferedWriter(new OutputStreamWriter(Socket.getOutputStream())); String line = null; Date sb = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(); sdf.format(sb); jta.append("\t" + sdf.format(sb) + "\n"); while ((line = br.readLine()) != null) { jta.append(line + System.lineSeparator()); } Socket.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { wdw(); } // 定义一个方法 private void wdw() { String text = jtf.getText(); text = "客户端对服务端说:" + text; jta.append(text + System.lineSeparator()); try { bh.write(text); bh.newLine(); bh.flush(); jtf.setText(""); } catch (IOException e1) { e1.printStackTrace(); } } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { wdw(); } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } }
3、ChatClientAPP类
服务端类
package com.chat; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.UIManager; public class ChatServerAPP implements ActionListener, KeyListener { public static void main(String[] args) throws Exception { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); new ChatServerAPP(new ChatFrame("繁星聊天服务端")); } private JTextArea jta; private JTextField jtf; private BufferedWriter bh = null; public ChatServerAPP(ChatFrame chatFrame) { this.jtf = chatFrame.jtf; this.jta = chatFrame.jta; chatFrame.jb.addActionListener(this); jtf.addKeyListener(this); try { @SuppressWarnings("resource") ServerSocket ServerSocket = new ServerSocket(8888); Socket Socket = ServerSocket.accept(); // InputStream in=Socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(Socket.getInputStream())); bh = new BufferedWriter(new OutputStreamWriter(Socket.getOutputStream())); String line = null; Date sb = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(); sdf.format(sb); jta.append("\t" + sdf.format(sb) + "\n"); while ((line = br.readLine()) != null) { jta.append(line + System.lineSeparator()); } Socket.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { wdw(); } // 定义一个方法 private void wdw() { String text = jtf.getText(); text = "服务端对客户端说:" + text; jta.append(text + System.lineSeparator()); try { bh.write(text); bh.newLine(); bh.flush(); jtf.setText(""); } catch (IOException e1) { e1.printStackTrace(); } } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { wdw(); } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } }