package netware;import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class MyChat extends JFrame
{
private JLabel yourip=new JLabel("输入对方IP:");
private JLabel myip=new JLabel("本机IP及端口:");
private JLabel s=new JLabel("按回车发送信息:");
private JTextField ip=new JTextField(12);
private JTextField mp=new JTextField(12);
private List rec=new List(12);
private JTextField sen=new JTextField(20);
JButton send=new JButton("发送");
JButton cancle=new JButton("清空记录");
private Date date=new Date();
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=f.format(date);
String line=System.getProperty("line.separator");
DatagramSocket ds=null;
public MyChat() throws Exception
{
rec.setForeground(Color.BLUE);
sen.setFont(new Font("微软雅黑",Font.BOLD,13));
sen.setForeground(Color.orange);
rec.setFont(new Font("微软雅黑",Font.BOLD,13));
//rec.setLineWrap(true);
this.setLocation(100,100);
//rec.setEditable(false);
rec.add("在IP输入框里输入对方IP就可以和她(他)开始通讯了!"+line+"版权所有:叶有为");
rec.add(line);
rec.add("***********************************************************************************************");
rec.add(line);
JScrollPane jp=new JScrollPane(rec);
this.setTitle("叶有为通讯软件");
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
ds=new DatagramSocket(3000);
this.add(jp,"North");
this.add(s,"Center");
this.add(sen,"South");
JPanel p=new JPanel();
p.setLayout(new GridLayout(3,3));
p.add(yourip);
p.add(ip);
p.add(myip);
p.add(mp);
p.add(send);
p.add(cancle);
this.add(p,"East");
ActionListener a=new ActionListener()
{ @Override
public void actionPerformed(ActionEvent e)
{
if(ip.getText()!=null&&sen.getText()!=null)
{
byte[] buf=new byte[1024];
buf=sen.getText().getBytes();
try
{
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName(ip.getText()),3000);
ds.send(dp);
} catch (Exception e2)
{
// TODO: handle exception
}
rec.add(line,0);
rec.add(time+" "+"我说:"+sen.getText(),0);
rec.add(line);
sen.setText("");
}
}
};
sen.addActionListener(a);
send.addActionListener(a);
new Thread(new Runnable()
{ @Override
public void run()
{
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
while(true)
{
try
{
ds.receive(dp);
if(dp.getLength()>0)
{
rec.add(line,0);
rec.add(time+" "+"from:"+dp.getAddress().getHostAddress()+":"+dp.getPort()+"说:"+new String(buf,0,buf.length),0);
rec.add(line);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}).start();
cancle.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e)
{
rec.removeAll();
rec.add("在IP输入框里输入对方IP就可以和她(他)开始通讯了!"+line+"版权所有:叶有为");
rec.add(line);
rec.add("**************************************************************************************************");
sen.setText("");
}
});
this.setVisible(true);
mp.setText(InetAddress.getLocalHost()+":"+"3000");
}
public static void main(String[] args) throws Exception
{
MyChat m=new MyChat();
m.setSize(500,320);
m.setResizable(false);
}
}在运行的时候 在发送一串中文后在发送就会有乱码出现 不知道什么回事。哪位朋友能帮忙找出bug。另外怎么设置发送的和接收的字体的颜色 谢谢啦。
我现在起不来两个实例了,Bind 错误~
Exception in thread "main" java.net.BindException: Address already in use: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at MyChat.<init>(MyChat.java:68)
at MyChat.main(MyChat.java:163)
我去掉了port 3000的bind。。
byte[] buf=new byte[1024];
buf=sen.getText().getBytes();不要new了,多余了。
{ @Override
public void run()
{
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
while(true)
{
try
{
ds.receive(dp);
if(dp.getLength()>0)
{
rec.add(line,0);
String text = new String(buf,0, dp.getLength());
System.out.println(text);
rec.add(time+" "+"from:"+dp.getAddress().getHostAddress()+":"+dp.getPort()+"说:"+ text,0);
rec.add(line);
}
} catch (Exception e)
{
e.printStackTrace();
}
String text = new String(buf,0, dp.getLength());
改了就应该对了。
[email protected]
MSN
哥们你其他的都写出来了,怎么这个问题还搞不定呢?
比如说第一次你收到了 abcd而第二次,你收到了 e。结果就变成了 ebcd.明白了吧?buffer没有清空,
我倒是没有清空你buffer,但是精确到长度也可以,简单。