第二个实例创建一个java工程,基于tomcat服务器,程序运行时会启动客户端,实现了一个客户端向其他的客户端发送即时信息的功能
MainWindow.java
1 package com.jikexueyuan.myjavachatclient.view; 2 3 import java.awt.event.MouseAdapter; 4 import java.awt.event.MouseEvent; 5 6 import javax.swing.GroupLayout; 7 import javax.swing.GroupLayout.Alignment; 8 import javax.swing.JButton; 9 import javax.swing.JFrame; 10 import javax.swing.JPanel; 11 import javax.swing.JTextArea; 12 import javax.swing.JTextField; 13 import javax.swing.LayoutStyle.ComponentPlacement; 14 import javax.swing.border.EmptyBorder; 15 16 import com.jikexueyuan.myjavachatclient.main.ChatManager; 17 18 public class MainWIndow extends JFrame { 19 20 /** 21 * 22 */ 23 private static final long serialVersionUID = 1L; 24 private JPanel contentPane; 25 JTextArea txt; 26 private JTextField ip; 27 private JTextField send; 28 29 30 31 /** 32 * Create the frame. 33 */ 34 public MainWIndow() { 35 setAlwaysOnTop(true); 36 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 37 setBounds(100, 100, 450, 300); 38 contentPane = new JPanel(); 39 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 40 setContentPane(contentPane); 41 42 txt = new JTextArea(); 43 txt.setText("Ready..."); 44 45 ip = new JTextField(); 46 ip.setText("127.0.0.1"); 47 ip.setColumns(10); 48 49 JButton button = new JButton("连接到服务器"); 50 button.addMouseListener(new MouseAdapter() { 51 @Override 52 public void mouseClicked(MouseEvent arg0) { 53 ChatManager.getCM().connect(ip.getText()); 54 } 55 }); 56 57 send = new JTextField(); 58 send.setText("你好"); 59 send.setColumns(10); 60 61 JButton button_1 = new JButton("发送"); 62 button_1.addMouseListener(new MouseAdapter() { 63 @Override 64 public void mouseClicked(MouseEvent arg0) { 65 ChatManager.getCM().send(send.getText()); 66 appendText("我说:"+send.getText()); 67 send.setText(""); 68 } 69 }); 70 GroupLayout gl_contentPane = new GroupLayout(contentPane); 71 gl_contentPane.setHorizontalGroup( 72 gl_contentPane.createParallelGroup(Alignment.LEADING) 73 .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup() 74 .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) 75 .addGroup(gl_contentPane.createSequentialGroup() 76 .addContainerGap() 77 .addComponent(txt, GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)) 78 .addGroup(gl_contentPane.createSequentialGroup() 79 .addContainerGap() 80 .addComponent(ip, GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE) 81 .addPreferredGap(ComponentPlacement.RELATED) 82 .addComponent(button)) 83 .addGroup(gl_contentPane.createSequentialGroup() 84 .addComponent(send, GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE) 85 .addPreferredGap(ComponentPlacement.RELATED) 86 .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE))) 87 .addContainerGap()) 88 ); 89 gl_contentPane.setVerticalGroup( 90 gl_contentPane.createParallelGroup(Alignment.LEADING) 91 .addGroup(gl_contentPane.createSequentialGroup() 92 .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) 93 .addComponent(ip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 94 .addComponent(button)) 95 .addPreferredGap(ComponentPlacement.RELATED) 96 .addComponent(txt, GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE) 97 .addPreferredGap(ComponentPlacement.RELATED) 98 .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) 99 .addComponent(send, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 100 .addComponent(button_1))) 101 ); 102 contentPane.setLayout(gl_contentPane); 103 } 104 105 106 public void appendText(String in) { 107 txt.append(" "+in); 108 } 109 }
StartClient.java
1 package com.jikexueyuan.myjavachatclient.main; 2 3 import java.awt.EventQueue; 4 5 import com.jikexueyuan.myjavachatclient.view.MainWIndow; 6 7 public class StartClient { 8 9 public static void main(String[] args) { 10 EventQueue.invokeLater(new Runnable() { 11 public void run() { 12 try { 13 MainWIndow frame = new MainWIndow(); 14 frame.setVisible(true); 15 ChatManager.getCM().setWindow(frame); 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } 19 } 20 }); 21 } 22 23 }
ChatManager.java
1 package com.jikexueyuan.myjavachatclient.main; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.OutputStreamWriter; 7 import java.io.PrintWriter; 8 import java.net.Socket; 9 import java.net.UnknownHostException; 10 11 import com.jikexueyuan.myjavachatclient.view.MainWIndow; 12 13 14 public class ChatManager { 15 16 private ChatManager(){} 17 private static final ChatManager instance = new ChatManager(); 18 public static ChatManager getCM() { 19 return instance; 20 } 21 22 MainWIndow window; 23 String IP; 24 Socket socket; 25 BufferedReader reader; 26 PrintWriter writer; 27 28 public void setWindow(MainWIndow window) { 29 this.window = window; 30 window.appendText("文本框已经和ChatManager绑定了。"); 31 } 32 33 public void connect(String ip) { 34 this.IP = ip; 35 new Thread(){ 36 37 @Override 38 public void run() { 39 try { 40 socket = new Socket(IP, 12345); 41 writer = new PrintWriter( 42 new OutputStreamWriter( 43 socket.getOutputStream())); 44 45 reader = new BufferedReader( 46 new InputStreamReader( 47 socket.getInputStream())); 48 String line; 49 while ((line = reader.readLine()) != null) { 50 window.appendText("收到:"+line); 51 } 52 writer.close(); 53 reader.close(); 54 writer = null; 55 reader = null; 56 } catch (UnknownHostException e) { 57 e.printStackTrace(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 } 62 }.start(); 63 } 64 65 public void send(String out) { 66 if (writer != null) { 67 writer.write(out+" "); 68 writer.flush(); 69 }else { 70 window.appendText("当前的链接已经中断"); 71 } 72 } 73 }
将工程运行起来,这里我启动2个客户端
实现了即时通信