失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Java实现仿QQ登陆 好友界面(可连接数据库)

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

时间:2019-05-20 23:58:13

相关推荐

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

文章目录

一、项目结构二、项目功能三、制作界面1、QQ登陆界面2、好友界面3、数据库连接部分4、数据库代码四、项目实现结果

一、项目结构

二、项目功能

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{@Overridepublic 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,密码:123456String 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 testgo 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');

四、项目实现结果

如果觉得《Java实现仿QQ登陆 好友界面(可连接数据库)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。