学海无涯苦作舟!
JOptionPane.INFORMATION_MESSAGE);
dispose();
}
else {
JOptionPane.showMessageDialog(this, \删除分组失败!\删除分组\
JOptionPane.INFORMATION_MESSAGE);
}}
catch(Exception ee) {
JOptionPane.showMessageDialog(this, \你的分组为空,无法删除!\删除分组\
JOptionPane.INFORMATION_MESSAGE); }} }
5.4 好友管理模块
5.4.1 添加好友
主要功能是添加已存在的用户为好友以便进行通讯,如图5-7所示。
图5-7 添加好友界面
主要实现代码如下:
public int Verify() { //验证用户是否存在
String username = jt.getText().trim(); UserItem ui = new UserItem();
ui.setUsername(username);
Protocal proVerifyUser = new Protocal(ProtocalNumber.VERIFYUSER2, \SendPacket sp = new SendPacket(proVerifyUser); int result = ((Integer) sp.Send()).intValue(); return result; }
学海无涯苦作舟!
public void actionPerformed(ActionEvent e) { //添加好友
if (e.getSource() == btnok) {
friend = (String) jt.getText().trim();
if (friend.equals(\
JOptionPane.showMessageDialog(this, \好友名称不能为空!\添加好友\
JOptionPane.INFORMATION_MESSAGE);
}
return;
if (friend.equals(CommonData.currUser.getUsername())) {
JOptionPane.showMessageDialog(this, \你不能加自己为好友!\添加好友\ JOptionPane.INFORMATION_MESSAGE); jt.setText(\return;
}
int verify = Verify(); if (verify == 0) {
JOptionPane.showMessageDialog(this, \你要添加的好友不存在!\添加好友\
JOptionPane.INFORMATION_MESSAGE);
jt.setText(\return;
}
else {
ui = new UserItem(); ui.setUsername(friend);
fi = new FriendItem(); fi.setFriend(ui);
String g_name = (String) choice.getSelectedItem(); fi.setGroup_in(g_name); fi.setOwner_friend(CommonData.currUser.getUsername());
Protocal proaddfriend = new Protocal(ProtocalNumber.ADDFRIEND,
CommonData.currUser.getUsername(), fi);
SendPacket sp = new SendPacket(proaddfriend); int result = ((Integer) sp.Send()).intValue();
学海无涯苦作舟!
if (result == 1) { JOptionPane.showMessageDialog(this, \此好友已添加过!\添加好友\
JOptionPane.INFORMATION_MESSAGE);
jt.setText(\return;
}
if (result == 0) { JOptionPane.showMessageDialog(this, \添加好友成功!\添加好友\
JOptionPane.INFORMATION_MESSAGE);
this.dispose(); }}}}
5.4.2 删除好友
主要功能是删除相应分组下的对应好友,如图5-8所示。
图5-8 删除好友界面
主要实现代码如下:
public void actionPerformed(ActionEvent e) { //删除好友
if (e.getSource() == btnok) { try {
group = choiceGroup.getSelectedItem().toString(); friend = choiceFriend.getSelectedItem().toString(); FriendItem fi = new FriendItem();
fi.setOwner_friend(CommonData.currUser.getUsername()); fi.setGroup_in(group); UserItem ui = new UserItem();
ui.setUsername(friend); fi.setFriend(ui);
学海无涯苦作舟!
Protocal proExit = new Protocal(ProtocalNumber.DELETEFRIEND,
CommonData.currUser.getUsername(), fi);
SendPacket sp = new SendPacket(proExit); int result = ((Integer) sp.Send()).intValue();
if (result != 0) { JOptionPane.showMessageDialog(this, \删除好友成功!\删除好友\
JOptionPane.INFORMATION_MESSAGE);
}}
dispose();
catch (Exception ee) {JOptionPane.showMessageDialog(this, \你的分组或好友为空,无法删除!\
删除好友\ JOptionPane.INFORMATION_MESSAGE); }}}
5.5 查询用户模块
主要功能是查询当前用户外的所有用户基本信息(包括是否在线),如图5-9所示。
图5-9 查询用户界面
主要实现代码为:
public Vector
ui=new UserItem();
ui.setUsername(CommonData.currUser.getUsername());
相关推荐: