?? mainwin.java
字號:
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {//關閉程序
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
//tell who add me as friend offline
try{
String whoips;
String s="offline"+myjicq;
s.trim();
System.out.println(s);
byte[] data=s.getBytes();
for(int i=0;i<whoaddmesip.size();i++){
whoips=whoaddmesip.get(i).toString().trim();
sendPacket=new
DatagramPacket(data,s.length(),InetAddress.getByName(whoips),sendPort);
sendSocket.send(sendPacket);//通知好友我下線了
}//for
} catch(IOException e2){sendtext.append(sendtext.getText());
e2.printStackTrace();}
//end offline
//告訴服務器我下線了
out.println("logout");
out.println(myjicq);
//socket.close();
System.exit(0);
}
}
void this_mousePressed(MouseEvent e) {
jButton1.setIcon(icon1);
}
void list_mouseClicked(MouseEvent e) {
jPopupMenu1.show(this,e.getX()+20,e.getY()+20);
}
void direct_mouseClicked(MouseEvent e) {//直接添加好友
DirectAdd.setLocationRelativeTo(MainWin.this);
DirectAdd.setSize(260,160);
DirectAdd.show();
// JOptionPane.showMessageDialog(this,":-(對不起,還有好多沒做出來.hg","ok",JOptionPane.INFORMATION_MESSAGE);
}
void ok_mouseClicked(MouseEvent e) {
about.dispose();
}
void cancel_mouseClicked(MouseEvent e) {
senddata.dispose();
}
void sendmessage_mousePressed(MouseEvent e) {//發消息菜單
senddata.setLocationRelativeTo(MainWin.this);
senddata.setBounds(e.getX()+50,e.getY()+50,400,280);
index=list.getSelectedIndex();
System.out.println(index);
nametext.setText(friendnames.get(index).toString());
icqno.setText(friendjicq.get(index).toString());
theip=friendips.get(index).toString();//ip address
System.out.println(theip);
senddata.show();
}
void find_mouseClicked(MouseEvent e) {//顯示查找好友窗口
findf.show();
}//find
void send_mouseClicked(MouseEvent e) {//發送消息
//*********send message
try{
String s=sendtext.getText().trim();
// System.out.println(s);
byte[] data=s.getBytes();
System.out.println(theip);
theip.trim();
if (theip.equals("null")||theip.equals(" ")||theip.equals("0")){JOptionPane.showMessageDialog(this,":-(對不起,不在線","ok",JOptionPane.INFORMATION_MESSAGE);
}
else{
sendPacket=new
DatagramPacket(data,s.length(),InetAddress.getByName(theip),sendPort);
sendSocket.send(sendPacket);}
} catch(IOException e2){sendtext.append(sendtext.getText());
e2.printStackTrace();}
senddata.dispose();
//*******end send message
}
void getmessage_mousePressed(MouseEvent e) {//接受消息菜單
String message=received.trim();
index=list.getSelectedIndex();
if(index==index4)getinfo.append(message);
else getinfo.append(" ");
getfromname.setText(friendnames.get(index).toString().trim());
getfromjicq.setText(friendjicq.get(index).toString().trim());
getdata.show();
}
void getok_mouseClicked(MouseEvent e) {//接受消息
getinfo.setText(" ");
getdata.dispose();
received=" ";
}
//update friend info;
void update_mouseClicked(MouseEvent e) {//更新好友列表
tempname=findf.tmpname;
tempjicq=findf.tmpjicq;
tempip=findf.tmpip;
temppic=findf.tmppic;
tempstatus=findf.tmpstatus;
tempemail=findf.tmpemail;
tempinfo=findf.tmpinfo;
DefaultListModel mm2=(DefaultListModel)list.getModel();
int picid=0;
for(int p=0;p<tempname.size();p++){
picid=Integer.parseInt(temppic.get(p).toString());
if(status.get(p).equals("1")){
mm2.addElement(new Object[]{tempname.get(p),new ImageIcon(picsonline[picid])});}
else {
mm2.addElement(new Object[]{tempname.get(p),new ImageIcon(picsonline[picid])});}
//picid=Integer.parseInt(temppic.get(p).toString());
//mm2.addElement(new Object[]{tempname.get(p),new ImageIcon(picsonline[picid])});
}//for
//add to friendlist
for(int k=0;k<tempname.size();k++){
friendnames.add(tempname.get(k));
friendjicq.add(tempjicq.get(k));
friendips.add(tempip.get(k));
picno.add(temppic.get(k));
status.add(tempstatus.get(k));
friendemail.add(tempemail.get(k));
friendinfo.add(tempinfo.get(k));
}//for
//clean tmp
for(int p=0;p<tempname.size();p++){
findf.tmpip.removeAllElements();
findf.tmpjicq.removeAllElements();
findf.tmpname.removeAllElements();
findf.tmppic.removeAllElements();
findf.tmpstatus.removeAllElements();
findf.tmpemail.removeAllElements();
findf.tmpinfo.removeAllElements();
}
}
//delete freind
void delfriend_mousePressed(MouseEvent e) {//刪除好友
out.println("delfriend");
int index2;
index2=list.getSelectedIndex();
out.println(friendjicq.get(index2));//the friendjicq to del
out.println(myjicq);//my jicqno
DefaultListModel mm=(DefaultListModel)list.getModel();
mm.removeElementAt(index2);
friendnames.removeElementAt(index2);
friendips.removeElementAt(index2);
friendjicq.removeElementAt(index2);
picno.removeElementAt(index2);
status.removeElementAt(index2);
friendemail.removeElementAt(index2);
friendinfo.removeElementAt(index2);
}//////////////delfriend
//tell friend i am online
void online_mouseClicked(MouseEvent e) {
out.println("getwhoaddme");
out.println(myjicq);
String whoip=" ";
do{
try{whoip=in.readLine().trim();
if(whoip.equals("over")) break;
whoaddmesip.add(whoip);
}catch(IOException s){System.out.println("false getwhoaddme");}
}while(!whoip.equals("over"));
for(int i=0;i<whoaddmesip.size();i++)
{System.out.println(whoaddmesip.get(i));
}
try{
String whoips;
String s="online"+myjicq;
s.trim();
System.out.println(s);
byte[] data=s.getBytes();
for(int i=0;i<whoaddmesip.size();i++){
whoips=whoaddmesip.get(i).toString().trim();
sendPacket=new
DatagramPacket(data,s.length(),InetAddress.getByName(whoips),sendPort);
sendSocket.send(sendPacket);
}//for
} catch(IOException e2){sendtext.append(sendtext.getText());
e2.printStackTrace();System.exit(1);}
}/////end tellfrienonline
void myinfo_mouseClicked(MouseEvent e) {//陌生人消息
if(fromunknow){
String message=received.trim();
getinfo.append(message);
getdata.show();
}
}
void lookinfo_mousePressed(MouseEvent e) {//查看好友資料菜單
hello.setLocationRelativeTo(MainWin.this);
hello.setBounds(e.getX()+50,e.getY()+50,380,300);
index=list.getSelectedIndex();
helloname.setText(friendnames.get(index).toString());
hellojicq.setText(friendjicq.get(index).toString());
helloemail.setText(friendemail.get(index).toString());
helloinfo.setText(friendinfo.get(index).toString().trim());
hello.show();
}
void hellook_mouseClicked(MouseEvent e) {//查看好友資料關閉
hello.dispose();
}
//add the one who add me as friend
void addit_mouseClicked(MouseEvent e) {
//如果有人加我,我就加它
out.println("addnewfriend");
out.println(tempgetjicq);
out.println(myjicq);
String thename=" ";
try{
String thejicqno,theip,thestatus,picinfo,email,infos;
do{thename=in.readLine();
if(thename.equals("over")) break;
friendnames.add(thename);
thejicqno=in.readLine();
friendjicq.add(new Integer(thejicqno));
theip=in.readLine();
friendips.add(theip);
thestatus=in.readLine();
status.add(thestatus);
picinfo=in.readLine();
picno.add(new Integer(picinfo));
email=in.readLine();
friendemail.add(email);
infos=in.readLine();
friendinfo.add(infos);
}while(!thename.equals("over"));
}catch(IOException e1){System.out.println("false");}
int dddd=friendnames.size()-1;
DefaultListModel mm2=(DefaultListModel)list.getModel();
int picid;
picid=Integer.parseInt(picno.get(dddd).toString());
mm2.addElement(new Object[]{friendnames.get(dddd),new ImageIcon(picsonline[picid])});
}
void iknow_mouseClicked(MouseEvent e) {
OneAddyou.dispose();
}
void directaddok_mouseClicked(MouseEvent e) {//直接添加好友
out.println("addnewfriend");
out.println(friendid.getText().trim());
out.println(myjicq);
String thename=" ";
try{
String thejicqno,theip,thestatus,picinfo,email,infos;
do{thename=in.readLine();
if(thename.equals("over")) break;
friendnames.add(thename);
thejicqno=in.readLine();
friendjicq.add(new Integer(thejicqno));
theip=in.readLine();
friendips.add(theip);
thestatus=in.readLine();
status.add(thestatus);
picinfo=in.readLine();
picno.add(new Integer(picinfo));
email=in.readLine();
friendemail.add(email);
infos=in.readLine();
friendinfo.add(infos);
}while(!thename.equals("over"));
}catch(IOException e1){System.out.println("false");}
int dddd=friendnames.size()-1;
DefaultListModel mm2=(DefaultListModel)list.getModel();
int picid;
picid=Integer.parseInt(picno.get(dddd).toString());
mm2.addElement(new Object[]{friendnames.get(dddd),new ImageIcon(picsonline[picid])});
DirectAdd.dispose();
};//end directadd friend
}//end class MainWin
//以下繼承DefaultListModel類以創建好友列表
class NameAndPicListModel extends DefaultListModel
{ public NameAndPicListModel(Vector friendnames,String [] pics)
{ for(int i=0;i<friendnames.size();++i){
addElement(new Object[]{friendnames.get(i),new ImageIcon(pics[i])});
}
}
public String getName(Object object)
{ Object[] array=(Object[])object;
return(String) array[0];
}
public Icon getIcon(Object object)
{ Object[] array=(Object[]) object;
return (Icon) array[1];
}
}
class NameAndPicListCellRenderer extends JLabel implements ListCellRenderer
{ private Border lineBorder=BorderFactory.createLineBorder(Color.red,2),
emptyBorder =BorderFactory.createEmptyBorder(2,2,2,2);
public NameAndPicListCellRenderer(){
setOpaque(true);
}
public Component getListCellRendererComponent(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;
}
}
class MainWin_list_mouseAdapter extends java.awt.event.MouseAdapter {
MainWin adaptee;
MainWin_list_mouseAdapter(MainWin adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.list_mouseClicked(e);
}
}
class MainWin_direct_mouseAdapter extends java.awt.event.MouseAdapter {
MainWin adaptee;
MainWin_direct_mouseAdapter(MainWin adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.direct_mouseClicked(e);
}
}
class MainWin_ok_mouseAdapter extends java.awt.event.MouseAdapter {
MainWin adaptee;
MainWin_ok_mouseAdapter(MainWin adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.ok_mouseClicked(e);
}
}
class MainWin_sendmessage_mouseAdapter extends java.awt.event.MouseAdapter {
MainWin adaptee;
MainWin_sendmessage_mouseAdapter(MainWin adaptee) {
this.adaptee = adaptee;
}
public void mousePressed(MouseEvent e) {
adaptee.sendmessage_mousePressed(e);
}
}
class MainWin_cancel_mouseAdapter extends java.awt.event.MouseAdapter {
MainWin adaptee;
MainWin_cancel_mouseAdapter(MainWin adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.cancel_mouseClicked(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -