java 山寨QQ 实现(一)

By EvilBinary 小E

先看界面

//登录对话框代码

package org.evilbinary.client.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.evilbinary.client.QQClient;

public class LoginDialog extends JDialog {
	protected JTextField userName;
	protected JPasswordField userPwd;
	protected boolean isLogin;
	protected QQClient client;
	public LoginDialog() {
		createUI();
		isLogin = false;
		client=null;
		this.setModal(true);

	}

	private void createUI() {
		// this.setFont(MainFrame.font);
		this.setTitle("小E机器人");
		this.setIconImage(this.getToolkit().getImage("image/icon.png"));
		this.setResizable(false);
		this.setSize(340, 250);
		this.setLayout(null);
		JPanel topPanel = new JPanel();
		JPanel midPanel = new JPanel();
		JPanel bottomPanel = new JPanel();
		this.add(topPanel);
		this.add(midPanel);
		this.add(bottomPanel);

		topPanel.setBounds(0, 0, 340, 60);
		// topPanel.setBackground(new Color(245, 237, 253));
		midPanel.setBounds(0, 60, 340, 120);
		// midPanel.setBackground(new Color(100,137,153));
		bottomPanel.setBounds(0, 180, 340, 30);
		// top
		JLabel logo = new JLabel(new ImageIcon("image/logo.gif"));
		logo.setBounds(0, 0, 340, 60);
		this.add(logo);
		// mid
		midPanel.setLayout(new FlowLayout(1, 10, 13));
		userName = new JTextField(13);
		userPwd = new JPasswordField(13);
		userName.setText("10394709");
		userPwd.setText("xxxxxxxxxx");
		userName.setSize(200, 100);
		userName.setBounds(0, 0, 120, 130);
		userPwd.setSize(100, 30);
		JLabel register = new JLabel("申请账号  ");
		register.setCursor(new Cursor(Cursor.HAND_CURSOR));
		register.setForeground(Color.blue);
		JLabel forget = new JLabel("忘了密码?");
		forget.setCursor(new Cursor(Cursor.HAND_CURSOR));
		forget.setForeground(Color.blue);
		midPanel.add(new JLabel("帐号:"));
		midPanel.add(userName);
		midPanel.add(register);
		midPanel.add(new JLabel("密码:"));
		midPanel.add(userPwd);
		midPanel.add(forget);

		JComboBox status = new JComboBox();
		status.addItem("在线");
		status.addItem("Q我");
		status.addItem("离开");
		status.addItem("忙碌");
		status.addItem("勿扰");
		status.addItem("隐身");
		JCheckBox remember = new JCheckBox("记住密码");
		JCheckBox auto = new JCheckBox("自动登录");
		midPanel.add(status);
		midPanel.add(remember);
		midPanel.add(auto);

		// bottom
		bottomPanel.setLayout(null);
		JButton setBtn = new JButton("设置");
		setBtn.setBounds(15, 8, 70, 22);
		JButton loginBtn = new JButton("登录");
		loginBtn.setBounds(250, 8, 70, 22);
		bottomPanel.add(setBtn);
		bottomPanel.add(loginBtn);
		loginBtn.addActionListener(new LoginActionListerner());
		JLabel bottom = new JLabel();
		bottom.setForeground(new Color(191, 225, 249));
		bottom.setBounds(0, 190, 340, 60);
		this.add(bottom);

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setAlwaysOnTop(true);
	}

	public class LoginActionListerner implements ActionListener {

		public void actionPerformed(ActionEvent event) {
			String userName=LoginDialog.this.userName.getText().trim();
			String passWord=new String(LoginDialog.this.userPwd.getPassword());
//			System.out.println(userName);
//			System.out.println(passWord);
			LoginDialog.this.isLogin=true;
			if(userName.equals("")){ 
				JOptionPane.showMessageDialog(LoginDialog.this, "请您输入账户再登录","Error",0);
			}else{
				if(passWord.equals("")){
					JOptionPane.showMessageDialog(LoginDialog.this, "请输入密码再登录","Error",0);
				}else{
					client=new QQClient(userName,passWord,"?");
					if(client.login()){	
						LoginDialog.this.setVisible(false);
					}else{
						LoginDialog.this.setVisible(true);
					}
				}
			}
			
		}
	}

	public void showDialog() {
		this.setVisible(true);
	}
}

//皮肤是用 com.seaglasslookandfeel   http://code.google.com/p/seaglass/ 这里下载

//皮肤使用方法代码如下

	try {
				UIManager.setLookAndFeel(new SeaGlassLookAndFeel());
				// UIManager.getLookAndFeelDefaults().put("defaultFont", new
				// Font("Microsoft Yahei", Font.PLAIN, 12));
				MainFrame frame = new MainFrame();
			} catch (UnsupportedLookAndFeelException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}



1、具有登陆界面,包括服务器IP(或服务器名)、用户名、密码。 登录:在数据库服务器上验证用户名和密码,如果匹配就进入QQ聊天主界面,否则提示不能登录。 取消:退出即可。 注册:进入用户注册界面。 服务器栏中默认是你要使用的服务器名字,即该服务器安装的数据库,所有qq用户信息都保存在这个服务器的数据库里。图片显示的是我试验的服务器名字。 登录成功后还要把自己当前所在机器的IP地址提取出来并保存到数据库的个人信息中,替换原来注册或上次使用的IP。 2、注册界面,可以让第一次使用的用户注册,即给自己在服务器上注册一个用户名、密码、自己所使用的机器IP、自己的端口号,如图所示: 注册:首先验证密码和确认密码是否一致,如果不一致,则提示,否则,把输入的信息记录到数据库中,如果用户名重复也要提示。 其中IP栏目的内容可以由程序自动提取出本机的ip地址,并显示出来,省得用户输入错误。 3、聊天主界面 登陆成功后就进入本届面,界面如下: 界面设计类似简单的QQ界面,可以使用BorderLayout布局管理器,在上面(BorderLayou.NORTH)显示“好友列表”四个字,中间部分(BorderLayou.CENTER)显示自己的好友名单,名单可以用多个JLabel控件(JLabel中添加ImageIcon)显示,或者用JTree控件显示(比较复杂),名单中可以显示qq号、昵称(姓名)、状态(在线、离线等)。最下边(BorderLayou.SOUTH)可以显示“欢迎您:***”。 好友要求用不同的图标显示在线和不在线的状态。 双击在线的好友名字可以打开聊天窗口,而双击不在线的好友不动(即不开启聊天窗口)。 达到这种效果的前提是在登录成功后,获得当前登录用户的信息,在本模块根据登录信息获取用户名、获取他的好友列表等数据。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值