第二个实例创建一个java工程,基于tomcat服务器,程序运行时会启动客户端,实现了一个客户端向其他的客户端发送即时信息的功能
MainWindow.java
package com.jikexueyuan.myjavachatclient.view; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder; import com.jikexueyuan.myjavachatclient.main.ChatManager; public class MainWIndow extends JFrame { /**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JTextArea txt;
private JTextField ip;
private JTextField send; /**
* Create the frame.
*/
public MainWIndow() {
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(, , , );
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(, , , ));
setContentPane(contentPane); txt = new JTextArea();
txt.setText("Ready..."); ip = new JTextField();
ip.setText("127.0.0.1");
ip.setColumns(); JButton button = new JButton("连接到服务器");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ChatManager.getCM().connect(ip.getText());
}
}); send = new JTextField();
send.setText("你好");
send.setColumns(); JButton button_1 = new JButton("发送");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ChatManager.getCM().send(send.getText());
appendText("我说:"+send.getText());
send.setText("");
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(txt, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(ip, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(send, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, , GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(ip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(button))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(txt, GroupLayout.DEFAULT_SIZE, , Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(send, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(button_1)))
);
contentPane.setLayout(gl_contentPane);
} public void appendText(String in) {
txt.append("\n"+in);
}
}
StartClient.java
package com.jikexueyuan.myjavachatclient.main; import java.awt.EventQueue; import com.jikexueyuan.myjavachatclient.view.MainWIndow; public class StartClient { public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWIndow frame = new MainWIndow();
frame.setVisible(true);
ChatManager.getCM().setWindow(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} }
ChatManager.java
package com.jikexueyuan.myjavachatclient.main; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException; import com.jikexueyuan.myjavachatclient.view.MainWIndow; public class ChatManager { private ChatManager(){}
private static final ChatManager instance = new ChatManager();
public static ChatManager getCM() {
return instance;
} MainWIndow window;
String IP;
Socket socket;
BufferedReader reader;
PrintWriter writer; public void setWindow(MainWIndow window) {
this.window = window;
window.appendText("文本框已经和ChatManager绑定了。");
} public void connect(String ip) {
this.IP = ip;
new Thread(){ @Override
public void run() {
try {
socket = new Socket(IP, );
writer = new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream())); reader = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
window.appendText("收到:"+line);
}
writer.close();
reader.close();
writer = null;
reader = null;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} public void send(String out) {
if (writer != null) {
writer.write(out+"\n");
writer.flush();
}else {
window.appendText("当前的链接已经中断");
}
}
}
将工程运行起来,这里我启动2个客户端
实现了即时通信