学习了Java的GUI后,尝试着做了一个仿QQ登录界面,感觉还行,简单地实现了自动登录,记住密码,以及连接MySQL(8.0)数据库判断账号密码的功能。
运行效果:
package a;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.print.attribute.AttributeSet;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
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 javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import com.mysql.cj.protocol.Resultset;
import java.sql.*;
public class InitQQ extends JFrame{
private int x=0,y=0;//用来存放拖动窗口时鼠标的当前位置
private loginButtonHandler ll;//登陆按钮的监听对象
private JFrame frame;//frame窗口
private JButton miniButton;//最小化按钮
private JButton closeButton;//关闭按钮
private JButton loginButton;//登录按钮
private JPanel northPanel;//北部面板
private JPanel westPanel;//西部面板
private JPanel centerPanel;//中部面板
private JPanel eastPanel;//东部面板
private JPanel southPanel;//南部面板
private JLabel northLabel;//北部标签
private JLabel westLabel;//西部标签
private JLabel codeLabel;//忘记密码标签
private JLabel enrollLabel;//注册账号标签
private JTextField text;//账号文本框
private JPasswordField codeWord;//密码框
private JCheckBox auto;//自动登录复选框
private JCheckBox mima;//记住密码复选框
private static boolean mimaState;//记住密码复选框的当前状态
private static boolean loginState;//自动登录复选框的当前状态״̬״̬
double time;//让主界面停留的时间
private Connection conn;
private static String selectSql="select qq from userinfo";//查找语句
private static String id;//储存记住的账号
//初始化界面
public InitQQ() {
frame=new JFrame("QQ登录");
Toolkit t=Toolkit.getDefaultToolkit();
Dimension d=t.getScreenSize();
frame.setBounds((d.width-d.width/3)/2,(d.height-d.height/3)/2,500,330);
frame.setIconImage(t.getImage(InitQQ.class.getResource("1.jpg")));
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame.setUndecorated(true);//取消自带的边框
frame.setResizable(false);
northPanel=creat_north();
westPanel=creat_west();
centerPanel=creat_center();
southPanel=creat_south();
eastPanel=creat_east();
frame.add(northPanel,BorderLayout.NORTH);
frame.add(westPanel,BorderLayout.WEST);
frame.add(southPanel,BorderLayout.SOUTH);
frame.add(centerPanel,BorderLayout.CENTER);
frame.add(eastPanel,BorderLayout.EAST);
//由于将JFrame自带的边框隐藏了,所以需要通过下面的方法来实现窗口的拖动功能
frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x=e.getY();//鼠标的X
y=e.getY();//鼠标的Y
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
int x_screen=e.getXOnScreen();
int y_screen=e.getYOnScreen();
int xx=x_screen-x;
int yy=y_screen-y;
frame.setLocation(xx, yy);
}
});
//此句必须放在最后
frame.setVisible(true);
if(auto.isSelected())
{
Date d1=new Date();
try {
Thread.sleep(1000);//ͣ停留一秒
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Date d2=new Date();
time=(double)((d2.getTime()-d1.getTime())/1000);
System.out.println("时间停留"+time);
}
if(time==1&&auto.isSelected())
frame.dispose();
else {
if(time==1)//如果未选中自动登录的复选框,则改变其状态״̬
{
File file1=new File("d:\\自动登录的状态.txt");
OutputStream os;
try {
os = new FileOutputStream(file1);
String str="false";
byte[] b=str.getBytes();
os.write(b);
os.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
//创建北部面板
public JPanel creat_north() {
JPanel jp1=new JPanel();
jp1.setLayout(null);
jp1.setPreferredSize(new Dimension(0,190));
ImageIcon in=new ImageIcon(InitQQ.class.getResource("11.png"));
JLabel cc=new JLabel(in);
cc.setBounds(0,0,500,190);
//this.getLayeredPane().add(jp1, new Integer(Integer.MIN_VALUE));
//jp1=(JPanel)this.getContentPane();
cc.setOpaque(false);
closeButton=new JButton(new ImageIcon(InitQQ.class.getResource("2.jpg")));//将图片作为按钮的背景
closeButton.setRolloverIcon(new ImageIcon(InitQQ.class.getResource("2.png")));//鼠标进入按钮时切换图片
closeButton.setPressedIcon(new ImageIcon(InitQQ.class.getResource("2.png")));//鼠标按下按钮时切换图片
miniButton=new JButton(new ImageIcon(InitQQ.class.getResource("3.jpg")));
miniButton.setRolloverIcon(new ImageIcon(InitQQ.class.getResource("3.png")));
miniButton.setPressedIcon(new ImageIcon(InitQQ.class.getResource("3.png")));
closeButton.setBounds(468, 0, 30, 30);
closeButton.setToolTipText("关闭");
miniButton.setBounds(437, 0, 30, 30);
miniButton.setToolTipText("最小化");
closeButton.setContentAreaFilled(false);//设置按钮透明
closeButton.setBorderPainted(false);//取消按钮的边框
closeButton.setFocusPainted(false);//消除按钮的焦点,即点击按钮时不出现边框
miniButton.setContentAreaFilled(false);
miniButton.setBorderPainted(false);
miniButton.setFocusPainted(false);
//jb2.setContentAreaFilled(b);
jp1.add(closeButton);
jp1.add(miniButton);
jp1.add(cc);
closeButtonHandler e=new closeButtonHandler();
closeButton.addActionListener(e);
miniButton.addActionListener(new miniButtonHandler());
return jp1;
}
//创建西部面板
public JPanel creat_west() {
JPanel jp2=new JPanel();
jp2.setLayout(null);
jp2.setPreferredSize(new Dimension(160,0));
ImageIcon ss=new ImageIcon(InitQQ.class.getResource("5.jpg"));
JLabel cs=new JLabel(ss);
cs.setBounds(35,0,100,100);
jp2.add(cs);
return jp2;
}
//创建中部面板
public JPanel creat_center() {
File file=new File("d:\\记住密码的状态.txt");
if(file.exists())
{
InputStream input=null;
try {
input=new FileInputStream(file);
byte[] b=new byte[(int)file.length()];
input.read(b);
mimaState=Boolean.parseBoolean(new String(b));//将byte类型转化为boolean型,若用getBoolean(),则全为false;此方法中的字符串忽略大小写等于true时,结果才为true,否则都为false
input.close();
System.out.println(mimaState);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
mimaState=Boolean.parseBoolean(new String("false"));
File file1=new File("d:\\自动登录的状态.txt");
if(file1.exists())
{
InputStream input=null;
try {
input=new FileInputStream(file1);
byte[] b=new byte[(int)file1.length()];
input.read(b);
loginState=Boolean.parseBoolean(new String(b));//将byte类型转化为boolean型,若用getBoolean(),则全为false;此方法中的字符串忽略大小写等于true时,结果才为true,否则都为false
input.close();
System.out.println(loginState);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
loginState=Boolean.parseBoolean(new String("false"));
JPanel jp4=new JPanel();
jp4.setLayout(null);
jp4.setPreferredSize(new Dimension(0,220));
text=new JTextField(10);//最多存放10个字
text.setBounds(0,10,200,30);
text.setFont(new Font("宋体",Font.BOLD,17));//字体和字体大小
//使QQ账号框只能输入数字
text.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
int key=e.getKeyChar();
int count =0;
//System.out.println(key);
if((key>=KeyEvent.VK_0&&key<=KeyEvent.VK_9))
count++;
if((!(key>=KeyEvent.VK_0&&key<=KeyEvent.VK_9))||(count==10))
e.consume();
}
});
mima=new JCheckBox("记住密码",mimaState);
mima.setBounds(0,78,80,18);
auto=new JCheckBox("自动登录",loginState);
auto.setBounds(110, 78, 80, 18);
text.addFocusListener(new JTextFieldHdandler(text,mima,"QQ号"));
text.setOpaque(false);
jp4.add(text);
codeWord=new JPasswordField(18);
codeWord.setBounds(0,42, 200, 30);
codeWord.setFont(new Font("宋体",Font.BOLD,17));
codeWord.addFocusListener(new JPasswordFielddHdandler(codeWord,mima,"密码"));
codeWord.setOpaque(false);
jp4.add(mima);
jp4.add(codeWord);
jp4.add(auto);
ll=new loginButtonHandler(text,codeWord,mima,auto);//将账号框和密码框注册到登陆按扭的事件处理上
return jp4;
}
//创建南部面板
public JPanel creat_south() {
JPanel jp3=new JPanel();
jp3.setLayout(null);
jp3.setPreferredSize(new Dimension(0,40));
loginButton= new JButton(new ImageIcon(InitQQ.class.getResource("20.png")));
loginButton.setBounds(160,0,200,30);
loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//将鼠标设置为手掌型
loginButton.setContentAreaFilled(false);
loginButton.setFocusPainted(false);
jp3.add(loginButton);
loginButton.addActionListener(ll);
return jp3;
}
//创建东部面板
public JPanel creat_east() {
JPanel jp5=new JPanel();
jp5.setLayout(null);
jp5.setPreferredSize(new Dimension(130,0));
enrollLabel=new JLabel("注册账号");
enrollLabel.setBounds(0, 10, 100, 30);
enrollLabel.setFont(new Font("宋体",Font.BOLD,15));
enrollLabel.setForeground(new Color(100,149,238));
enrollLabel.addMouseListener(new LabelHandler(enrollLabel));
enrollLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//将鼠标设置为手型
codeLabel=new JLabel("忘记密码");
codeLabel.setBounds(0, 42, 100, 30);
codeLabel.setFont(new Font("宋体",Font.BOLD,15));
codeLabel.setForeground(new Color(100,149,238));
codeLabel.addMouseListener(new LabelHandler(codeLabel));
codeLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
jp5.add(enrollLabel);
jp5.add(codeLabel);
return jp5;
}
//北部面板关闭按钮的事件处理
class closeButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//frame.dispose();
System.exit(0);
}
}
//北部面板最小化按钮的事件处理
class miniButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
frame.setExtendedState(frame.ICONIFIED);
}
}
//南部的登录按钮的事件处理
class loginButtonHandler implements ActionListener {
private JTextField t;
private JPasswordField f;
private JCheckBox mima;
private JCheckBox login;
public loginButtonHandler(JTextField t,JPasswordField f,JCheckBox mima,JCheckBox login) {
this.t=t;
this.f=f;
this.mima=mima;
this.login=login;
}
public void actionPerformed(ActionEvent e) {
int count1=0;
int count2=0;
try {
conn=Tool.getConnection();
Statement stmt=conn.createStatement();
ResultSet rest = stmt.executeQuery(selectSql);
String a=(String)t.getText();
while(rest.next())
{
if(a.equals(rest.getString(1)))
{
System.out.println("有账号");
count1++;
break;
}
}
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if(count1==1)
{
String selectSqlMima="select password from userinfo where qq=?";
try {
PreparedStatement p1=conn.prepareStatement(selectSqlMima);
p1.setString(1, t.getText());
ResultSet rest1=p1.executeQuery();
String str2=new String(f.getPassword());
while(rest1.next()) {
if(str2.equals(rest1.getString(1)))
{
System.out.println("密码正确");
count2++;
break;
}
else
System.out.println("密码错误");
}
Tool.closeConnection(conn);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
String str1=(String)t.getText();
char[] p=f.getPassword();
String str2=new String(p);
System.out.println((String)t.getText());
System.out.println(str2);
File rememberMimaFile=new File("d:\\记住密码的状态.txt");
File autoLoginFile=new File("d:\\自动登录的状态.txt");
if(count1==1&&count2==1)
{
if(mima.isSelected())
{
File file2=new File("d:\\保存记住的账号.txt");
try {
file2.createNewFile();
OutputStream os1=new FileOutputStream(file2);
String str3=t.getText();
byte[] b1=str3.getBytes();
os1.write(b1);
os1.close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
rememberMimaFile.createNewFile();
OutputStream os=new FileOutputStream(rememberMimaFile);
String str="true";
byte[] b=str.getBytes();
os.write(b);
os.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
{
try {
rememberMimaFile.createNewFile();
OutputStream os=new FileOutputStream(rememberMimaFile);
String str="false";
byte[] b=str.getBytes();
os.write(b);
os.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(mima.isSelected()&&login.isSelected())
{
try {
autoLoginFile.createNewFile();
OutputStream os=new FileOutputStream(autoLoginFile);
String str="true";
byte[] b=str.getBytes();
os.write(b);
os.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
{
try {
autoLoginFile.createNewFile();
OutputStream os=new FileOutputStream(autoLoginFile);
String str="false";
byte[] b=str.getBytes();
os.write(b);
os.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
frame.dispose();
JFrame jf=new JFrame();
jf.setBounds(600,300,500,200);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(null,"账号或密码错误,请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
//System.exit(0);
}
}
//实现文本框的焦点功能,当焦点不在文本框内时,显示默认提示信息(QQ号)
public class JTextFieldHdandler implements FocusListener{
private String str;
private JTextField text1;
private JCheckBox rememberMima;
public JTextFieldHdandler(JTextField text1,JCheckBox rememberMima,String str) {
this.text1=text1;
this.rememberMima=rememberMima;
this.str=str;
if(rememberMima.isSelected())
{
File file=new File("d:\\保存记住的账号.txt");
try {
InputStream in=new FileInputStream(file);
byte[] bn=new byte[(int)file.length()];
in.read(bn);
in.close();
id=new String(bn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text1.setText(new String(id));
text1.setForeground(Color.BLACK);
}
else
{
text1.setText(str);
text1.setForeground(Color.gray);
}
}
public void focusGained(FocusEvent e) {
if(text1.getText().equals(str))
{
text1.setText("");
text1.setForeground(Color.BLACK);
}
}
public void focusLost(FocusEvent e) {
if(text1.getText().equals("")) {
text1.setForeground(Color.gray);
text1.setText(str);
}
}
}
//实现密码框的焦点功能,当焦点不在密码框内时,显示默认提示信息(密码)
public class JPasswordFielddHdandler implements FocusListener{
private String str;
private JPasswordField text1;
private JCheckBox rememberMima;
public JPasswordFielddHdandler(JPasswordField text1,JCheckBox rememberMima,String str) {
this.text1=text1;
this.rememberMima=rememberMima;
this.str=str;
if(rememberMima.isSelected())
{
String strMima="000";
String selectSql1="select * from userinfo where qq=?";
try {
conn=Tool.getConnection();
PreparedStatement p1=conn.prepareStatement(selectSql1);
p1.setString(1,id);
ResultSet rest1=p1.executeQuery();
while(rest1.next()) {
if(id.equals(rest1.getString("qq")))
{
strMima=rest1.getString(2);
System.out.println(strMima);
break;
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
text1.setText(strMima);
text1.setEchoChar('*');
text1.setForeground(Color.BLACK);
}
else
{
text1.setText(str);
text1.setEchoChar((char)(0));//不设置回显
text1.setForeground(Color.gray);
}
}
public void focusGained(FocusEvent e) {
if(text1.getText().equals(str))
{
text1.setText("");
text1.setEchoChar('*');//将回显设置为'*'
text1.setForeground(Color.BLACK);
}
}
public void focusLost(FocusEvent e) {
if(text1.getText().equals("")) {
text1.setEchoChar((char)(0));
text1.setForeground(Color.gray);
text1.setText(str);
}
}
}
//对注册账号的标签添加鼠标事件
public class LabelHandler extends MouseAdapter{
private JFrame jf;
private JLabel la;
public LabelHandler(JLabel la) {
this.la=la;
}
public void mouseClicked(MouseEvent e) {
jf=new JFrame("123");
jf.setBounds(500,300,200,200);
jf.setVisible(true);
}
public void mouseEntered(MouseEvent e) {
la.setForeground(new Color(255,77,35));
}
public void mousePressed(MouseEvent e) {
la.setForeground(new Color(255,77,35));
}
public void mouseExited(MouseEvent e) {
la.setForeground(new Color(100,149,238));
}
/*public void mouseReleased(MouseEvent e) {
}*/
}
public static void main(String[] args) {
new InitQQ();
}
}
链接:网盘链接
提取码:ywf6
将图片与该类放在同一包下即可