程序主要分为三大部分,登陆程序,服务器程序,客服端程序。主要涉及到的知识为Java基础语法知识,图形界面,线程,以及网络通信技术等。
首先讲一下登陆模块,功能就是实现用户的登陆,图形的布局是用的BorderLayout结合GridLayout,,另外为Login按钮设置了事件监听,当用户输入完成后,点击按钮,登陆程序会启动一个对象,那就是客服端的程序,并同时将用户输入的昵称和服务器地址传送过去,之后登陆界面将影藏起来。
接下来就是客服端程序,图形界面我用的也是Bordlayout结合Gridlayout,并弄了一个可以滚动的显示框,程序核心部分是通过socker对服务器进行连接,当用户点击发送按钮的时候会将文本框的内容发送到服务器,接收是通过创建一个线程来完成的
服务器和客服端差不多,主要是网络那一部分用的是服务端的模型来建立的。
如下是本程序的一些截图。
源程序:
//Login.java import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; @SuppressWarnings("serial") public class Login extends JFrame implements ActionListener{ JFrame jf = new JFrame("login"); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JLabel l1 = new JLabel("请输入您得昵称"); JLabel l2 = new JLabel("请设置服务器地址"); JTextField t1 = new JTextField(); JTextField t2 = new JTextField(); JButton loginButton = new JButton("Login"); JButton exitButton = new JButton("Exit"); public Login(){ p1.setLayout(new GridLayout(2,2)); p1.add(l1); p1.add(t1); p1.add(l2); p1.add(t2); p2.add(loginButton); jf.getContentPane().add(BorderLayout.NORTH,p1); jf.getContentPane().add(BorderLayout.CENTER,p2); jf.setSize(200,200); jf.setVisible(true); loginButton.addActionListener(this); } public static void main(String[] args){ Login login = new Login(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(!t1.getText().equals("") && !t2.getText().equals("")){ TestClient c=new TestClient(t1.getText(),t2.getText());//启动客户端聊天主窗口 jf.setVisible(false); } } }
//TestClient.java import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @SuppressWarnings("serial") public class TestClient extends JFrame implements ActionListener { DataInputStream dis; DataOutputStream dos; String s1,s2,s3,sna,sip; Socket s; String sss=""; JTextField tf;//文本编辑框 JTextArea ta;//文本显示 JButton bt;//按钮 JFrame jf = new JFrame(); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); public TestClient(String sna, String sip){ this.sna = sna; this.sip = sip; this.setTitle("聊天程序客服端"); tf = new JTextField(40); JScrollPane jp = new JScrollPane(); ta = new JTextArea(10,10); jp.setViewportView(ta); bt = new JButton("发送"); p2.setLayout(new GridLayout(1,2)); p2.add(tf); p2.add(bt); this.getContentPane().add(BorderLayout.CENTER,jp); this.getContentPane().add(BorderLayout.SOUTH,p2); bt.addActionListener(this); this.setSize(200,200); this.setVisible(true); try { s = new Socket(sip,777); dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); this.createReadThread(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //发送 s2 = tf.getText(); s3 = sna + "说: " + s2; ta.append("自己说: "+ s2 + "\n"); try { dos.writeUTF(s3); tf.setText(sss); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public void createReadThread(){ ClientReadThread rt=new ClientReadThread(this.ta,this.dis); rt.start(); } }
//ClientReadThread.java import java.io.DataInputStream; import java.io.IOException; import javax.swing.JTextArea; public class ClientReadThread extends Thread{ JTextArea ta; DataInputStream dis; String text; public ClientReadThread(JTextArea t,DataInputStream d){ this.ta=t; this.dis=d; } //线程实现的功能 public void run(){ try{ text=dis.readUTF();//读取输入流的信息UTF-8多国码 while(!text.equals("quit")){ ta.append("服务器回复: "+text); ta.append("\n"); text=dis.readUTF(); } }catch(IOException e){ System.out.println("连接结束!"); } finally{ System.exit(0); } } }
//TestServer.java import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; @SuppressWarnings("serial") public class TestServer extends JFrame implements ActionListener { DataInputStream dis; DataOutputStream dos; String s1,s2; ServerSocket ss; Socket s; String sss=""; JTextField tf;//文本编辑框 JTextArea ta;//文本显示 JButton bt;//按钮 JFrame jf = new JFrame(); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); public TestServer(){ this.setTitle("聊天程序服务端"); tf = new JTextField(40); JScrollPane jp = new JScrollPane(); ta = new JTextArea(10,10); jp.setViewportView(ta); bt = new JButton("发送"); p2.setLayout(new GridLayout(1,2)); p2.add(tf); p2.add(bt); this.getContentPane().add(BorderLayout.CENTER,jp); this.getContentPane().add(BorderLayout.SOUTH,p2); bt.addActionListener(this); this.setSize(200,200); this.setVisible(true); try { ss = new ServerSocket(777); s = ss.accept(); dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); //接收 this.createReadThread(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public static void main(String[] args){ TestServer login = new TestServer(); } public void createReadThread(){ ServerReadThread rt=new ServerReadThread(this.ta,this.dis); rt.start(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s2 = tf.getText(); ta.append("自己说: "+ s2 + "\n"); try { dos.writeUTF(s2); tf.setText(sss); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
//ServerReadThread.java import java.io.DataInputStream; import java.io.IOException; import javax.swing.JTextArea; public class ServerReadThread extends Thread{ DataInputStream dis; JTextArea ta; String stemp; public ServerReadThread(JTextArea ta, DataInputStream dis){ this.dis = dis; this.ta = ta; } public void run() { try { stemp = dis.readUTF(); while(true) { ta.append(stemp+"\n"); stemp = dis.readUTF(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }