黑马程序员技术交流社区
标题:
聊天小程序(GUI)
[打印本页]
作者:
qihuan
时间:
2015-7-25 19:05
标题:
聊天小程序(GUI)
分享一个用GUI实现的一个聊天小程序,大家帮我看看还有什么问题以及如何改进。
package netPractice;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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;
/**
* UDP实现的一个聊天软件
* 有收数据的部分,和发数据的部分。
* 这两部分需要同时执行。
* 那就需要用到多线程技术。
* 一个线程控制收,一个线程控制发。
* @author Qihuan
*
*/
class Gui {
Frame f;
TextArea taShow;
TextField tfSend;
Button bSend;
void init() {
f = new Frame("NewChat");
f.setBounds(300,100,300,500);
f.setLayout(new FlowLayout());
taShow = new TextArea(27,36);
tfSend = new TextField(29);
bSend = new Button("Send");
f.add(taShow);
f.add(tfSend);
f.add(bSend);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
}
private Gui(){}
public static Gui r = new Gui();
public static Gui getInstance(){
return r;
}
}
//发送端
class SendMs implements Runnable {
private DatagramSocket ds;
public SendMs(DatagramSocket ds) {
this.ds = ds;
}
public void run() {
try {
Gui.getInstance().init();
Gui.getInstance().bSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SendMessage();
}
});
Gui.getInstance().tfSend.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
SendMessage();
}
}
});
} catch (Exception e) {
throw new RuntimeException("发送失败");
}
}
protected void SendMessage() {
String line = Gui.getInstance().tfSend.getText();
byte[] buf = line.getBytes();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.0.255"),10005);
ds.send(dp);
} catch (IOException e1) {
e1.printStackTrace();
}
Gui.getInstance().tfSend.setText(null);
}
}
//接收端
class ReceiveMs implements Runnable {
private DatagramSocket ds;
public ReceiveMs(DatagramSocket ds) {
this.ds = ds;
}
public void run() {
try {
// Gui.getInstance().init();
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String name = dp.getAddress().getHostName();
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, dp.getLength());
Gui.getInstance().taShow.append(name+"("+ip+"):"+data+"\r\n");
System.out.println(name+"("+ip+"):"+data);
}
} catch (Exception e) {
throw new RuntimeException("接收失败");
}
}
}
public class NewChat {
public static void main(String[] args) {
try {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(10005);
SendMs sm = new SendMs(sendSocket);
ReceiveMs rm = new ReceiveMs(receiveSocket);
new Thread(sm).start();
new Thread(rm).start();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2