局域网之间的小QQ聊天小程序

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 16:59   11   0
package QQ;


import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Random;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


import com.sun.org.omg.CORBA.Initializer;


public class QQa {

private JFrame frame;
private JTextField ipField;
private JTextField portField;
private JTextField myPortField;
private JTextField sendMsgField;
private JTextArea msgArea;
private DatagramSocket datagramSocket;


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run() {
QQa window = new QQa();
window.frame.setVisible(true);
}
});
}

public QQa(){
initialize();
}

private void initialize(){
frame=new JFrame();
frame.setBounds(150, 150, 800, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel ipLable=new JLabel("");
ipLable.setBounds(33, 10, 54, 15);
frame.getContentPane().add(ipLable);

ipField=new JTextField();
ipLable.setLabelFor(ipField);
ipField.setBounds(97, 7, 125, 21);
frame.getContentPane().add(ipField);
ipField.setColumns(10);

JLabel myportLable=new JLabel("");
myportLable.setBounds(417, 10, 104, 15);
frame.getContentPane().add(myportLable);

JLabel label = new JLabel("");
label.setBounds(244, 10, 82, 15);
frame.getContentPane().add(label);

portField = new JTextField();
label.setLabelFor(portField);
portField.setBounds(323, 7, 66, 21);
frame.getContentPane().add(portField);
portField.setColumns(10);

myPortField=new JTextField();
myPortField.setEditable(false);
myPortField.setBounds(531, 7, 66, 21);
frame.getContentPane().add(myPortField);
myPortField.setColumns(10);

JScrollPane scrollPane=new JScrollPane();
scrollPane.setBounds(33, 43, 645, 314);
frame.getContentPane().add(scrollPane);

msgArea=new JTextArea();
msgArea.setEditable(false);
scrollPane.setViewportView(msgArea);
msgArea.setRows(8);

JLabel sendMsgLabel=new JLabel("");
sendMsgLabel.setBounds(33, 367, 82, 15);
frame.getContentPane().add(sendMsgLabel);

sendMsgField=new JTextField();
sendMsgField.setBounds(107, 364, 510, 21);
frame.getContentPane().add(sendMsgField);
sendMsgField.setColumns(10);

JButton sendMsgBtn=new JButton("发送");

sendMsgBtn.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent e) {
String msg=sendMsgField.getText();
if(msg!=null && !msg.equals("")){
msgArea.append("我说:"+msg+"\n");
sendMsgField.setText(null);
try {
byte[] bytes=msg.getBytes("utf-8");

String ip=ipField.getText();
int port=Integer.parseInt(portField.getText());

DatagramPacket packet=new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(ip,port));
datagramSocket.send(packet);


} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();
} catch (IOException eee) {

eee.printStackTrace();
}

}

}


});
sendMsgBtn.setBounds(632, 363, 66, 23);
frame.getContentPane().add(sendMsgBtn);

int myport=new Random().nextInt(1000)+8000;
try {
this.datagramSocket=new DatagramSocket(myport);
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.myPortField.setText(""+myport);
Thread recieveMsg=new Thread(){
public void run() {
byte[] bytes=new byte[1024];
do{
DatagramPacket packet=new DatagramPacket(bytes,0,bytes.length);
try {
datagramSocket.receive(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


String msg;
try {
msg = new String(bytes,0,packet.getLength(),"utf-8");
msgArea.append(packet.getAddress().getHostAddress()+"说:"+msg+"\n");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}while(true);



}
};
recieveMsg.start();

}




}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP