📄 client.java
字号:
btnFriend = new JButton();
btnRecent = new JButton();
btnFind = new JButton("查找");
btnFind.setIcon(new ImageIcon("image\\find.png"));
pnlRecent =new JPanel();
pnlRecent.setLayout(null);
pnlRecent.setBounds(0, 120, 200, 320);
pnlRecent.setBackground(Color.WHITE);
pnlGroup =new JPanel();
pnlGroup.setLayout(null);
pnlGroup.setBounds(0, 100, 200, 320);
pnlGroup.setBackground(Color.WHITE);
button.setText("我的好友");
button.setBounds(0, 60, 200, 20);
getContentPane().add(button);
btnFriend.setText("MyQQ群");
btnFriend.setBounds(0, 400, 200, 20);
getContentPane().add(btnFriend);
btnRecent.setText("最近联系人");
btnRecent.setBounds(0, 420, 200, 20);
btnFind.setBounds(100, 445, 100, 20);
this.getContentPane().add(btnRecent);
this.getContentPane().add(btnFind);
//final JPanel panel = new JPanel();//好友信息列表
panel = new JPanel();//好友信息列表
panel.setLayout(null);
panel.setBackground(Color.WHITE);
try {
setFriendList(panel);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
scrollPane = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(0, 80, 200, 320);
getContentPane().add(scrollPane);
registerEvent();
pnlGroup.setVisible(false);
pnlRecent.setVisible(false);
}
//注册窗体的控件事件
private void registerEvent()
{
//控件的事件处理
btnRecent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!pnlRecent.isVisible())
{
switchTab(1);
switchTab(2);
setRecent();
pnlRecent.setVisible(true);
}
}
});
btnFind.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
FindFriend f = new FindFriend(Client.this,userNum);
f.setVisible(true);
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!scrollPane.isVisible())
{
scrollPane.setBounds(0, 80, 200, 320);
btnFriend.setBounds(0, 400, 200, 20);
btnRecent.setBounds(0, 420, 200, 20);
scrollPane.setVisible(true);
pnlGroup.setVisible(false);
pnlRecent.setVisible(false);
}
}
});
btnFriend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
pnlGroup.setVisible(true);
if(pnlRecent.isVisible())
{
pnlGroup.setBounds(0, 100, 200, 320);
btnRecent.setBounds(0, 420, 200, 20);
scrollPane.setVisible(false);
pnlRecent.setVisible(false);
}
else if(scrollPane.isVisible())
{
switchTab(1);
try {
Client.this.setGroup(pnlGroup); //调用绑定群列表的函数
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
Client.this.getContentPane().add(pnlGroup);//将群列表添加到窗口
}
}
});
}
//构造函数
public Client(int userNum) {
super("MyQQ");
this.userNum = userNum;
friend = new Vector();
group = new Vector();
sh = new SqlHelper();
sh.setStatement(sh.getConnection());
User.setOnLine(1, userNum);
addWindowListener(new WindowCloser());
getContentPane().setLayout(null);
this.getContentPane().setBackground(new Color(221,232,243));
setBounds(100, 100, 208, 500);
setup(); //控件初始化
ready(); //准备线程
rec = new recThread();
//
}
//用于生成黑白图片
public static void gray(String source, String result)
{
try
{
BufferedImage src = ImageIO.read(new File(source));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, "JPEG", new File(result));
}
catch (IOException e)
{
e.printStackTrace();
}
}
//关闭对话框触发的事件
public void closeChat(String key)
{
ht_friend.remove(key);
friendList.remove(key);
}
//为连接服务其做准备
public void closeChatGroup(String key)
{
ht_Group.remove(key);
}
public void ready()
{
try {
clientAddress = InetAddress.getLocalHost();
byte[] ip = clientAddress.getAddress();
strIP = (ip[0]&0xFF)+"."+(ip[1]&0xFF)+
"."+(ip[2]&0xFF)+"."+(ip[3]&0xFF);
User.setIP(strIP, this.userNum);
connection = new Socket(strIP,port);
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());
out.writeUTF("L "+userNum+"");
} catch (UnknownHostException e) {
// TODO 自动生成 catch 块
JOptionPane.showMessageDialog(Client.this, "未找到服务器!");
Client.this.clientClosed();
} catch (IOException e) {
// TODO 自动生成 catch 块
JOptionPane.showMessageDialog(Client.this, "未找到服务器!");
Client.this.clientClosed();
}
}
// 用于接收服务器消息的线程类
public void sendMessage(String string)
{
try {
out.writeUTF(string);
} catch (IOException e) {
// TODO 自动生成 catch 块
JOptionPane.showMessageDialog(Client.this, "未找到服务器!");
Client.this.clientClosed();
}
}
//由于更新窗体
public void updateClient()
{
try {
friend.removeAllElements();
panel.removeAll();
Client.this.setFriendList(panel);
}catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
panel.updateUI();
}
//用于接受服务器的信息
private class recThread extends Thread
{
public recThread()
{
start();
}
public void run()
{
try{
while(login){
String line = in.readUTF();
rightNow = Calendar.getInstance().getTime().toLocaleString();
if(line.charAt(0) == 'Q'){
if(line.length() == 1){
// 退出
connection.close();
login = false;
break;
}
else{
// 其他用户退出
//System.out.println("-------其他用户推出----------");
updateClient();
}
}
else if(line.charAt(0) == 'L'){
// 其他用户登录
//System.out.println("-------其他用户登录----------");
updateClient();
}
else if(line.charAt(0)=='G')
{//接受到群消息
System.out.println(line);
int groupID = new Integer(line.substring(1, line.indexOf("S"))).intValue();
ChatGroup temp = null;
temp = (ChatGroup)ht_Group.get(groupID+"");
if(temp!=null)
{
int sendUser = new Integer(line.substring(line.indexOf("S")+1, line.indexOf(" "))).intValue();
String sendUserName = User.getName(sendUser);
//格式化后在输出
line = " " + sendUserName + " "+ rightNow + "\n "+ line.substring(line.indexOf(" "));
temp.setMessage(line);
}
}
else if(line.charAt(0) == 'A'){
// 其他用户登录
//System.out.println("-------自己登录----------");
//System.out.println(line);
}
else if(line.charAt(0)== 'S')//接受到系统消息
{
Enumeration en = ht_friend.elements();
line = " " + line.substring(1,line.indexOf(" ")) + " "+ rightNow + "\n "+ line.substring(line.indexOf(" "));
while(en.hasMoreElements())
{
Chat s = (Chat)en.nextElement();
s.setMessage(line);
}
}
else{
//System.out.println(line.substring(0,line.indexOf(" ")));
//System.out.println(line);
String sendUser = line.substring(1,line.indexOf(" "));
String sendUserName = User.getName(new Integer(sendUser).intValue());
char top = line.charAt(0);
boolean open = true;
//用户接受好友发来的信息
Chat temp = null;
try
{
temp = (Chat)ht_friend.get(sendUser);
}
catch(Exception e)
{
open =false;
}
if(temp==null)
{
open =false;
}
if(open == false)
{
temp = new Chat(Client.this.userNum,new Integer(sendUser).intValue(),Client.this);
temp.setVisible(true);
ht_friend.put(sendUser, temp);
friendList.add(sendUser);
}
if(top=='M')
{
//格式化后在输出
line = " " + sendUserName + " "+ rightNow + "\n "+ line.substring(line.indexOf(" "));
temp.setVisible(true);
temp.setMessage(line);
}
else if(top=='F')
{
String fileName = line.substring(line.indexOf("N")+1,line.indexOf("!"));
temp.setVisible(true);
temp.setMessage(" "+rightNow+ "\n "+sendUserName+"向您发送文件:"+fileName+"\n");
if(0==JOptionPane.showConfirmDialog(null, "您是否接受该文件"+fileName, "消息对话框", JOptionPane.YES_NO_OPTION))
{
FileWriter fw = new FileWriter("files\\"+fileName);
fw.write(line.substring(line.indexOf("!")+1));
fw.close();
Client.this.sendMessage("M"+sendUser+" "+fileName+"传送完毕!");
}
else
{
Client.this.sendMessage("M"+sendUser+" "+"取消了接受该文件");
}
//System.out.println(line);
}
//用于添加到最近联系人
boolean isExist = false;
User user = null;
try{
user = (User)recent.get(sendUser);
}
catch(Exception e)
{
}
if(user==null)
{
isExist = true;
}
if(isExist)
{
int sendUserNum = new Integer(sendUser).intValue();
User sendPeople = User.getUser("select * from tbl_Users where userNum="+sendUserNum);
recent.put(sendUser, sendPeople);
}
}
}
}catch(SocketException se){
rightNow = Calendar.getInstance().getTime().toLocaleString();
login = false;
}catch (IOException ioe){
//output.append("Error: "+ioe+"\n\n");
}
}
}
private class clientMove extends MouseAdapter
{
boolean enter = true;
public void mouseEntered(MouseEvent e) {
// TODO 自动生成方法存根
final Rectangle rv = Client.this.getBounds();
if(!enter)
{
if(rv.y==-480)
{
final Timer t = new Timer("ss");
t.schedule(new TimerTask(){
int i = 0;
public void run() {
for(i=1;i<=10;i++)
{
int top = -480+48*i;
Client.this.setBounds(rv.x, top, rv.width, rv.height);
}
if(i>10)
{
t.cancel();
enter = true;
}
}
},500,100);
}
}
}
public void mouseExited(MouseEvent e) {
// TODO 自动生成方法存根
final Rectangle rv = Client.this.getBounds();
if(enter)
{
if(rv.y<=0)
{
final Timer t = new Timer("ss");
t.schedule(new TimerTask(){
int i = 0;
public void run() {
for(i=1;i<=10;i++)
{
int top = -48*i;
System.out.println(top);
Client.this.setBounds(rv.x, top, rv.width, rv.height);
}
if(i>10)
{
t.cancel();
enter = false;
}
}
},1000,100);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -