📄 mainface.java
字号:
package qq;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JList;
import javax.swing.JScrollPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.util.Enumeration;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.util.Hashtable;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
public class MainFace extends JFrame implements Runnable {
Hashtable chatWindows = new Hashtable();
public static Hashtable localUserchatWindows = new Hashtable(); //key为本地用户id,value为chatWindows
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel2 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel l1 = new JLabel(); //显示用户id
JLabel l2 = new JLabel(); //显示用户昵称
JLabel l3 = new JLabel(); //显示用户ip
JButton add = new JButton();
/////////////处理jList控件/////////////////////
DefaultListModel jListModel = new DefaultListModel();
JList jList1 = new JList(jListModel);
JScrollPane jScrollPane1 = new JScrollPane(jList1);
//////////////////////////////////////////////
private int id = 0;
JTextField addText = new JTextField();
JButton updateUserInfo = new JButton();
JLabel jLabel1 = new JLabel();
JPopupMenu jPopupMenu1 = new JPopupMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JButton delete = new JButton();
public MainFace() {
super("QQ 1.0");
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
jPanel1.setLayout(xYLayout1);
jLabel2.setFont(new java.awt.Font("黑体", Font.PLAIN, 16));
jLabel2.setText(" IP");
jLabel4.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 16));
jLabel4.setText("帐号");
jLabel7.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 16));
jLabel7.setText("昵称");
l1.setBackground(Color.white);
l1.setText("");
l2.setBackground(Color.white);
l2.setText("");
l3.setBackground(Color.white);
l3.setText("");
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
}); add.setToolTipText("添加好友");
add.setSelectedIcon(null);
add.setText("添加");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add_actionPerformed(e);
}
});
jPanel1.setBorder(BorderFactory.createTitledBorder("我的信息"));
xYLayout1.setWidth(255);
xYLayout1.setHeight(560);
updateUserInfo.setToolTipText("修改用户个人信息");
updateUserInfo.setText("修改");
updateUserInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateUserInfo_actionPerformed(e);
}
});
jList1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
jList1_mousePressed(e);
}
});
jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 13));
jLabel1.setText("添加删除好友");
addText.setToolTipText("输入ID查找用户,点击添加加为好友,点击删除删除好友");
jMenuItem1.setText("开始聊天");
jMenuItem2.setText("删除好友");
delete.setToolTipText("删除好友");
delete.setText("删除");
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
delete_actionPerformed(e);
}
});
jPanel1.add(l2, new XYConstraints(57, 37, 116, 25));
jPanel1.add(jLabel4, new XYConstraints(8, 1, 35, 25));
jPanel1.add(jLabel7, new XYConstraints(8, 36, 35, 25));
jPanel1.add(jLabel2, new XYConstraints(8, 72, 35, 25));
jPanel1.add(l1, new XYConstraints(57, 1, 116, 25));
jPanel1.add(l3, new XYConstraints(57, 72, 116, 25));
this.getContentPane().add(addText, new XYConstraints(108, 144, 106, 24));
this.getContentPane().add(jLabel1, new XYConstraints(22, 144, -1, 24));
jScrollPane1.setBorder(BorderFactory.createTitledBorder("我的好友"));
this.getContentPane().add(jPanel1, new XYConstraints(19, 8, 200, 129));
this.getContentPane().add(jScrollPane1,
new XYConstraints(19, 196, 200, 337));
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
this.getContentPane().add(add, new XYConstraints(19, 171, 71, 21));
this.getContentPane().add(delete, new XYConstraints(87, 171, 71, 21));
this.getContentPane().add(updateUserInfo,
new XYConstraints(153, 171, 71, 21));
this.setSize(248, 580);
this.setLocation(700, 30);
this.setVisible(true);
}
//mainFace方法
public void mainFace(int id) {
this.id = id;
//显示登陆用户的本人信息
l1.setText(String.valueOf(id));
l2.setText(((UserInfo) Client.localUser.get(id)).getUserName());
l3.setText(((UserInfo) Client.localUser.get(id)).getUserIp());
localUserchatWindows.put(id, chatWindows);
//获取用户好友列表
Client client = new Client();
UserInfo user = new UserInfo();
user = (UserInfo) Client.localUser.get(id);
user.setInfoType(5);
client.send(user);
client.userFriendList = (Hashtable) client.get(user.getUserPort());
Client.friendList.put(id, client.userFriendList);
Enumeration en = client.userFriendList.elements();
while (en.hasMoreElements()) {
UserInfo u = new UserInfo();
u = (UserInfo) en.nextElement();
String str = u.getUserName() + " " + String.valueOf(u.getUserId());
jListModel.addElement(str);
}
//单击弹出聊天窗口
jList1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList1_valueChanged(e);
}
});
}
//关闭窗口,向服务器发送改变在线状态的请求,退出登陆
public void this_windowClosing(WindowEvent e) {
int isok = JOptionPane.showConfirmDialog(this, "您确定要退出吗?", "消息提示",
JOptionPane.OK_CANCEL_OPTION);
System.out.println(isok);
if (isok == 0) {
((UserInfo) Client.localUser.get(id)).setInfoType(4); //设置类型为退出请求
Client client = new Client();
client.send(Client.localUser.get(id)); //向服务器提交退出请求
localUserchatWindows.remove(id); //从本地用户聊天窗口中移除该用户打开的聊天窗口
Client.friendList.remove(id); //从本地用户好友列表中移除该用户的好友列表
Client.localUser.remove(id); //从本地用户中移除该用户
System.exit(0);
}
}
//监听来自服务器端的信息
public void run() {
//try {
while (true) {
Client client = new Client();
Object obj = client.get(((UserInfo) Client.localUser.get(id)).
getUserPort());
if (obj instanceof Message) {
//获得服务器返回的聊天信息
Message msg = (Message) obj;
if (((Hashtable) MainFace.localUserchatWindows.get(id)).
containsKey(msg.getFromId())) {
((ChatFace) ((Hashtable) MainFace.localUserchatWindows.get(
id)).get(msg.getFromId())).receive.append("\n" +
msg.toString());
} else {
ChatFace chatface = new ChatFace((UserInfo) Client.
localUser.get(id), (UserInfo) ((Hashtable) Client.
friendList.get(id)).get(msg.getFromId()));
chatface.setTitle("与 " + msg.getFromName() + " 对话中");
chatWindows.put(msg.getFromId(), chatface);
localUserchatWindows.remove(id);
localUserchatWindows.put(id, chatWindows);
((ChatFace) ((Hashtable) MainFace.localUserchatWindows.get(
id)).get(msg.getFromId())).receive.append("\n" +
msg.toString());
}
} else if (obj instanceof UserInfo) {
UserInfo userInfo = (UserInfo) obj;
if (userInfo.getInfoType() == 3) { //获得服务器返回的用户更新状态
if (userInfo.getOnLine()) {
l2.setText(userInfo.getUserName());
JOptionPane.showMessageDialog(null, "修改用户信息成功!");
} else {
JOptionPane.showMessageDialog(null, "修改用户信息发生错误!");
}
} else if (userInfo.getInfoType() == 6) { //获得服务器返回的添加的好友信息
if (userInfo.getOnLine()) {
((Hashtable) Client.friendList.get(id)).put(userInfo.
getUserId(), userInfo);
String str = userInfo.getUserName() + " " +
String.valueOf(userInfo.getUserId());
JOptionPane.showMessageDialog(null, "添加好友成功!");
addText.setText("");
jListModel.addElement(str);
} else {
JOptionPane.showMessageDialog(null, "该用户不存在,请核实用户ID!");
addText.setText("");
}
} else if (userInfo.getInfoType() == 7) { //获得服务器返回的删除好友信息
if (userInfo.getOnLine()) {
((Hashtable) Client.friendList.get(id)).remove(userInfo.
getUserId());
String str = userInfo.getUserName() + " " +
String.valueOf(userInfo.getUserId());
JOptionPane.showMessageDialog(null, "删除好友成功!");
addText.setText("");
jListModel.removeElement(str);
} else {
JOptionPane.showMessageDialog(null, "该用户还不是你的好友,无需删除!");
addText.setText("");
}
}
}
}
}
//单击弹出聊天窗口
public void jList1_valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
//int[] index = jList1.getSelectedIndices();获得索引
//String temp[] = string[index[0]].split(" ", 2);索引为0的为选中的第一个元素
String temp[] = ((String) jList1.getSelectedValue()).split(" ",
2);
String name = temp[0];
int idNumber = Integer.parseInt(temp[1]);
ChatFace chatface = new ChatFace((UserInfo) Client.localUser.
get(
id), (UserInfo) ((Hashtable) Client.friendList.get(id)).
get(idNumber));
chatface.setTitle("与 " + name + " 对话中");
chatWindows.put(idNumber, chatface);
localUserchatWindows.remove(id);
localUserchatWindows.put(id, chatWindows);
}
}
//添加好友事件
public void add_actionPerformed(ActionEvent e) {
if(!addText.getText().equals("")){
if(addText.getText().equals(l1.getText())){
JOptionPane.showMessageDialog(null, "无法添加自己为好友!");
addText.setText("");
}else{
String str = "A;" + id + ";" + addText.getText().toString().trim();
Client client = new Client();
int isok = JOptionPane.
showConfirmDialog(this,
"您确定要添加" + addText.getText() + "为好友?",
"消息提示", JOptionPane.OK_CANCEL_OPTION);
if (isok == 0) {
client.send(str);
} else {
addText.setText("");
}
}
}else{
JOptionPane.showMessageDialog(null, "请输入需要添加为好友的用户ID!");
}
}
//删除好友事件
public void delete_actionPerformed(ActionEvent e) {
if(!addText.getText().equals("")){
String str = "D;" + id + ";" + addText.getText().toString().trim();
Client client = new Client();
int isok = JOptionPane.
showConfirmDialog(this,
"您确定要删除" + addText.getText() +
"的好友资格?",
"消息提示", JOptionPane.OK_CANCEL_OPTION);
if (isok == 0) {
// jListModel
client.send(str);
} else {
addText.setText("");
}
}else{
JOptionPane.showMessageDialog(null, "请输入需要删除好友的用户ID!");
}
}
//更新用户信息事件
public void updateUserInfo_actionPerformed(ActionEvent e) {
UpdateUserInfoFace updateUserInfo = new UpdateUserInfoFace(id);
}
//鼠标右击菜单事件
public void jList1_mousePressed(MouseEvent e) {
jPopupMenu1.show(jList1, e.getX(), e.getY());
int index = jList1.locationToIndex(e.getPoint());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -