聊天框java代码 java简单的聊天窗口代码( 五 )


}
else
sendmsg(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*-- 将信息进行转发 --*/
public void sendmsg(String msg)
{
user us = new user();
DataOutputStream os = null;
if(lists.size()0)
{
for(int i=0;ilists.size();i++)
{
us = lists.get(i);
try {
os = new DataOutputStream(us.socket.getOutputStream());
os.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
JOptionPane.showMessageDialog(null, "当前无用户在线 。发送消息失败","失败",JOptionPane.OK_OPTION);
}
public void destory()
{
try {
this.ss.close();
} catch (IOException e) {
e.printStackTrace();
}
this.dispose();
}
public static void main(String[] args)
{
new ServerFrame();
}
}
=================================================
package server;
import java.net.*;
public class user {
String name;
Socket socket;
}
急需一个java编程实现的简单聊天窗口代码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientDemo01 {
public static void main(String[] args){
JFrame f=new JFrame("AA");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(15,30);
ta.setEditable(false);//文本域只读
JScrollPane sp=new JScrollPane(ta);//滚动窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("发送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
socket=new Socket("192.168.0.4",5000);
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread01 mt=new MyThread01(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener01(tf,ta,bos));
}
}
class ButtonActionListener01 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText();
if(!message.equals("")){
tf.setText("");//清空文本框
ta.append("AA:"+message+"\n");//添加到文本域并换行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("发送失败");
}
}
}
}
class MyThread01 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread01(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("BB:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
} import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ServerDemo01{
public static void main(String[] args){
JFrame f=new JFrame("BB");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(12,30);//文本域,第一个参数为行数,第二个参数为列数

推荐阅读