码迷,mamicode.com
首页 > 其他好文 > 详细

山寨版QQ

时间:2014-05-08 00:10:47      阅读:617      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

登录界面:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CopyQQ extends JFrame{
	Container c;
	JPanel p;
	JLabel lab=new JLabel("欢迎使用javaQQ");
	JLabel labserver=new JLabel("服务器:");
	JLabel labuser=new JLabel("用户名:");
	JLabel labpassword=new JLabel("密码:");
	JTextField tetserver=new JTextField(20);
	JTextField tetuser=new JTextField(10);
	JTextField tetpassword=new JTextField(15);
	JButton btnlog=new JButton("登录");
	JButton btncancel=new JButton("取消");
	JButton btnreg=new JButton("注册");
	
	void init(){
		c=this.getContentPane();
		c.setLayout(new BorderLayout());
		p=new JPanel();p.setLayout(null);
		p.add(lab);
		p.add(labserver);
		p.add(labuser);
		p.add(labpassword);
		p.add(tetserver);
		p.add(tetuser);
		p.add(tetpassword);
		p.add(btnlog);
		p.add(btncancel);
		p.add(btnreg);
		btnreg.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Register reg=new Register();
				reg.setVisible(true);
			}
		});
		btnlog.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String tmpserver=tetserver.getText();
				String tmpuser=tetuser.getText();
				String tmppassword=tetpassword.getText();
				ResultSet rs;
				JDBC jd=new JDBC();
				Connection cn=null;
				PreparedStatement ps=null;
				try {
					cn=jd.getConnection();
					String sqlselect="select * from information where LogName=? and Password=?";
					ps=cn.prepareStatement(sqlselect);
					ps.setString(1,tmpuser);
					ps.setString(2,tmppassword);
					rs=ps.executeQuery();
					String qq="";
					while(rs.next()){
						qq=rs.getString("LogName");
					}
                     if(qq.equals("")){
                    	 JOptionPane.showMessageDialog(null, "用户名或密码错误");
                     }else{
                    	 JOptionPane.showMessageDialog(null, "登录成功");
                    	 Main ma=new Main(qq);
                    	 ma.setVisible(true);
                    	 CopyQQ.this.dispose();
                    	// System.exit(0);
                     }
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		btncancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
			}
		});
		lab.setBounds(80,0,200,30);
		labserver.setBounds(60,30,50,20);
		tetserver.setBounds(110,30,100,20);
		labuser.setBounds(60,60,50,20);
		tetuser.setBounds(110,60,100,20);
		labpassword.setBounds(60,90,50,20);
		tetpassword.setBounds(110,90,100,20);
		btnlog.setBounds(30,120,70,30);
		btncancel.setBounds(110,120,70,30);
		btnreg.setBounds(190,120,70,30);
		c.add(p,BorderLayout.CENTER);
		this.setSize(300,200);
		this.setVisible(true);
	}
	public CopyQQ(String s){
		super(s);
		init();
	}
	public static void main(String[] args) {
		CopyQQ qq=new CopyQQ("用户登录");
	}

}

bubuko.com,布布扣

注册界面:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Register extends JFrame{
	JLabel jllog;
	JLabel jlname;
	JLabel jlpassword;
	JLabel jlsurepassword;
	JLabel jlip;
	JLabel jlport;
	JTextField tetlog;
	JTextField tetname;
	JTextField tetpassword;
	JTextField tetsurepassword;
	JTextField tetip;
	JTextField tetport;
	JButton btnreg;
	JPanel p;
	Container c;
	public void init(){
		c=this.getContentPane();c.setLayout(new BorderLayout());
		p=new JPanel();p.setLayout(null);
		btnreg=new JButton("注册");
		jllog=new JLabel("登录名:");jlname=new JLabel("姓名:");jlpassword=new JLabel("密码:");
		jlsurepassword=new JLabel("确认密码:");jlip=new JLabel("IP:");jlport=new JLabel("端口号:");
		tetlog=new JTextField(20);tetname=new JTextField(20);tetip=new JTextField(20);
		tetpassword=new JTextField(20);tetsurepassword=new JTextField(20);tetport=new JTextField(20);
		p.add(jllog);p.add(jlname);p.add(jlpassword);p.add(jlsurepassword);p.add(jlip);p.add(jlport);
		p.add(tetlog);p.add(tetname);p.add(tetpassword);p.add(tetsurepassword);p.add(tetip);p.add(tetport);
		p.add(btnreg);
		jllog.setBounds(0,0,130,20);tetlog.setBounds(150,0,120,20);
		jlname.setBounds(0,30,130,20);tetname.setBounds(150,30,120,20);
		jlpassword.setBounds(0,60,130,20);tetpassword.setBounds(150,60,120,20);
		jlsurepassword.setBounds(0,90,130,20);tetsurepassword.setBounds(150,90,120,20);
		jlip.setBounds(0,120,130,20);tetip.setBounds(150,120,120,20);
		jlport.setBounds(0,150,130,20);tetport.setBounds(150,150,120,20);
		btnreg.setBounds(0,180,150,20);
		btnreg.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String strlog=tetlog.getText();
				String strname=tetname.getText();
				String strpassword=tetpassword.getText();
				String strsurepassword=tetsurepassword.getText();
				String strip=tetip.getText();
				String strport=tetport.getText();
				if(strpassword.equals(strsurepassword)&&!strpassword.equals(null)){
					JDBC jd=new JDBC();
					Connection conn = null;
					PreparedStatement ps = null;
					try {
						conn=jd.getConnection();
						String sqlinsert="insert into information(LogName,Name,Password,Ip,Port) values(?,?,?,?,?)";
						ps = conn.prepareStatement(sqlinsert);
						ps.setString(1,strlog);
						ps.setString(2,strname);
						ps.setString(3,strpassword);
						ps.setString(4,strip);
						ps.setString(5,strport);
						ps.executeUpdate();
						System.exit(0);
					} catch (SQLException e2) {
						e2.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "注册成功");
					System.exit(0);
				}
				else{
					JOptionPane.showMessageDialog(null, "两次密码不一致");
				}
			}
		});
		c.add(p,BorderLayout.CENTER);
		this.setSize(300,250);
		this.setVisible(true);
	}
	public Register(){
		super("新用户注册");
		init();
	}
	public static void main(String[] args) {
		Register reg=new Register();
	}

}

bubuko.com,布布扣

主界面:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame{
	Container c;
	JLabel jltop;
	JLabel jlbelow;
	JLabel user1;
	JLabel user2;
	ImageIcon image1;
	ImageIcon image2;
	JPanel p=new JPanel();
	String myQQ="";
	void init(){
		c=this.getContentPane();
		c.setLayout(new BorderLayout());
		p.setLayout(null);
		jltop=new JLabel("                             好友列表");
		jlbelow=new JLabel("                          欢迎您,李四");
		image1=new ImageIcon("F:/程序/java程序/高级案例与分析/image/qq1.png");
		image2=new ImageIcon("F:/程序/java程序/高级案例与分析/image/qq2.png");
		user1=new JLabel("张三",image1,JLabel.LEFT);
		user2=new JLabel("王五",image2,JLabel.LEFT);
		user1.setBounds(0,20,70,20);
		user2.setBounds(0,50,70,20);
		c.add(jltop,BorderLayout.NORTH);
		p.add(user1);
		p.add(user2);
		c.add(p);
		c.add(jlbelow,BorderLayout.SOUTH);
		JDBC jd=new JDBC();
		Connection cn=null;
		PreparedStatement ps=null;
		ResultSet rs;
		try {
			cn=jd.getConnection();
			String sqlSelect="select * from information where LogName=?";
			ps=cn.prepareStatement(sqlSelect);
			ps.setString(1,myQQ);
			rs=ps.executeQuery();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		user1.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				Talk t1=new Talk(myQQ,3000,5000);
				Talk t2=new Talk("user1",5000,3000);
			}
		});
		user2.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				Talk t1=new Talk(myQQ,3000,5000);
				Talk t2=new Talk("user2",5000,3000);
			}
		});
		this.setSize(250,600);
		this.setVisible(true);
	}
	public Main(String qq){
		super(qq);
		myQQ=qq;
		init();
	}
	public static void main(String[] args) {
		Main ma=new Main("欢迎使用QQ聊天");
	}

}

bubuko.com,布布扣

聊天界面:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Talk extends JFrame{
	JLabel la=new JLabel("请输入");
	JTextField tfd=new JTextField(20);
	JButton but=new JButton("发送");
	JPanel p=new JPanel();
	JTextArea jta=new JTextArea(10,20);
	Container c;
	DatagramSocket ds;
	DatagramReceive dc;
	int localPost=3000;
	int remotePost=5000;
	boolean flag=true;
	public void init(){
		c=this.getContentPane();
		c.setLayout(new BorderLayout());
		p.setLayout(new FlowLayout());
		p.add(la);p.add(tfd);p.add(but);
		c.add(p,BorderLayout.NORTH);
		c.add(jta,BorderLayout.CENTER);
		but.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String temp=tfd.getText();
				jta.append("\n我说:"+temp);
				byte[] buf=temp.getBytes();
				try {
					DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("localhost"),remotePost);
					try {
						ds.send(dp);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					if(temp.trim().equals("bye")){
						flag=false;
						//dc.close();
					}
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				}
			}
		});
	this.setSize(500,500);
	this.setVisible(true);
	}
	public Talk(String name,int localPost,int remotePost){
		super(name);
		this.localPost=localPost;
		this.remotePost=remotePost;
		init();
		try {
			ds=new DatagramSocket(localPost);
			dc=new DatagramReceive(name,ds,jta);
			dc.start();
		} catch (SocketException e) {
			System.out.println("DatagramSocket创建失败");
		}
		
	}
	public static void main(String[] args) {
	}

}

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import javax.swing.JTextArea;

public class DatagramReceive extends Thread{
	String name;
	DatagramSocket ds;
	JTextArea jta;
	boolean flag=true;
	public DatagramReceive(String name,DatagramSocket ds,JTextArea jta){
		this.name=name;
		this.ds=ds;
		this.jta=jta;
	}
		public void run(){
			while(flag){
				byte[] buf=new byte[1024];
				DatagramPacket dp=new DatagramPacket(buf,buf.length);
				try {
					ds.receive(dp);
					String temp=new String(buf,0,dp.getLength());
					jta.append("\n对方说:"+temp);
					if(temp.trim().equals("bye")){
						flag=false;
						ds.close();
					}
				} catch (IOException e) {
					System.out.println(name+"接受失败");
				}
				
			}
		}

}
bubuko.com,布布扣
连接数据库部分:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC {
	Statement st;
	String url="jdbc:sqlserver://localhost:1433;DatabaseName=QQ";
	String user="sa";
	String password="a123456";
	static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	Connection conn;
	static {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}  
	}
	public  Connection getConnection() throws SQLException{
		conn=DriverManager.getConnection(url,user,password);
		return conn;
	}
	public static void main(String[] args) throws ClassNotFoundException,SQLException{
		JDBC jd=new JDBC();
	}

}


山寨版QQ,布布扣,bubuko.com

山寨版QQ

标签:style   blog   class   code   java   tar   

原文地址:http://blog.csdn.net/crescent__moon/article/details/25228249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!