JavaSwing仿QQ登录界面,注释完善,适合新手学习

使用说明:

  1. 这是一个java做的仿制QQ登录界面,界面仅使用一个类,
  2. JDK版本为jdk-11 
  3. 素材包的名字为:素材(下载)请在项目中新建一个名字为“素材”的文件夹。
  4. 素材: https://pan.baidu.com/s/1XoXlU4ITi621GLEkuDkSAA 提取码: 1234 
  5. 适合新手学习java Swing使用,如有任何运行问题,请联系QQ:1585889713 我尽量秒回
  6. 注释很完善,句句都写的明明白白的。

package 第十章.view;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
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 第十一章.界面类.Table;//此界面为java的jdbc做的增删改和模糊查,数据库MySQL,需要的联系我QQ:1585889713

public class Login extends JFrame implements MouseListener {

JLabel bacgrangd,jan,bi,QQ,qq,tu;//gif,最小化,关闭,logo,QQ,头像
JLabel an1,an2,lie1,lie2;//暗色块|线
JTextField user;//账号
JPasswordField pass;//密码
JPanel bgcolor;//白
JLabel su1,mi1,ku1,ku2,gou1,gou2;//缩略图
JLabel text1,text2,text3,text4,text5;//自动登录,记住密码,找回密码,注册账号,登录
static Point origin = new Point();//变量,用于可拖动窗体
int a=0,b=0,c=0,d=0;//控制线
int f=0,g=0,h=0,j=0;//控制√
JLabel submit,ma;//背景


public Login() {

//实例化
bacgrangd = new JLabel(new ImageIcon("素材//1.gif"));
jan = new JLabel(new ImageIcon("素材//最小化.png"));
bi = new JLabel(new ImageIcon("素材//关闭.png"));
QQ = new JLabel(new ImageIcon("素材//qq.png"));
qq = new JLabel("QQ");
an1 = new JLabel();an2 = new JLabel();//暗调
tu = new JLabel(new ImageIcon("素材//头像.png"));
user = new JTextField();
pass = new JPasswordField();
su1 = new JLabel(new ImageIcon("素材//qq (1).png"));
mi1 = new JLabel(new ImageIcon("素材//密码.png"));
lie1 = new JLabel(new ImageIcon("素材//直线2.png"));
lie2 = new JLabel(new ImageIcon("素材//直线2.png"));
bgcolor = new JPanel();
ku1 = new JLabel(new ImageIcon("素材//框框.png"));
ku2 = new JLabel(new ImageIcon("素材//框框.png"));
gou1 = new JLabel(new ImageIcon("素材//对勾.png"));
gou2 = new JLabel(new ImageIcon("素材//对勾.png"));
text1 = new JLabel("自动登录");
text2 = new JLabel("记住密码");
text3 = new JLabel("找回密码");
text4 = new JLabel("注册账号");
text5 = new JLabel("登录");
submit = new JLabel();
ma = new JLabel(new ImageIcon("素材//二维码.png"));

//位置
bacgrangd.setBounds(-35, -123, 500, 250);
jan.setBounds(364, 2, 32, 32);
bi.setBounds(396, 3, 32, 32);
QQ.setBounds(10, 10, 32, 32);
qq.setBounds(50, 5, 45, 45);
an1.setBounds(361, 0, 35, 35);
an2.setBounds(395, 0, 35, 35);
tu.setBounds(170, 80, 90, 85);
user.setBounds(130, 160, 180, 40);
pass.setBounds(130, 200, 180, 40);
su1.setBounds(100, 170, 20, 20);
mi1.setBounds(100, 210, 20, 20);
lie1.setBounds(100, 190, 240, 10);
lie2.setBounds(100, 230, 240, 10);
bgcolor.setBounds(0, 125, 500, 300);
ku1.setBounds(100, 250, 20, 20);
ku2.setBounds(190, 250, 20, 20);
gou1.setBounds(106, 255, 10, 10);
gou2.setBounds(196, 255, 10, 10);
text1.setBounds(125, 250, 80, 20);
text2.setBounds(215, 250, 80, 20);
text3.setBounds(288, 250, 80, 20);
text4.setBounds(15, 300, 80, 20);
text5.setBounds(206, 285, 80, 20);
submit.setBounds(100, 280, 242, 35);
ma.setBounds(385, 290, 30, 30);
//属性
qq.setFont(new Font("微软雅黑", 1, 25));
qq.setForeground(Color.white);
an1.setBackground(new Color(0,0,0,0.3f));
an2.setBackground(new Color(0,0,0,0.3f));
bgcolor.setBackground(new Color(255, 255, 255));

user.setForeground(Color.gray);
user.setText("QQ号码/手机/邮箱");
user.setOpaque(false);//透明背景
user.setBorder(null);//去掉边框
user.setFont(new Font("微软雅黑", Font.PLAIN, 16));//框内文字样式
pass.setFont(new Font("微软雅黑", Font.PLAIN, 16));//框内文字样式
pass.setBorder(null);//去掉边框

pass.setOpaque(false);//透明背景
pass.setForeground(Color.gray);
pass.setText("密码");
pass.setEchoChar((char)0);//让密码显示出来

text1.setFont(new Font("微软雅黑", 0, 12));
text2.setFont(new Font("微软雅黑", 0, 12));
text3.setFont(new Font("微软雅黑", 0, 12));
text4.setFont(new Font("微软雅黑", 0, 12));
text5.setFont(new Font("微软雅黑", 0, 15));
text1.setForeground(new Color(170, 170, 170));
text2.setForeground(new Color(170, 170, 170));
text3.setForeground(new Color(170, 170, 170));
text4.setForeground(new Color(170, 170, 170));
text5.setForeground(Color.white);

gou1.setVisible(false);
gou2.setVisible(false);

submit.setBackground(new Color(5, 186, 251));
submit.setOpaque(true);

text3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
text4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));


//事件区域
jan.addMouseListener(this);
bi.addMouseListener(this);
user.addMouseListener(this);
pass.addMouseListener(this);
text1.addMouseListener(this);
text2.addMouseListener(this);
text3.addMouseListener(this);
text4.addMouseListener(this);
ku1.addMouseListener(this);
ku2.addMouseListener(this);
submit.addMouseListener(this);
ma.addMouseListener(this);
this.addMouseListener(this);


this.addMouseMotionListener(new MouseMotionListener() {//窗体拖动事件
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
Point p = getLocation();
setLocation(p.x + e.getX() - origin.x, p.y + e.getY()- origin.y);
}
});

user.addFocusListener(new FocusListener() {

public void focusLost(FocusEvent e) {//失去焦点
su1.setIcon(new javax.swing.ImageIcon("素材//qq (1).png"));
lie1.setIcon(new javax.swing.ImageIcon("素材//直线2.png"));
c=0;
if(user.getText().isEmpty()) {//判断是否为空(为了设置默认提示语)
user.setForeground(Color.gray);
user.setText("QQ号码/手机/邮箱");
}
}

public void focusGained(FocusEvent e) {//得到焦点
user.setForeground(Color.black);
lie1.setIcon(new javax.swing.ImageIcon("素材//直线3.png"));
a=1;c=1;b=0;
su1.setIcon(new javax.swing.ImageIcon("素材//qq (2).png"));
if(user.getText().equals("QQ号码/手机/邮箱")) {
user.setText("");
}else {
user.setText(user.getText());
user.selectAll();
}
}
});

pass.addFocusListener(new FocusListener() {

public void focusLost(FocusEvent e) {//失去焦点
lie2.setIcon(new javax.swing.ImageIcon("素材//直线2.png"));//失去焦点换图片
mi1.setIcon(new javax.swing.ImageIcon("素材//密码.png"));
d=0;
if(pass.getText().isEmpty()) {
pass.setForeground(Color.gray);
pass.setText("密码");
pass.setEchoChar((char)0);//让密码显示出来
}
}

public void focusGained(FocusEvent e) {//得到焦点
mi1.setIcon(new javax.swing.ImageIcon("素材//密码 (1).png"));
lie2.setIcon(new javax.swing.ImageIcon("素材//直线3.png"));
b=1;a=0;d=1;
pass.setForeground(Color.black);
pass.setEchoChar('*');//让用户输入看不见
if(pass.getText().equals("密码")) {
pass.setText("");
}else {
pass.setText(pass.getText());
}
}
});


this.setLayout(null);//布局

this.add(jan);
this.add(bi);
this.add(qq);
this.add(QQ);
this.add(an1);
this.add(an2);
this.add(tu);
this.add(lie1);
this.add(lie2);
this.add(user);
this.add(pass);
this.add(su1);
this.add(mi1);
this.add(gou1);
this.add(gou2);
this.add(ku1);
this.add(ku2);
this.add(text1);
this.add(text2);
this.add(text3);
this.add(text4);
this.add(text5);
this.add(submit);
this.add(ma);
this.add(bgcolor);
this.add(bacgrangd);

this.setSize(430, 330);
this.setIconImage(Toolkit.getDefaultToolkit().createImage("素材\\透明照片.png"));//窗体图标
this.setLocationRelativeTo(null);//保持居中
this.setUndecorated(true);//去顶部
this.setFocusable(true);//面板首先获得焦点
this.setBackground(new Color(255,255,255));//背景颜色
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);//最顶层
this.setVisible(true);//显示
}


public static void main(String[] args) {

new Login();

}

public void mouseClicked(MouseEvent e) {//点击不恢复
}

public void mousePressed(MouseEvent e) {//点击后
if (e.getSource() == jan) {
setExtendedState(JFrame.ICONIFIED);
}else if(e.getSource()== this) {
origin.x = e.getX();
origin.y = e.getY();
}else if(e.getSource()==bi) {
System.exit(0);
}else if(e.getSource()==ku1||e.getSource()==text1) {
if(f==0) {
gou1.setVisible(true);
g=1;f=1;
}else if(g==1) {
gou1.setVisible(false);
f=0;g=0;
}
}else if(e.getSource()==ku2||e.getSource()==text2) {
if(h==0) {
gou2.setVisible(true);
j=1;h=1;
}else if(j==1) {
gou2.setVisible(false);
h=0;j=0;
}
}else if(e.getSource()==submit||e.getSource()==text5) {
text5.setFont(new Font("微软雅黑", 0, 14));
dispose();

String users = user.getText();
String password = pass.getText();

if(users.equals("向家康")&&password.equals("666666")) {
//new Table();//打开新的主界面如果要关闭登录界面可以写  dispose();
}else {
JOptionPane.showMessageDialog(null, "用户名:向家康,密码:666666,您并未设置打开界面!");
new Login();
}

}
}

public void mouseReleased(MouseEvent e) {//点击时
if(e.getSource()==submit||e.getSource()==text5) {
text5.setFont(new Font("微软雅黑", 0, 15));
}
}

public void mouseEntered(MouseEvent e) {//悬停
if (e.getSource() == jan) {
an1.setOpaque(true);
}else if(e.getSource()==bi) {
an2.setOpaque(true);
}else if(e.getSource()==user) {
if(a==0&&c==0) {
lie1.setIcon(new javax.swing.ImageIcon("素材//直线4.png"));
}
}else if(e.getSource()==pass) {
if(b==0&&d==0) {
lie2.setIcon(new javax.swing.ImageIcon("素材//直线4.png"));
}
}else if(e.getSource()==text3) {
text3.setForeground(Color.GRAY);
}else if(e.getSource()==text4) {
text4.setForeground(Color.GRAY);
}else if(e.getSource()==ma) {
ma.setIcon(new javax.swing.ImageIcon("素材//二维码2.png"));
}
}

public void mouseExited(MouseEvent e) {//悬停后
if (e.getSource() == jan) {
an1.setOpaque(false);
}else if(e.getSource()==bi) {
an2.setOpaque(false);
}else if(e.getSource()==user) {
if(a==0) {
lie1.setIcon(new javax.swing.ImageIcon("素材//直线2.png"));
}
}else if(e.getSource()==pass) {
if(b==0) {
lie2.setIcon(new javax.swing.ImageIcon("素材//直线2.png"));
}
}else if(e.getSource()==text3) {
text3.setForeground(new Color(170, 170, 170));
}else if(e.getSource()==text4) {
text4.setForeground(new Color(170, 170, 170));
}else if(e.getSource()==ma) {
ma.setIcon(new javax.swing.ImageIcon("素材//二维码.png"));
}

}

}

 

 //登录界面背景是 动态的!欢迎大家来挑刺。。。最好是来学习,哈哈哈。

 

 

转载于:https://www.cnblogs.com/Xiangjiakang/p/10961165.html

package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; 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.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值