Java实现仿QQ登陆、好友界面(可连接数据库)

一、项目结构

在这里插入图片描述

二、项目功能

1、登录功能
2、连接数据库
3、其他的我还没有加,等我学到了那些部分我在加上去!!

三、制作界面

在DIYQQ登陆界面时,我们需要将QQ登陆界面大致分为不同的模块,然后根据不同的模板,用不同的容器分割开,我们可以在容器里面添加不同的组件。

1、QQ登陆界面

package GUI;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
public class Test1 
{
//创建组件:
	JFrame f=new JFrame("QQ登陆界面");//创建窗口
	String [] a= {"123456","456789","789123"};//用数组来存放QQ号和密码
	JComboBox <String>c=new JComboBox<String>(a);
	JLabel l1=new JLabel("注册账号");
	JLabel l2=new JLabel("找回密码");
	JCheckBox cb1=new JCheckBox("记住密码",false);
	JCheckBox cb2=new JCheckBox("自动登录",false);
	JPasswordField p=new JPasswordField();//密码框组件
	ImageIcon i1=new ImageIcon("C:\\images\\1.jpg");//QQ上方背景
	ImageIcon i2=new ImageIcon("C:\\images\\2.png");//QQ头像
	JLabel l3=new JLabel(i1);
	JLabel l4=new JLabel(i2);
	JButton button=new JButton("登陆");

	//将界面组装起来
	public void init() 
	{
		f.setSize(426, 300);//设置窗口尺寸
        f.setLocation(497, 242);//设置窗口位置
        f.setResizable(false);//禁止改变窗口大小
        //设置顶部图片位置
        Box box=Box.createVerticalBox();
        //设置图片尺寸
        i2.setImage(i2.getImage().getScaledInstance(400, 100, Image.SCALE_AREA_AVERAGING));
        box.add(l4);
        f.add(box,BorderLayout.NORTH);
        //设置底部复选框
        Box bs=Box.createHorizontalBox();
        cb1.setBounds(8,13,60,10);
        cb2.setBounds(120,13,60,10);
        bs.add(cb1);
        bs.add(cb2);
        f.add(bs);
        //设置密码框、账号输入框等
        Box bc=Box.createVerticalBox();
        bc.setPreferredSize(new Dimension(100,10));
        c.setBounds(8,15,175,30);
        p.setBounds(8,50,175,30);
        bc.add(c);
        bc.add(p);
        bc.add(bs);
        //设置注册账号和找回密码:
        Box br=Box.createVerticalBox();
        l1.setBounds(10,13,60,30);
        l2.setBounds(10,43,60,30);
        br.add(l1);
        br.add(l2);
        //设置头像、背景:
        i1.setImage(i1.getImage().getScaledInstance(60, 50, Image.SCALE_AREA_AVERAGING));
        Box bx=Box.createHorizontalBox();
        bx.add(l3);
        bx.add(bc);
        bx.add(br);
        bx.add(button);
        f.add(bx);
        //设置登陆按钮、记住密码等
        Box blast=Box.createVerticalBox();
        blast.add(bs);
        blast.add(button);
        f.add(blast,BorderLayout.SOUTH);
        My m1=new My();
        p.addActionListener(m1);//设置监听
        button.addActionListener(m1);//在登陆按钮上设置监听
		f.pack();//让软件自己安排合适的尺寸
		f.setVisible(true);//设置可见
	}
	class My implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0)//此处为动作监听事件
		{
			String o1=(String)c.getSelectedItem();//获取账号
			String o2=new String(p.getPassword());//将字符数组转化为字符串
			Test2 log =new 	Test2();//创建一个新对象
			Boolean booleans=false;//
			try
			{
				booleans=log.findUser(o1, o2);
			}
			catch (SQLException ex)
			{
				ex.printStackTrace();
			}
			if(booleans)
			{
				JOptionPane.showMessageDialog(null, "登录成功");//弹出正确信息
				System.out.println("登录成功");
				f.dispose();
				 Qqdenglu fs=new Qqdenglu();
               fs.login();
				System.out.println("登录成功");
			}
			else 
			{
				JOptionPane.showMessageDialog(null, "你输入的账户名或密码不正确,请重新输入");//弹出错误信息
				System.out.println("你输入的账号不正确");
			}		
		}
		
	}
	public static void main(String [] args)
	{
		new Test1().init();
	}
}

2、好友界面

同样的,我们可以参考之前制作登录界面的方式去制作好友列表的界面,使用方法和登录界面是一样的。

package GUI;

import java.awt.*;
import javax.swing.*;

public class Qqdenglu extends Test1
{
	JFrame f=new JFrame("QQ");
	TextArea ta=new TextArea()//设置文本框
	ImageIcon i6=new ImageIcon("C:\\images\\2.jpg");
	JLabel l6=new JLabel(i6);
	JLabel ll1=new JLabel("网名:.");
	JLabel ll2=new JLabel("个性签名");
	TextField tf2=new TextField("搜索");
	Button bu1=new Button("消息");
	Button bu2=new Button("联系人");
	Button bu3=new Button("好友");
	Button bu4=new Button("群聊");
	Button bu5=new Button("+");
	Button bu6=new Button("频道");
	Button bu7=new Button("加好友");
	Button bu8=new Button("腾讯文档");
	Button bu9=new Button("QQ页游");
	Button bu10=new Button("QQ畅玩");
	Button bu11=new Button("微云");
	Choice c1=new Choice();
	Choice c2=new Choice();
	Choice c3=new Choice();
	Choice c4=new Choice();
	Choice c5=new Choice();
	Choice c6=new Choice();
	Choice c7=new Choice();
	Choice c8=new Choice();
	public void login() 
	{
		c1.add("空间");
		c2.add("新朋友");
		c3.add("我的设备 2/2");
		c4.add("特别关心 7/7");
		c5.add(" 8/11");
		c6.add("   120/137");
		Box b1=Box.createVerticalBox();
		b1.add(ll1);
		b1.add(ll2);
		
		Box b2=Box.createHorizontalBox();
		i6.setImage(i6.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
     	b2.add(l6,BorderLayout.EAST);
		b2.add(b1,BorderLayout.EAST);
		
		Box b3=Box.createVerticalBox();
		b3.add(b2);
		b3.add(tf2);
		f.add(b3);
		
		Box b4=Box.createHorizontalBox();
		b4.add(bu1);
		b4.add(bu2);
		b4.add(bu6);
		b4.add(c1);
	    
		Box b5=Box.createVerticalBox();
		b5.add(b3);
		b5.add(b4);
		
		Box b7=Box.createVerticalBox();
		b7.add(c2);
		b7.add(c3);
		b7.add(c4);
		b7.add(c5);
		b7.add(c6);
		
		Box b6=Box.createVerticalBox();
        b6.add(b5);
        b6.add(b7);
        
        
		Box b8=Box.createHorizontalBox();
		c7.add("主菜单");
		b8.add(c7);
		b8.add(bu7);
		b8.add(bu8);
		b8.add(bu9);
		b8.add(bu10);
		b8.add(bu11);
		c8.add("应用管理器");
		b8.add(c8);
		
		Box b9=Box.createVerticalBox();
		b9.add(b6);
		b9.add(b8);
		f.add(b9);
		f.pack();
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args)
	{
		new Qqdenglu().login();
		
	}
}

3、数据库连接部分

数据库连接需要导入一个包,这里发不出来,需要的可以后台戳我。在连接数据库的时候,需要登录名、密码、数据库名,这个需要我们事先设置。

package GUI;

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

public class Test2
{

	PreparedStatement prestmt=null;
	Connection c=null;
	ResultSet rs =null;
public Boolean findUser(String qqnumber,String password)throws SQLException
{
	try
	{
	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	c=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=test","sa","123456");//数据库名称test,用户名:sa,密码:123456
	String sql="select * from tb_qquser1"+" where qqnumber=? and password=?";
    prestmt=c.prepareStatement(sql);
    prestmt.setString(1, qqnumber);
    prestmt.setString(2, password);
	rs=prestmt.executeQuery();
	if(rs.next())
	{
		return true;//返回Boolean booleans=false;
	}
	}
	catch(Exception e)
	{
	e.printStackTrace();	
	}
	finally
	{
	if(rs!=null) {rs.close();}
	if(prestmt!=null) {prestmt.close();}
	if(c!=null) {c.close();}
}
	return false;
}
public static void main(String[] args) throws SQLException
{
	Test2 t2=new Test2();
	System.out.println(t2.findUser("123456","123456"));
}
}

4、数据库代码

在连接数据库之前。需要新建一个登录名(如果你之前有就可以不用建)---->>新建数据库—>>新建查询—>>设置ID为主键,qq号,和qq密码

use test
 go 
create table  tb_qquser1
(
id int identity(1,1) primary key(id),//设置ID为主键
qqnumber varchar(50),
password varchar(50)
);
insert into tb_qquser1(qqnumber,password)
values('123456','123456');
insert into tb_qquser1(qqnumber,password)
values('456789','456789');
insert into tb_qquser1 (qqnumber,password)
values('789123','789123');

四、项目实现结果

在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 Java 编写的 QQ 登录界面的示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QQLogin extends JFrame implements ActionListener { private JLabel label1, label2; private JTextField text1; private JPasswordField text2; private JButton button1, button2; public QQLogin() { setTitle("QQ登录"); setSize(300, 180); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); label1 = new JLabel("QQ号码:"); label2 = new JLabel("QQ密码:"); text1 = new JTextField(20); text2 = new JPasswordField(20); button1 = new JButton("登录"); button2 = new JButton("取消"); JPanel panel1 = new JPanel(); panel1.setLayout(new GridLayout(2, 1)); panel1.add(label1); panel1.add(label2); JPanel panel2 = new JPanel(); panel2.setLayout(new GridLayout(2, 1)); panel2.add(text1); panel2.add(text2); JPanel panel3 = new JPanel(); panel3.setLayout(new FlowLayout()); panel3.add(button1); panel3.add(button2); setLayout(new BorderLayout()); add(panel1, BorderLayout.WEST); add(panel2, BorderLayout.CENTER); add(panel3, BorderLayout.SOUTH); button1.addActionListener(this); button2.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { String qq = text1.getText().trim(); String pwd = new String(text2.getPassword()).trim(); if ("".equals(qq) || "".equals(pwd)) { JOptionPane.showMessageDialog(this, "请输入QQ号码和密码!", "提示", JOptionPane.WARNING_MESSAGE); return; } // TODO: 在这里添加登录验证逻辑 JOptionPane.showMessageDialog(this, "QQ号码或密码错误,请重新输入!", "提示", JOptionPane.WARNING_MESSAGE); text1.setText(""); text2.setText(""); } else if (e.getSource() == button2) { System.exit(0); } } public static void main(String[] args) { QQLogin login = new QQLogin(); login.setVisible(true); } } ``` 该示例使用了 Java 的图形用户界面(GUI)库 Swing,通过 JFrame、JLabel、JTextField、JPasswordField 和 JButton 等组件实现了一个简单的 QQ 登录界面。其中,actionPerformed 方法中的 TODO 部分可以根据实际需要添加登录验证逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值