packageqqtalk;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.net.*;
importjava.io.*;
publicclass qqtalk
{
publicstatic void main(String[] args)
{
EventQueue.invokeLater(newRunnable()
{
publicvoid run()
{
DrawFrameframe = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
classDrawFrame extends JFrame
{
publicDrawFrame()
{
setLocation(100,100);
setTitle("我的QQ");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//添加菜单项……
JMenuBarmenuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenuedit = new JMenu("菜单");
menuBar.add(edit);
JMenuItemsset = new JMenuItem("设置");
JMenuItemeexit = new JMenuItem("退出");
edit.add(sset);
edit.addSeparator();
edit.add(eexit);
finalDrawPanel pan = new DrawPanel();
//添加设置对话框
sset.addActionListener(newActionListener(){
publicvoid actionPerformed(ActionEvent event)
{
finalJFrame jf = new JFrame("设置");
jf.setSize(300,150);
jf.setLocation(200,200);
jf.setResizable(false);
jf.setVisible(true);
JPaneljp = new JPanel();
finalJTextField jt1 = new JTextField(20);
finalJTextField jt2 = new JTextField(20);
finalJTextField jt3 = new JTextField(20);
jp.add(newJLabel("聊天端口:"));
jf.add(jp);
jp.add(jt1);
jp.add(newJLabel("目标地址:"));
jp.add(jt2);
jp.add(newJLabel("文件端口:"));
jp.add(jt3);
JButtonjb1 = new JButton("确定");
JButtonjb2 = new JButton("取消");
jp.add(jb1);
jp.add(jb2);
//设置“设置”按钮
jb1.addActionListener(newActionListener(){
publicvoid actionPerformed(ActionEvent event){
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(){
publicvoid actionPerformed(ActionEvent event){
jf.dispose();
}
});
}
});
eexit.addActionListener(newActionListener(){
publicvoid actionPerformed(ActionEvent event){
System.exit(0);
}
});
add(pan);
pack();
}
publicstatic final int DEFAULT_WIDTH = 400;
publicstatic final int DEFAULT_HEIGHT = 400;
}
classDrawPanel extends JPanel
{
DatagramSocketds;
DatagramPacketdp;
DatagramSocketds1;
DatagramPacketdp1;
Threadthread;
Threadthread1;
TextAreatextArea1 = new TextArea(10,50);
TextAreatextArea2 = new TextArea(10,50);
JTextFieldtextField = new JTextField(50);
staticint cat1,cat2;
staticString IP;
publicvoid setCat1(int cat)
{
cat1= cat;
}
publicvoid setCat2(int cat)
{
cat2= cat;
}
publicvoid setIP(String ip)
{
IP=ip;
}
//重置端口号
publicvoid ls()
{
try
{
ds= new DatagramSocket(cat1);
ds1= new DatagramSocket(cat2);
}
catch(Exceptione)
{
e.printStackTrace();
}
thread= new Thread(new Runnable()
{
publicvoid run()
{
bytebuf[] = new byte[1024];//聊天每次发送文字不能超过1024b
DatagramPacketdp = new DatagramPacket(buf,buf.length);
while(true)
{
try
{
ds.receive(dp);
textArea1.setText(textArea1.getText()+new String(buf,0,dp.getLength())
+"\r\n");
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
});
thread.start();
}
publicDrawPanel()
{
setLayout(newBorderLayout());
JTabbedPanecard = new JTabbedPane();
textArea1.setEditable(false);
card.add("聊天",textArea1);
add(card,BorderLayout.NORTH);
JPanelcenter = new JPanel();
center.setLayout(newFlowLayout());
center.add(newJLabel("昵称:"));
center.add(textField);
add(center,BorderLayout.CENTER);
JPanelsouth = new JPanel();
south.setLayout(newBorderLayout());
south.add(textArea2,BorderLayout.NORTH);
JPanelsouth2 = new JPanel();
south2.setLayout(newFlowLayout());
JButtonsent = new JButton("传送文件");
JButtonreceive = new JButton("接收文件");
//receive.setEnabled(false);
JButtonsentMessage = new JButton("发送");
south2.add(sent);
south2.add(receive);
south2.add(sentMessage);
add(south,BorderLayout.SOUTH);
south.add(south2,BorderLayout.SOUTH);
//发送信息的按钮的监听器:
sentMessage.addActionListener(newActionListener()
{
publicvoid actionPerformed(ActionEvent event)
{
textArea1.append(textField.getText()+":"+textArea2.getText()+"\n");
Strings;
try
{
s= textField.getText() + ":" + textArea2.getText();
byte[]buf = s.getBytes();
dp= new
DatagramPacket(buf,buf.length,InetAddress.getByName(IP),cat1);
ds.send(dp);
textArea2.setText("");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
//发送文件的按钮的监听器
sent.addActionListener(newActionListener()
{
publicvoid actionPerformed(ActionEvent event)
{
JFramef = new JFrame("打开");
f.setSize(400,300);
JFileChooserchooser = new JFileChooser();
f.add(chooser);
intreturnVal = chooser.showOpenDialog(f);
if(returnVal== JFileChooser.APPROVE_OPTION)
{
textArea1.append("正在传送文件:"+chooser.getCurrentDirectory()+"\\"+chooser.getSelectedFile().getName()+"\n");
try
{
byte[]b = new byte[10248];
FileInputStreamfis = new FileInputStream(chooser.getSelectedFile());
DataInputStreamdos =new DataInputStream(fis);
dos.read(b);
dp1= new
DatagramPacket(b,b.length,InetAddress.getByName(IP),cat2);
ds1.send(dp1);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
//接收文件的按钮的监听器:
receive.addActionListener(newActionListener()
{
publicvoid actionPerformed(ActionEvent event)
{
JFramef1 = new JFrame("保存");
f1.setSize(400,300);
bytebuf[] = new byte[10248];
DatagramPacketdp1 = new DatagramPacket(buf,buf.length);
JFileChooserch = new JFileChooser();
f1.add(ch);
inta = ch.showSaveDialog(f1);
if(a==JFileChooser.APPROVE_OPTION)
{
StringfileName = ch.getSelectedFile().getPath();
try
{
ds1.receive(dp1);
FileOutputStreamfos = new FileOutputStream(fileName);
fos.write(buf);
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
});
}
}