用java语言编一个聊天小程序.docx
《用java语言编一个聊天小程序.docx》由会员分享,可在线阅读,更多相关《用java语言编一个聊天小程序.docx(13页珍藏版)》请在冰点文库上搜索。
用java语言编一个聊天小程序
用java语言编一个聊天小程序
要求:
使用图形用户界面。
能实现一个聊天室中多人聊天。
可以两人私聊。
提示:
使用socket通信
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
import.*;
importjava.io.*;
publicclasstest
{
publicstaticvoidmain(String[]args)
{
EventQueue.invokeLater(newRunnable()
{
publicvoidrun()
{
DrawFrameframe=newDrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
classDrawFrameextendsJFrame
{
publicDrawFrame()
{
setLocation(100,100);
setTitle("我的QQ");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//添加菜单项……
JMenuBarmenuBar=newJMenuBar();
setJMenuBar(menuBar);
JMenuedit=newJMenu("菜单");
menuBar.add(edit);
JMenuItemsset=newJMenuItem("设置");
JMenuItemeexit=newJMenuItem("退出");
edit.add(sset);
edit.addSeparator();
edit.add(eexit);
finalDrawPanelpan=newDrawPanel();
//添加设置对话框
sset.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevent)
{
finalJFramejf=newJFrame("设置");
jf.setSize(300,150);
jf.setLocation(200,200);
jf.setResizable(false);
jf.setVisible(true);
JPaneljp=newJPanel();
finalJTextFieldjt1=newJTextField(20);
finalJTextFieldjt2=newJTextField(20);
finalJTextFieldjt3=newJTextField(20);
jp.add(newJLabel("聊天端口:
"));
jf.add(jp);
jp.add(jt1);
jp.add(newJLabel("目标地址:
"));
jp.add(jt2);
jp.add(newJLabel("文件端口:
"));
jp.add(jt3);
JButtonjb1=newJButton("确定");
JButtonjb2=newJButton("取消");
jp.add(jb1);
jp.add(jb2);
//设置“设置”按钮
jb1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevent){
try{
intcat1=Integer.parseInt(jt1.getText());
pan.setCat1(cat1);//设置对话端口号
intcat2=Integer.parseInt(jt3.getText());
pan.setCat2(cat2);//设置文件传输端口号
Strings=jt2.getText();
pan.setIP(s);//设置IP地址
pan.ls();
}
catch(Exceptione)
{
e.printStackTrace();
}
jf.dispose();//关闭对话框
}
});
jb2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevent){
jf.dispose();
}
});
}
});
eexit.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevent){
System.exit(0);
}
});
add(pan);
pack();
}
publicstaticfinalintDEFAULT_WIDTH=400;
publicstaticfinalintDEFAULT_HEIGHT=400;
}
classDrawPanelextendsJPanel
{
DatagramSocketds;
DatagramPacketdp;
DatagramSocketds1;
DatagramPacketdp1;
Threadthread;
Threadthread1;
TextAreatextArea1=newTextArea(10,50);
TextAreatextArea2=newTextArea(10,50);
JTextFieldtextField=newJTextField(50);
staticintcat1,cat2;
staticStringIP;
publicvoidsetCat1(intcat)
{
cat1=cat;
}
publicvoidsetCat2(intcat)
{
cat2=cat;
}
publicvoidsetIP(Stringip)
{
IP=ip;
}
//重置端口号
publicvoidls()
{
try
{
ds=newDatagramSocket(cat1);
ds1=newDatagramSocket(cat2);
}
catch(Exceptione)
{
e.printStackTrace();
}
thread=newThread(newRunnable()
{
publicvoidrun()
{
bytebuf[]=newbyte[1024];//聊天每次发送文字不能超过1024b
DatagramPacketdp=newDatagramPacket(buf,buf.length);
while(true)
{
try
{
ds.receive(dp);
textArea1.setText(textArea1.getText()+newString(buf,0,dp.getLength())+"\r\n");
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
});
thread.start();
}
publicDrawPanel()
{
setLayout(newBorderLayout());
JTabbedPanecard=newJTabbedPane();
textArea1.setEditable(false);
card.add("聊天",textArea1);
add(card,BorderLayout.NORTH);
JPanelcenter=newJPanel();
center.setLayout(newFlowLayout());
center.add(newJLabel("昵称:
"));
center.add(textField);
add(center,BorderLayout.CENTER);
JPanelsouth=newJPanel();
south.setLayout(newBorderLayout());
south.add(textArea2,BorderLayout.NORTH);
JPanelsouth2=newJPanel();
south2.setLayout(newFlowLayout());
JButtonsent=newJButton("传送文件");
JButtonreceive=newJButton("接收文件");
//receive.setEnabled(false);
JButtonsentMessage=newJButton("发送");
south2.add(sent);
south2.add(receive);
south2.add(sentMessage);
add(south,BorderLayout.SOUTH);
south.add(south2,BorderLayout.SOUTH);
//发送信息的按钮的监听器:
sentMessage.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
textArea1.append(textField.getText()+":
"+textArea2.getText()+"\n");
Strings;
try
{
s=textField.getText()+":
"+textArea2.getText();
byte[]buf=s.getBytes();
dp=newDatagramPacket(buf,buf.length,InetAddress.getByName(IP),cat1);
ds.send(dp);
textArea2.setText("");
}
catch(Exceptionex)
{
ex.printStackTrace();
}
}
});
//发送文件的按钮的监听器
sent.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
JFramef=newJFrame("打开");
f.setSize(400,300);
JFileChooserchooser=newJFileChooser();
f.add(chooser);
intreturnVal=chooser.showOpenDialog(f);
if(returnVal==JFileChooser.APPROVE_OPTION)
{
textArea1.append("正在传送文件:
"+chooser.getCurrentDirectory()+"\\"
+chooser.getSelectedFile().getName()+"\n");
try
{
byte[]b=newbyte[10248];
FileInputStreamfis=newFileInputStream(chooser.getSelectedFile());
DataInputStreamdos=newDataInputStream(fis);
dos.read(b);
dp1=newDatagramPacket(b,b.length,InetAddress.getByName(IP),cat2);
ds1.send(dp1);
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
});
//接收文件的按钮的监听器:
receive.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
JFramef1=newJFrame("保存");
f1.setSize(400,300);
bytebuf[]=newbyte[10248];
DatagramPacketdp1=newDatagramPacket(buf,buf.length);
JFileChooserch=newJFileChooser();
f1.add(ch);
inta=ch.showSaveDialog(f1);
if(a==JFileChooser.APPROVE_OPTION)
{
StringfileName=ch.getSelectedFile().getPath();
try
{
ds1.receive(dp1);
FileOutputStreamfos=newFileOutputStream(fileName);
fos.write(buf);
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
});
}
}