📄 jim.java.bak
字号:
DatagramPacket sendPacket;
DatagramSocket sendSocket;
int sengPort=5000;
sendSocket=new DatagramSocket(sendPort);
Message msg=new Message(myImNo,myFriend.fImNo,"MESSAGE",
jtaSendMsg.getText().trim());
byte[] data;
data=msg.returnMessage().getBytes();
sendPacket=new DatagramPacket(data,data.length,InetAddress.getByName(myFriend.fIp.trim()),myFriend.fRPort);
sendSocket.send(sendPacket);
sendSocket.close();
jtaSendMsg.setText(" ");
}
catch(Exception e1)
{
e1.printStackTrace():
}
}
protected void processWindow(WindowEvent e)
{
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
myFriend.isShowDlg=false;
}
}
}
class Message
{
int fromImNo=-1;
int toImNo=-1;
String commandWord=" ";
String messageString=" ";
public Message(int fromImNo,int toImNo,String commandWord,String messageString)
{
this.fromImNo=fromImNo;
this.toImNo=toImNo;
this.commandWord=commandWord;
this.messageString=messageString;
}
public Message(String message)
{
int inx;
String tmpStr;
tmpStr=message.trim();
inx=tmpStr.indexOf("#");
fromImNo=Integer.parseInt(tmpStr.substring(0,inx));
tmpStr=tmpStr.substring(inx+1);
inx=tmpStr.indexOf("#");
toImNo=Integer.parseInt(tmpStr.substring(0,inx));
tmpStr=tmpStr.indexOf(":");
commandWord=tmpStr.substring(0,inx);
messageString=tmpStr.substring(inx+1);
}
public String returnMessage()
{
return
String.valueOf(fromImNo)+"#"+String.valueOf(toImNo)+"#"+commandWord+
":"+messageString;
}
}
class Friend
{
public String fNickName,fIp,fEmail,fHomePage,fPhone,fResume;
public int fImNo,fRPort,fFace,fSex,fAge,fOline;
String messageLog=" ";
boolean isShowDlg=false;
MessageDialog myDialog=null;
public Friend(String fNickName,String fIp,String fEMail,String fHomePage,String fPhone,String fResume,int fImNo,int fRPort,int fFace,int fSex,int fAge,int fOnline)
{
this.fNickName=fNickName;
this.fImNo=fImNo;
this.fIp=fIp;
this.fRPort=fRPort;
this.fEMail=fEMail;
this.fHomePage=fHomePage;
this.fPhone=fPhone;
this.fResume=fResume;
this.fFace=fFace;
this.fSex=fSex;
this.fAge=fAge;
this.fOnline=fOnline;
}
public void showMessageDlg(JimWin jimWin)
{
isShowDlg=true;
if(this.myDialog==null)
{
MessageDialog myDlg=new MessageDialog(jimWin.myImNo,this);
this.myDialog=myDlg;
myDlg.setLocationRelativeTo(jimWin);
}
this.myDialog.show();
}
}
class ShowFriendInfo extends JDialog
{
Friend myFriend;
Jpanel jpBottom=new JPanel();
JTextField jtNickName=new JTextField();
JLabel jlNickName=new JLabel();
JLabel jlImNo=new JLabel();
JTextField jtfImNo=new JTextField();
JComboBox jcbFace=new JComboBox();
JLabel jlSex=new JLabel();
JComboBox jcbSex=new JComboBox();
JLabel jlAge=new JLabel();
JTextField jtfAge=new JTextField();
JLabel jlPhone=new JLabel();
JTextField jtfPhone=new JTextField();
JTextField jtfEMail=new JTextField();
JLabel jlEmail=new JLabel();
JLabel jlHomePage=new JLabel();
JTextField jtfHomePage=new JTextField();
JLabel jlResume=new JLabel();
JTextPane jtpResume=new JTextPane();
JButton jbOK=new JButton();
public ShowFriendInfo(JimWin mainWin,Friend myFriend)
{
super(mainWin,"["+myFriend.fNickName+"]详细资料",true);
try
{
this.myFriend=myFriend;
showFriendInfoInit();
pack();
this.setSize(400,400);
this.setResizable(false);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
void showFriendInfoInit()throws Exception
{
String[] pics;
FilenameFilter fileFilter=new FilenameFilter(){
public boolean accept(File dir,String name){
if(name.endsWith("_1.JPG")||name.endsWith("_1.jpg")){
return true;
}
return false;
}
};
File dir=new File("face");
pics=dir.list(fileFilter);
jpBottom.setLayout(null);
this.getContentPane().setLayout(null);
jpBottom.setBounds(new Rectangle(0,0,550,550));
jlNickName.setText("昵称");
jlNickName.setBounds(new Rectangle(36,40,45,20));
jtNickName.setText(myFriend.fNickName);
jtNickName.setBounds(new Rectangle(70,40,180,22));
jlImNo.setText("JIM号");
jlImNo.setBounds(new Rectangle(27,70,45,20));
jtfImNo.setText(Integer.toString(myFriend.fImNo));
jtfImNo.setBounds(new Rectangle(70,70,180,22));
ComboBoxModel model=new HeadPicCombobox(pics);
ListCellRenderer renderer=new HeadpicCellRenderer();
jcbFace.setBounds(new Rectangle(290,40,80,50));
jcbFace.setModel(model);
jcbFace.setRenderer(renderer);
jcbFace.setSelectedIndex(myFriend.fFace-1);
jlSex.setText("性别");
jlSex.setBounds(new Rectangle(36,100,45,20));
jcbSex.setToolTipText("");
jcbSex.addItem("女");
jcbSex.addItem("男");
jcbSex.setBounds(new Rectangle(70,100,45,22));
jcbSex.setSelectedIndex(myFriend.fSex);
jlAge.setText("年龄");
jlAge.setBounds(new Rectangle(135,100,50,20));
jtfAge.setBounds(new Rectangle(170,100,40,22));
jtfAge.setText(Integer.toString(myFriend.fAge));
jlPhone.setText("电话");
jlPhone.setBounds(new Rectangle(225,100,50,20));
jtfPhone.setBounds(new Rectangle(260,100,110,22));
jtfPhone.setText(myFriend.fPhone);
jlEMail.setText("电子邮件");
jlEMail.setBounds(new Rectangle(10,130,60,20));
jtfEMail.setBounds(new Rectangle(70,130,300,22));
jtfEMail.setText(myFriend.fEMail);
jlHomePage.setText("个人主页");
jlHomePage.setBounds(new Rectangle(10,160,60,20));
jtfHomePage.setBounds(new Rectangle(70,160,300,22));
jtfHomePage.setText(myFriend.fHomePage);
jlResume.setText("个人说明");
jlResume.setBounds(new Rectangle(10,190,60,20));
jtfResume.setBounds(new Rectangle(70,190,300,110));
jtfResume.setText(myFriend.fResume);
jbOK.setText("确定");
jbOK.setBounds(new Rectangle(147,330,79,29));
jbOK.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseClicked(MouseEvent e){
jbOK_mouseClicked(e);
}
});
this.getContentPane().add(jpBottom,null);
jpBottom.add(jlNickName,null);
jpBottom.add(jtNickName,null);
jpBottom.add(jlImNo,null);
jpBottom.add(jtfImNo,null);
jpBottom.add(jcbFace,null);
jpBottom.add(jlSex,null);
jpBottom.add(jcbSex,null);
jpBottom.add(jlAge,null);
jpBottom.add(jtfAge,null);
jpBottom.add(jlPhone,null);
jpBottom.add(jtfPhone,null);
jpBottom.add(jtfEMail,null);
jpBottom.add(jlEMail,null);
jpBottom.add(jlHomePage,null);
jpBottom.add(jtfHomePage,null);
jpBottom.add(jlResume,null);
jpBottom.add(jtpResume,null);
jpBottom.add(jbOK,null);
}
void jbOK_mouseClicked(MouseEvent e)
{
this.dispose();
}
}
class AddFriend extends JDialog
{
int friendImNo;
JimWin mainWin;
JLabel jlFriendImNo;
JTextField jtfFriendImNo;
JButton btnOK;
public AddFriend(JimWin mainWin,int friendImNo)
{
super(mianWin,"添加好友",true);
this.friendImNo=friendImNo;
this.mainWin=mainWin;
if(friendImNo<0)
{
this.getContentPane().setLayout(null);
jlFriendImNo=new JLabel();
jlFriendImNo.setText("JIM号码");
btnOK=new JButton();
btnOK.setText("加为好友");
jlFriendImNo.setBounds(new Rectangle(30,15,90,20));
jtfFriendImNo.setBounds(new Rectangle(30,40,200,22));
btnOK.setBounds(new Rectangle(80,80,100,30));
btnOK.addMouseListener(new java.awt.event.MouseAdapter(){
public coid mouseClicked(MouseEvent e){
btnOK_mouseClicked(e);
}
});
this.getContentPane().add(jlFriendImNo,null);
this.getContentPane().add(jtfFriendImNo,null);
this.getContentPane().add(btnOK,null);
this.setSize(260,160);
this.setResizable(false);
}
}
void btnOK_mouseClicked(MouseEvent e)
{
try
{
friendImNo=Integer.parseInt(jtfFriendImNo.getText().trim());
String rtn=processAddFriend();
if(!rtn.equals("OK"))
JOptionPane.showMessageDialog(this,rtn,"提示信息",JOptionPane.INFORMATION_MESSAGE);
else
jtfFriendImNo.setText("");
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
public String processAddFriend()
{
Socket Socket;
BufferedReader in;
PrintWriter out;
String rtn="OK";
try
{
Socket=new Socket(InetAddress.getByName(mainWin.serverName),mainWin.serverPort);
in=new BufferedReader(new InputStreamReader(Socket.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(Socket.getOutputStream())),true);
try
{
out.println("ADDNEWFRIEND");
out.println(friendImNo);
out.println(mianWin.myImNo);
String
fNickName,fImNo,fFace="",fSex,fAge,fIp,fRPort,fOnline="",fEMail,fHomePage,fPhone,fResume;
do
{
fNickName=in.readLine();
if(fNickName.equals("OVER")||fNickName.equals("FALSE"))break;
fImNo=in.readLine();
fFace=in.readLine();
fSex=in.readLine();
fAge=in.readLine();
fIp=in.readLine();
fRPort=in.readLine();
fOnline=in.readLine();
fEMail=in.readLine();
fHomePage=in.readLine();
fPhone=in.readLine();
fResume=in.readLine();
mainWin.myFriends.add(new Friend(fNickName,fIp,fEMail,fHomePage,fPhone,fResume,Integer.parseInt(fImNo),
Integer.parseInt(fRPort),Integer.parseInt(fFace),Integer.parseInt(fSex),Integer.parseInt(fAge),Integer.parseInt(fOnline)));
}while(!fNickName.equals("OVER"));
out.print("END");
if(!fNickName.equals("FALSE"))
{
int fIndex=mainWin.myFriend.size()-1;
DefaultListModel dlm=(DefaultListModel)mainWin.jlstFriend.getModel();
if(Integer.parseInt(fOnline)==1)
dlm.addElement(new Object[]{((Friend)mainWin.myFriends.get(fIndex)).fNickName,new ImageIcon("face/"+mainWin.faceOffline[Integer.parseInt(fFace)-1])});
}
else
{
rtn=in.readLine();
}
}
catch(IOException e1)
{
rtn="加入好友时出错!";
}
finally
{
Socket.close();
return rtn;
}
}
catch(Exception ee)
{
rtn="初始化网络套接字出错!";
}
finally
{
return rtn;
}
}
}
class HeadPicCombobox extends DefaultComboBoxModel
{
public HeadPicCombobox(String[] pics){
for(int i=0;i<pics.length; ++i){
addElement(new Object[]{new ImageIcon("face/"+pics[i])});
}
}
public Icon getIcon(Object Object){
Object[] Array=(Object[])Object;
return(Icon)Array[0];
}
}
class HeadpicCellRenderer extends Jlabel implements ListCellRenderer
{
private Border
lineBorder=BorderFactory.createLineBorder(Color.red,2);
emptyBorder=BorderFactory.createEmptyBorder(2,2,2,2);
public HeadpicCellRender()
{
setOpaque(true);
}
public Component getListCellRendererComponent(JList List,
Object value,int index,boolean isSelected,boolean cellHasFocus)
{
HeadPicCombobox model=(HeadPicCombobox)List.getModel();
setIcon(model.getIcon(value));
if(isSelected){
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
}
else{
setForeground(list.getForeground());
setBackground(list.getBackground());
}
if(cellHasFocus)setBorder(lineBorder);
else setBorder(emptyBorder);
return this;
}
}
class NameAndPicListModel extends DefaultListModel
{
public NameAndPicListModel(Vector myFriends,String[] pics)
{
for(int i=0;i<myFriends.size();++i)
{
addElement(new Object[]{((Friend)myFriends.get(i)).fNickName,new ImageIcon("face/"+pics[i])});
}
}
public Srirng getName(Object Object)
{
Object[] Array=(Object[]) Object;
return(String)aray[0];
}
public Icon getIcon(Object Object)
{
Object[] Array=(Object[]) Object;
return(Icon)aray[1];
}
}
class NameAndPicListCellRenderer extends JLabel implements ListCellRender
{
private Border lineBorder=BorderFactory.createLineBorder(Color.red,2);
emptyBorder=BorderFactory.createEmptyBorder(2,2,2,2);
public NameAndPicListCellRenderer()
{
setOpaque(true);
}
public Component getListRendererComponent(JList List,Object value,
int index,boolean isSelected,boolean cellHasFocus)
{
NameAndPicListModel model=(NameAndPicListModel)List.getModel();
setText(model.getName(value));
setIcon(model.getIcon(value));
if(isSelected)
{
setForeground(List.getSelectionForeground());
setBackground(List.getSelectionBackground());
}
else
{
setForeground(List.getForeground());
setBackground(List.getBackground());
}
if(cellHasFocus)setBorder(lineBorder);
else setBorder(emptyBorder);
return this;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -