package com.zuxia.qq.view;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.*;
import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.*;
import com.zuxia.qq.model.UserModel;
import com.zuxia.qq.po.Message;
import com.zuxia.qq.po.User;
import com.zuxia.qq.view.FriendPanel;
import com.zuxia.qq.view.MyComboQQ;
import com.zuxia.qq.view.MyPanel;
import com.zuxia.qq.view.Regist;
import res.*;;
public class Login extends JFrame {
/**
* 登录
*
* @param args
*/
JLabel lb1 = new JLabel("帐号:");
JTextField jtx1 = new JTextField();
JComboBox jbox = new JComboBox();
List list = new ArrayList();
JLabel lb2 = new JLabel("注册 ");
JLabel lb3 = new JLabel("密码:");
JPasswordField jpas = new JPasswordField();
JLabel lb4 = new JLabel("忘记密码");
JButton jbt1 = new JButton("登录");
JButton jbt2 = new JButton("取消");
Socket socket;
ObjectOutputStream oos;
ObjectInputStream ois;
Properties pro =new Properties();
public Login() {
this.setTitle("山寨QQ");
this.setSize(350, 250);
this.setDefaultCloseOperation(Login.EXIT_ON_CLOSE);
// 居中
this.setLocationRelativeTo(null);
// 不可变大小
this.setResizable(false);
// 面板分割三部分,上 中 下
JPanel jp_up = new MyPanel();// Color.BLACK);
JPanel jp_center = new MyPanel();// Color.RED);
JPanel jp_down = new MyPanel(Color.cyan);
this.add(jp_up, BorderLayout.NORTH);
this.add(jp_center, BorderLayout.CENTER);
this.add(jp_down, BorderLayout.SOUTH);
// 上部分图片
JLabel lb = new JLabel();
lb.setIcon(new ImageIcon(res.Img.class.getResource("qq.JPG")));
jp_up.add(lb);
// 中间分两块
JPanel jp_center_up = new MyPanel();// Color.blue);
JPanel jP_center_down = new MyPanel();// Color.GREEN);
jp_center.setLayout(new BorderLayout());
jp_center.add(jp_center_up, BorderLayout.NORTH);
jp_center.add(jP_center_down, BorderLayout.CENTER);
// 在中间上部分添加组件
jp_center_up.add(lb1);
// try {
// pro=new Properties();
// pro.load(new FileInputStream(MyRes.class.getResource("user.txt").getPath()));
// } catch (FileNotFoundException e3) {
// e3.printStackTrace();
// } catch (IOException e3) {
// e3.printStackTrace();
// }
// while(pro.keySet().iterator().hasNext()){
// String key=(String) pro.keySet().iterator().next();
// String s=pro.getProperty(key);
// list.add(s);
// }
// list.add("11111");
// list.add("22222");
list=this.getQq();
// List list2=new ArrayList();
// list2=getQq();
// list2.add(1);
// 数据模型
MyComboQQ model = new MyComboQQ(list,"请输入账号");
jbox.setModel(model);
// model.getSelectedItem();
// JComboBox可输入
jbox.setEditable(true);
jbox.setEnabled(true);
// jbox.setEditor(null);
jbox.setPreferredSize(new Dimension(175, 25));
jp_center_up.add(jbox);
jp_center_up.add(lb2);
// 注册Label添加鼠标监听器,打开注册面板
lb2.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
new Regist().setVisible(true);
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
// 在中间下部分添加组件
jP_center_down.add(lb3);
jpas.setPreferredSize(new Dimension(175, 25));
jP_center_down.add(jpas);
jP_center_down.add(lb4);
// 在下部分添加按钮
jp_down.setLayout(new BorderLayout());
jp_down.add(jbt1, BorderLayout.EAST);
jp_down.add(jbt2, BorderLayout.WEST);
//初始化网络连接
initConnection();
jbt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Message mes=new Message("login");
User user=new User();
user.setQq((String) jbox.getSelectedItem());
user.setPwd(jpas.getText());
mes.setUser(user);
//向服务器发送信息
try {
oos.writeObject(mes);
oos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//接受服务器返回的消息
Message mesReturn = null;
try {
mesReturn = (Message) ois.readObject();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
// Properties pro = new Properties();
// // 取出Combox里的数据
// String username = (String) jbox.getSelectedItem();
// String password = jpas.getText();
// try {
// pro.load(new FileInputStream(Regist.file));
// } catch (IOException exception) {
// exception.printStackTrace();
// }
// System.out.println(username);
// System.out.println(password);
if (mesReturn.getMes().equals("ok")) {
// pro=new Properties();
try {
pro.load(new FileInputStream(MyRes.class.getResource("user.txt").getPath()));
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
pro.setProperty(jbox.getSelectedItem()+".qq", (String) jbox.getSelectedItem());
try {
pro.store(new FileOutputStream(MyRes.class.getResource("user.txt").getPath()), null);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
List<User> list1=null;
String iconPath=mesReturn.getUser().getIconPath();
// String iconPath=MyRes.class.getResource("1.JPG").getPath();
list1=mesReturn.getList();
// 好友列表对象
FriendPanel window = new FriendPanel(iconPath,list1,user);
// 业务逻辑
// UserModel model = new UserModel();
// // 获得好友的集合
// List list = model.getMyfriend();
// window.addFriends(list1);
window.addIcon(iconPath);
window.setVisible(true);
// 关闭登陆窗口
Login.this.dispose();
} else {
JOptionPane.showMessageDialog(Login.this, "用户密码错误");
}
}
});
}
public static void main(String[] args) {
new Login().setVisible(true);
}
public void initConnection(){
try{
socket=new Socket("127.0.0.1",10000);
oos=new ObjectOutputStream(socket.getOutputStream());
ois=new ObjectInputStream(socket.getInputStream());
}catch(Exception e){
e.printStackTrace();
}
}
public List getQq(){
List list1=new ArrayList();
try {
// pro=new Properties();
pro.load(new FileInputStream(MyRes.class.getResource("user.txt").getPath()));
} catch (FileNotFoundException e3) {
e3.printStackTrace();
} catch (IOException e3) {
e3.printStackTrace();
}
Iterator it = pro.keySet().iterator();
while(it.hasNext()){
String key=(String) it.next();
String s=pro.getProperty(key);
list1.add(s);
System.out.println("12312");
}
return list1;
}
}