import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import java.util.GregorianCalendar;
import javax.swing.JDialog;
import java.io.*;
public class MyselfChat extends Frame implements ActionListener{
Label label1=new Label("请输入你要发送的信息(限英文):");
Label label3=new Label("把以上消息发给右边所填写的IP地址");
TextArea input=new TextArea("",7,14,TextArea.SCROLLBARS_VERTICAL_ONLY);
TextArea output=new TextArea("",18,14,TextArea.SCROLLBARS_VERTICAL_ONLY);
TextField IPAdd=new TextField("10.255.8.86");
Button send=new Button("发送消息");
Button clear=new Button("清空消息");
GregorianCalendar time = new GregorianCalendar();
Container con;
MenuItem readin;
MyselfChat(){
//下面代码用来实现菜单栏!
MenuBar mb = new MenuBar();
Menu file = new Menu("文件");
Menu chat = new Menu("聊天");
Menu disport = new Menu("娱乐");
Menu yingyong = new Menu("应用");
Menu toolkits = new Menu("工具");
Menu help = new Menu("帮助");
Menu readout = new Menu("导出[O]");
MenuShortcut sl1 = new MenuShortcut('I'), //实现快捷键功能
sl2 = new MenuShortcut('O'),
sl3 = new MenuShortcut('B'),
sl4 = new MenuShortcut('C'),
sl5 = new MenuShortcut('K'),
sl6 = new MenuShortcut('E'),
sl7 = new MenuShortcut('U'),
sl8 = new MenuShortcut('D');
myMenuItemListener itemListener = new myMenuItemListener(this);
readin = new MenuItem("导入[I]",sl1);
readin.addActionListener(itemListener);
file.add(readin);
MenuItem backUp = new MenuItem("备份[B].....",sl3);
backUp.addActionListener(itemListener);
readout.add(backUp);
readout.addSeparator();
MenuItem backUpIntxt = new MenuItem("导出聊天记录为文本文件[C]",sl4);
backUpIntxt.addActionListener(itemListener);
readout.add(backUpIntxt);
MenuItem backUpInbackUptxt = new MenuItem("导出聊天记录为备份文件[K]",sl5);
backUpInbackUptxt.addActionListener(itemListener);
readout.add(backUpInbackUptxt);
readout.addActionListener(itemListener);
file.add(readout);
file.addSeparator(); //在菜单项之间加上分割线
MenuItem UpPass = new MenuItem("上传聊天记录[U]",sl7);
UpPass.addActionListener(itemListener);
file.add(UpPass);
MenuItem DownLoad = new MenuItem("下载聊天记录[D]",sl8);
DownLoad.addActionListener(itemListener);
file.add(DownLoad);
file.addSeparator(); //在菜单项之间加上分割线
MenuItem Exit = new MenuItem("退出[E]",sl6);
Exit.addActionListener(itemListener);
file.add(Exit);
MenuItem shipin = new MenuItem("视频聊天");
shipin.addActionListener(itemListener);
chat.add(shipin);
MenuItem yuyin = new MenuItem("语音聊天");
yuyin.addActionListener(itemListener);
chat.add(yuyin);
MenuItem music = new MenuItem("音乐");
music.addActionListener(itemListener);
disport.add(music);
MenuItem film = new MenuItem("电影");
film.addActionListener(itemListener);
disport.add(film);
MenuItem game = new MenuItem("游戏");
game.addActionListener(itemListener);
disport.add(game);
MenuItem sharefiles = new MenuItem("共享文件");
sharefiles.addActionListener(itemListener);
yingyong.add(sharefiles);
MenuItem distancehelp = new MenuItem("远程协助");
distancehelp.addActionListener(itemListener);
yingyong.add(distancehelp);
MenuItem view = new MenuItem("视图");
view.addActionListener(itemListener);
toolkits.add(view);
MenuItem addfriend = new MenuItem("添加好友");
addfriend.addActionListener(itemListener);
toolkits.add(addfriend);
MenuItem about = new MenuItem("关于");
about.addActionListener(itemListener);
help.add(about);
mb.add(file);
mb.add(chat);
mb.add(disport);
mb.add(yingyong);
mb.add(toolkits);
mb.add(help);
this.setMenuBar(mb);
//con = this.getContentPane();
this.setLayout(new BorderLayout());
this.setSize(480,500);
String s = this.IPAdd.getText();
this.setTitle("与"+s+"聊天中");
this.setResizable(false);
this.setBackground(Color.orange);
label1.setFont(new Font("宋体",Font.PLAIN,12));
label1.setForeground(new Color(0,0,192));
label1.setBounds(8,28,216,16);
input.setBackground(new Color(128,255,255));
input.setFont(new Font("",Font.BOLD,15));
input.setForeground(Color.magenta);
input.setBounds(8,44,248,120);
output.setBackground(new Color(128,255,255));
output.setFont(new Font("",Font.PLAIN,12));
output.setForeground(Color.magenta);
output.setBounds(264,44,248,120);
output.setEditable(false);
send.setFont(new Font("新宋题",Font.PLAIN,12));
send.setLocation(136,188);
send.setSize(120,22);
clear.setFont(new Font("新宋体",Font.PLAIN,12));
clear.setLocation(392,188);
clear.setSize(120,22);
label3.setFont(new Font("宋体",Font.PLAIN,12));
label3.setForeground(new Color(0,0,192));
label3.setBounds(8,172,160,16);
IPAdd.setFont(new Font("新宋体",Font.PLAIN,12));
IPAdd.setLocation(8,190);
IPAdd.setSize(120,19);
Panel p1 = new Panel();
p1.setLayout(new GridLayout(2,1));
p1.add(this.label1);
p1.add(this.input);
add(p1,BorderLayout.CENTER);
Panel p2 = new Panel();
p2.setLayout(new GridLayout(1,1));
p2.add(this.output);
add(p2,BorderLayout.NORTH);
Panel p3 = new Panel();
p3.setLayout(new FlowLayout());
p3.add(this.label3);
p3.add(this.IPAdd);
p3.add(this.send);
p3.add(this.clear);
add(p3,BorderLayout.SOUTH);
addWindowListener(new myWindowListener(this));
send.addActionListener(this);
clear.addActionListener(this);
show();
waitForData();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==send){
sendData();
}
else if(e.getSource()==clear)
output.setText("");
}
public static void main(String[] args){ //主函数
new MyselfChat();
}
void sendData(){
try{
String msg=input.getText();
if(msg.equals(""))
return;
input.setText("");
String ad = IPAdd.getText(); //获取用户输入的IP地址
InetAddress tea = InetAddress.getLocalHost(); //获取本机的IP地址
String asd = tea.getHostAddress();
output.append("["+asd+"]:("+time.get(GregorianCalendar.YEAR)
+"-"+time.get(GregorianCalendar.MONTH)+
"-"+time.get(GregorianCalendar.DATE)+""
+time.get(GregorianCalendar.HOUR)+":"
+time.get(GregorianCalendar.MINUTE)+":"
+time.get(GregorianCalendar.SECOND)+")"+"\n"+msg+"\n");
msg="From ["+asd+"]:("+time.get(GregorianCalendar.YEAR)
+"-"+time.get(GregorianCalendar.MONTH)+
"-"+time.get(GregorianCalendar.DATE)+""
+time.get(GregorianCalendar.HOUR)+":"
+time.get(GregorianCalendar.MINUTE)+":"
+time.get(GregorianCalendar.SECOND)+") \n"+msg ;
InetAddress address=InetAddress.getByName(ad);
int len = msg.length();
byte[] message=new byte[len];
msg.getBytes(0,len,message,0);
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket( );
socket.send(packet);
}catch(Exception e){}
}
void waitForData(){
try{
byte[]