本人一直想找时间系统整理一下之前做过的一些实验,便于后续用到的时候可以尽快的使用,po出来也便于大家交流学习,有问题欢迎交流指正,与诸君共勉!
这次是JAVA的一次复现QQ登录加一些自己设计的大作业,主要是JAVA课程锻炼java的GUI能力还有一些数据操作(数据库、文件操作)。实现了注册(自动生成QQ号)、登录(含记住密码、自动登录)。
实际上用java做GUI相比于很多框架来说是很不方便的。
实现效果
0.数据说明
QQ注册账户信息存入数据库qaccountsdb的account表中;
登录界面“自动登录”、“记住密码”是否选中信息分别存储在fileAutologin.txt、filerememberPsw.txt文件中
1.登陆界面
鼠标放置于相应按钮显示说明字样,鼠标移动至、点击按钮有图标变化:
单击“注册账号”按钮进入注册界面:
注册成功,生成五位随机不重复QQ号,账户数据存入数据库,弹出提示信息:
直接点击注册:
勾选记住密码,点击登录:
将自动登录、记住密码等信息传入文件:
并弹出欢迎界面:
再次打开登陆界面时,已记住密码:
勾选自动登录则本次手动登陆后,下次进入登录界面可自动登录
3.欢迎界面
不同时间段登录,展示不同界面图案及欢迎语:
以下分别为:
4:00~10:00
10:00~14:00
14:00~18:00
18:00~23:00
23:00~4:00 各时段登入欢迎界面会显示不同的欢迎语和图片
其中下方文字为滚动进入
例如
特殊节日及纪念日:
十月一日、一月一日、12月13日更换不同皮肤
哈哈哈哈GUI主打一个花里胡哨(bushi)~~
代码部分
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Random;
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.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.SwingConstants;
import javax.swing.Timer;
/**
* 登录界面类
* @author AGATHA
* @date 2022年10月22日
* @todo
*/
class QLoginFrame extends JFrame{
private JPanel panNorth,panWest,panCenter,panEast,panSouth;//四个panel
private JLabel labRegister ;//注册账号标签
private JLabel labForget;//找回密码标签
private JTextField textAccount;//账号文本框
private JPasswordField textPassword;//密码框
private JButton miniButton; //最小化按钮
private JButton closeButton; //关闭窗口按钮
private JButton loginButton;
private JButton tabButton;
JPopupMenu popupMenu;//状态选择菜单
private JButton stateButton; //状态选择按钮
private JCheckBox autoLogin;//自动登录复选框
private JCheckBox rememberPsw;//记住密码复选框
private static boolean autoLoginFlag = false; //自动登录复选框是否选中标志 默认未选中
private static boolean rememberPswFlag = false; //记住密码复选框是否选中标志 默认未选中
private static final String BGIMAGE_PATH = "src/loginBG12_1.png"; //登陆界面背景图路径
private static final String STATEBUTTON_PATH = "src/dot_40.png";//状态按钮背景图路径
//private static final String STATEBUTTON_PRESSES_PATH = "src/dot_pressed.png";
private static final String states[] = {"我在线上","Q我吧","离开","忙碌","请勿打扰","隐身"};
ImageIcon icons[] = {new ImageIcon(STATEBUTTON_PATH),new ImageIcon("src/Q我吧_38.png"),new ImageIcon("src/离开_38.png"),
new ImageIcon("src/忙碌_38.png"),new ImageIcon("src/请勿打扰_38.png"),new ImageIcon("src/隐身_38.png")};
private final int LOGINWIDTH = 640; // 登录窗口的宽度
private final int LOGINHEIGHT = 500;// 登录窗口的高度
private final int COOLINGTIME = 5;// 自动登录等待时间(期间可取消登录)
private int xOn = 0,yOn = 0;
private String url = "jdbc:mysql://localhost:3306/qaccountsdb?useUnicode=true&characterEncoding=utf8&useSSL=true";
private String username="root"; //mysql
private String password="123456"; //mysql
private File fileAutologin = new File("E:/eclipseWorkspace/QQRegister LoginGUI", "fileAutologin.txt");
private File filerememberPsw = new File("E:/eclipseWorkspace/QQRegister LoginGUI", "filerememberPsw.txt"); //保存自动登录、记住密码信息的文件
/**
* 登陆界面构造函数
* @param s
*/
QLoginFrame(String s){
super(s);
this.setSize(LOGINWIDTH, LOGINHEIGHT);
this.setLayout(new BorderLayout());
setLocationRelativeTo(null); //窗口居中
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setUndecorated(true);//取消默认边框
this.setResizable(false);//大小不可更改
/** 处理窗口拖动事件*/
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
xOn = e.getX();//记录鼠标按下时的坐标
yOn = e.getY();
}
});
/** 处理窗口拖动事件*/
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int xOnScreen = e.getXOnScreen();
int yOnScreen = e.getYOnScreen();
int xx = xOnScreen - xOn;
int yy = yOnScreen - yOn;
QLoginFrame.this.setLocation(xx, yy);//设置拖拽后,窗口的位置
}
});
//四个面板的初始化
panNorth = panNorthCreate();
panWest = panWestCreate();
panCenter = panCenterCreate();
panEast = panEastCreate();
panSouth = panSouthCreate();
this.add(panNorth, BorderLayout.NORTH);
this.add(panWest, BorderLayout.WEST);
this.add(panCenter, BorderLayout.CENTER);
this.add(panEast, BorderLayout.EAST);
this.add(panSouth, BorderLayout.SOUTH);
this.setVisible(true);
}
/**
* 得到北部面板
* @return
*/
public JPanel panNorthCreate() {
JPanel pan = new JPanel();
//ImageIcon background;
pan.setLayout(null);
pan.setPreferredSize(new Dimension(0, 260));
ImageIcon background = new ImageIcon(BGIMAGE_PATH); //创建背景图片
ImageIcon icon_11 = new ImageIcon("src/CloseButton_2.png"); //关闭窗口按钮图标
ImageIcon icon_12 = new ImageIcon("src/24gl-minimization_2.png"); //最小化按钮图标
ImageIcon icon_21 = new ImageIcon("src/coloseButton_Pressed_1.png"); //关闭按钮按下图标
ImageIcon icon_22 = new ImageIcon("src/mini_Button_Pressed_1.png"); //最小化按钮按下图标
ImageIcon icon_3 = new ImageIcon("src/tab_1.png"); //下拉图标
JLabel lab = new JLabel(background); //背景图片添加到标签
lab.setBounds(0, 0, LOGINWIDTH, 260);
lab.setOpaque(false); //将标签设置为不可视
closeButton = new JButton(icon_11);
miniButton = new JButton(icon_12);
tabButton = new JButton(icon_3);
closeButton.setRolloverIcon(icon_21);//设置按钮按下效果
closeButton.setPressedIcon(icon_21);
miniButton.setRolloverIcon(icon_22);
miniButton.setPressedIcon(icon_22);
closeButton.setBounds(LOGINWIDTH-42,2,30,30);//设置按钮尺寸
miniButton.setBounds(LOGINWIDTH-77,2,30,30);
tabButton.setBounds(LOGINWIDTH-112,2,30,30);
closeButton.setToolTipText("关闭");//设置按钮的鼠标放置显示
miniButton.setToolTipText("最小化");
tabButton.setToolTipText("设置");
closeButton.setContentAreaFilled(false);//设置按钮透明
miniButton.setContentAreaFilled(false);
tabButton.setContentAreaFilled(false);
closeButton.setFocusPainted(false);//隐藏focus焦点状态
miniButton.setFocusPainted(false);
tabButton.setFocusPainted(false);
closeButton.setBorderPainted(false);//隐藏border
miniButton.setBorderPainted(false);
tabButton.setBorderPainted(false);
pan.add(closeButton);
pan.add(miniButton);
pan.add(tabButton);
pan.add(lab);
//为按钮添加事件
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
miniButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
QLoginFrame.this.setState(Frame.ICONIFIED);
}
});
return pan;
}
/**
* 得到西部面板
* @return
*/
public JPanel panWestCreate() {
JPanel pan = new JPanel();
pan.setLayout(null);
pan.setBackground(Color.WHITE);
pan.setPreferredSize(new Dimension(180,0));
ImageIcon headPortrait = new ImageIcon("src/HeadPortrait_1_c1.png");
JLabel lab = new JLabel(headPortrait);
lab.setBounds(10,0,180,180);
ImageIcon icon_4 = new ImageIcon(STATEBUTTON_PATH);//状态图标
//ImageIcon icon_5 = new ImageIcon(STATEBUTTON_PRESSES_PATH);
stateButton = new JButton(icon_4);
stateButton.setBounds(130,130,30,30);
stateButton.setToolTipText("在线状态:"+states[0]);
stateButton.setContentAreaFilled(false);//设置按钮透明
stateButton.setFocusPainted(false);//隐藏focus焦点状态
stateButton.setBorderPainted(false);//隐藏border
pan.add(stateButton);
stateButton.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
showPopupMenu(e.getComponent(), e.getX(), e.getY(),e);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
pan.add(lab);
return pan;
}
/**
* 得到中部面板
* @return
*/
public JPanel panCenterCreate() {
JPanel pan = new JPanel();
pan.setBackground(Color.WHITE);
pan.setLayout(null);
//账号文本框
pan.setPreferredSize(new Dimension(0, 180));
textAccount = new JTextField("QQ号码",20); //账号文本框
textAccount.setOpaque(false); //设置账号文本框透明
textAccount.setBackground(new Color(0, 0, 0, 0));// 设置文本框透明背景色
textAccount.setFont(new Font("宋体",Font.BOLD,22));
textAccount.setForeground(Color.GRAY);
textAccount.setBounds(5, 30, 300, 43);// 设置文本框大小
textAccount.addFocusListener(new textAccountListener());
textPassword = new JPasswordField("密码",20); //密码文本框
textPassword.setOpaque(false); //设置密码文本框透明
//textPassword.setBorder(BorderFactory.createLineBorder(Color.BLACK,0)); //设置边框宽度为0
textPassword.setBackground(new Color(0, 70, 80, 10));// 设置文本框透明背景色
textPassword.setFont(new Font("宋体",Font.BOLD,24));
textPassword.setBounds(5,77,300,43);// 设置文本框大小
//记住密码复选框
if(readTxt(fileAutologin).equals("选中")) {
autoLoginFlag = true;
}
if(readTxt(filerememberPsw).equals("选中")) {
rememberPswFlag = true;
}
autoLogin = new JCheckBox("自动登录",autoLoginFlag);
rememberPsw = new JCheckBox("记住密码",rememberPswFlag);
autoLogin.setBounds(20,130,100,20);
rememberPsw.setBounds(190,130,100,20);
autoLogin.setBackground(Color.WHITE);
rememberPsw.setBackground(Color.WHITE);
autoLogin.setForeground(Color.gray);
rememberPsw.setForeground(Color.gray);
autoLogin.setFont(new Font("宋体",Font.BOLD,16));
rememberPsw.setFont(new Font("宋体",Font.BOLD,16));
autoLogin.setBackground(Color.WHITE);
rememberPsw.setBackground(Color.WHITE);
pan.add(textAccount);
pan.add(textPassword);
pan.add(autoLogin);
pan.add(rememberPsw);
try {
textPassword.addFocusListener(new PasswordFieldListener(textPassword, rememberPsw, "密码"));
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return pan;
}
/**
* 得到东部面板
* @return
*/
public JPanel panEastCreate() {
JPanel pan = new JPanel();
pan.setLayout(null);
pan.setBackground(Color.WHITE);
pan.setPreferredSize(new Dimension(130, 0));
Font f = new Font("宋体", Font.BOLD, 16);
Color c = new Color(100,149,238);
labRegister = new JLabel("注册账号");
labForget = new JLabel("找回密码");
labRegister.setBounds(0, 35, 100, 20);
labForget.setBounds(0, 85, 100, 20);
labRegister.setFont(f);
labForget.setFont(f);
labRegister.setForeground(c);
labForget.setForeground(c);
labRegister.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标形状为HAND_CURSOR
labForget.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
pan.add(labRegister);
pan.add(labForget);
return pan;
}
/**
* 得到南部面板
* @return
*/
public JPanel panSouthCreate() {
JPanel pan = new JPanel();
pan.setPreferredSize(new Dimension(0, 68));
pan.setBackground(Color.WHITE);
pan.setLayout(null);
ImageIcon addFriends = new ImageIcon("src/AddFriends_1.png");
ImageIcon qrCode = new ImageIcon("src/QRcode_1.png");
ImageIcon loginbt = new ImageIcon("src/loginBtBacground.png");
ImageIcon loginbt_pressed = new ImageIcon("src/loginBtBacground_pressed.png");
JLabel lab1 = new JLabel(addFriends);
JLabel lab2 = new JLabel(qrCode);
loginButton = new JButton("登录",loginbt);
loginButton.setRolloverIcon(loginbt_pressed);
loginButton.setPressedIcon(loginbt_pressed);
loginButton.setBorder(BorderFactory.createLineBorder(Color.BLACK,0)); //设置边框宽度为0(去掉边框)
loginButton.setContentAreaFilled(false); // 设置按钮透明
loginButton.setFocusPainted(false); //取消点击后的焦点
loginButton.setVerticalTextPosition(SwingConstants.CENTER); //设置按钮字体位置
loginButton.setHorizontalTextPosition(SwingConstants.CENTER);
loginButton.setForeground(new Color(255, 255, 255)); //文字颜色
loginButton.setFont(new Font("微软雅黑",Font.BOLD,16));
lab1.setBounds(10, 13, 40, 40);
lab2.setBounds(580, 13, 40, 40);
loginButton.setBounds(200, 0, 260, 45);
loginButton.addActionListener(new loginButtonListener());
labRegister.addMouseListener(new labRegisterListener());
pan.add(lab1);
pan.add(loginButton);
pan.add(lab2);
return pan;
}
/**登陆状态选择弹出式菜单*/
public void showPopupMenu(Component c, int x, int y,MouseEvent e1) {
popupMenu = new JPopupMenu();
JMenuItem items[] = new JMenuItem[6];
for(int i = 0;i < 6;i++)
items[i] = new JMenuItem(states[i]) ;
for(int i = 0;i < 6;i++)
popupMenu.add(items[i]);
popupMenu.show(c, x, y);
for(int i = 0;i < 6;i++)
items[i].addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// 鼠标点击(按下并抬起)
for(int i = 0;i < 6;i++) {
if(e.getSource() == items[i]) {
stateButton.setToolTipText("在线状态:"+states[i]);
stateButton.setIcon(icons[i]); //将状态按钮设置为对应状态图标
}
}
}
@Override
public void mousePressed(MouseEvent e) {
// 鼠标按下
}
@Override
public void mouseReleased(MouseEvent e) {
// 鼠标释放
for(int i = 0;i < 6;i++) {
if(e.getSource() == items[i]) {
stateButton.setToolTipText("在线状态:"+states[i]);
stateButton.setIcon(icons[i]); //将状态按钮设置为对应状态图标
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
// 鼠标进入组件区域
}
@Override
public void mouseExited(MouseEvent e) {
// 鼠标离开组件区域
}
});
}
/**传入txt文件 读取txt文件
* @param file
* @return 返回读取到的内容
*/
public static String readTxt(File file) {
if(file.isFile() && file.exists()){
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String text = null;
while((text = bufferedReader.readLine()) != null){
sb.append(text);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**文本框焦点监听*/
public class textAccountListener implements FocusListener{
@Override
public void focusGained(FocusEvent e) {
//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
if(textAccount.getText().equals("QQ号码")){
textAccount.setText(""); //将提示文字清空
textAccount.setBorder(BorderFactory.createLineBorder(new Color(125,226,244),2));//100,149,238
}
}
@Override
public void focusLost(FocusEvent e) {
//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
if (textAccount.getText().equals("")){
textAccount.setForeground(Color.gray); //将提示文字设置为灰色
textAccount.setText("QQ号码"); //显示提示文字
textAccount.setBorder(BorderFactory.createLineBorder(Color.gray));
}
}
}
/**定义密码框内焦点事件监听 */
public class PasswordFieldListener implements FocusListener{
private String str;
private JPasswordField text0;
private JCheckBox rememberPsw;
PasswordFieldListener(JPasswordField text, JCheckBox rememberPsw, String str) throws SQLException, ClassNotFoundException {
this.text0 = text;
this.rememberPsw = rememberPsw;
this.str = str;
if (rememberPsw.isSelected()) {
Class.forName("com.mysql.cj.jdbc.Driver");
//.连接成功,数据库对象 Connection
Connection connection;
connection = DriverManager.getConnection(url,username,password);
//4.执行SQL对象Statement,执行SQL的对象
Statement statement;
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//5.执行SQL的对象去执行SQL,
String sql0 = " SELECT * FROM account ;";
ResultSet resultSet = statement.executeQuery(sql0);
resultSet.last(); //把指针移动到最后一行
int count = resultSet.getRow();
resultSet.first(); //在还是用结果集的情况下,一定要还原指针到第一行去
System.out.println(count);
String sql1 = " SELECT * FROM account where id = "+count+";";
ResultSet resultSet1 = statement.executeQuery(sql1);
if(resultSet1.next()) {
textAccount.setText(resultSet1.getString("accountnum"));
textPassword.setText(resultSet1.getString("psw"));
textPassword.setForeground(Color.BLACK);
}
}
else {
text.setText(str);
text.setEchoChar((char) (0));//不设置回显
text.setForeground(Color.gray);
}
if(autoLogin.isSelected() && rememberPsw.isSelected()) {
//loginButton.doClick();
}
}
@Override
public void focusGained(FocusEvent e) {
if (textPassword.getText().equals(str)) {
text0.setText("");
text0.setEchoChar('*'); // 将回显设置为'*'
text0.setForeground(Color.BLACK);
text0.setBorder(BorderFactory.createLineBorder(new Color(125,226,244),2));
}
}
@Override
public void focusLost(FocusEvent e) {
if (textPassword.getText().equals("")) {
text0.setEchoChar((char) (0));
text0.setForeground(Color.gray);
text0.setText(str);
text0.setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
}
}
/**注册账号 标签点击事件*/
public class labRegisterListener extends MouseAdapter{
public void mouseClicked(MouseEvent e) {
QRegisterFrame rf = new QRegisterFrame("注册QQ");
}
public void mouseEnetered(MouseEvent e) {
}
}
/**
* 登录按钮事件
* @author AGATHA
* @date 2022年10月24日
* @todo 与数据库信息比对后 登录并保存自动登录、记住密码信息至文件
*/
public class loginButtonListener implements ActionListener{
private String usernameQ,passwordQ;
private String strAutologin,strRememberme;//写入自动登录、记住密码文件的信息
@Override
public void actionPerformed(ActionEvent e){
if((!textAccount.getText().equals("QQ号码"))&&(!textPassword.getText().equals("密码"))) {
usernameQ = textAccount.getText();
passwordQ = textPassword.getText();
try {
if(isindatabase(usernameQ,passwordQ)) {
//欢迎界面*************************************************************
//System.out.println("find Account successfully.");
QWelcomeFrame test = new QWelcomeFrame("AGATHA");
//************************以下为向文件保存自动登录、记住密码信息
if(autoLogin.isSelected()) {
autoLoginFlag = true;
strAutologin = "选中";
}else strAutologin = "未选中";
if(rememberPsw.isSelected()) {
rememberPswFlag = true;
strRememberme = "选中";
}else strRememberme = "未选中";
fileAutologin.createNewFile(); // 创建文件
//向文件写入内容
byte btAutologin[] = new byte[1024];
btAutologin = strAutologin.getBytes();
FileOutputStream in = new FileOutputStream(fileAutologin);
in.write(btAutologin, 0, btAutologin.length);
in.close();
//System.out.println("写入文件成功");
filerememberPsw.createNewFile(); // 创建文件
//向文件写入内容
byte btrememberPsw[] = new byte[1024];
btrememberPsw = strRememberme.getBytes();
FileOutputStream in2 = new FileOutputStream(filerememberPsw);
in2.write(btrememberPsw, 0, btrememberPsw.length);
in2.close();
}else {
//账号或密码输入错误
//System.out.println("Account not found.");
JOptionPane.showMessageDialog(null, "账号或密码输入错误","提示信息",JOptionPane.WARNING_MESSAGE,new ImageIcon("src/QQ_1_1.png"));
}
} catch (ClassNotFoundException | SQLException | IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}else {
//账号或密码未输入
JOptionPane.showMessageDialog(null, "请输入账号和密码","提示信息",JOptionPane.WARNING_MESSAGE,new ImageIcon("src/QQ_1_1.png"));
}
}
}
/**
* 判断账户是否已注册 且密码是否正确 与数据库比对
* @param usernameQ QQ账号
* @param passwordQ QQ密码
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public boolean isindatabase(String usernameQ,String passwordQ) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
//.连接成功,数据库对象 Connection
Connection connection;
connection = DriverManager.getConnection(url,username,password);
//4.执行SQL对象Statement,执行SQL的对象
Statement statement;
statement = connection.createStatement();
//5.执行SQL的对象去执行SQL,
String sql0 = " SELECT * FROM account where accountnum = '"+usernameQ+"';";
System.out.println(sql0);
ResultSet resultSet = statement.executeQuery(sql0);
if(resultSet.next()){
System.out.println(resultSet.getString("psw"));
if(resultSet.getRow() != 0 && resultSet.getString("psw").equals(passwordQ)) {
return true;
}
}
return false;
}
}
/**
* 注册界面类
* @author AGATHA
* @date 2022年10月23日
* @todo 实现用户注册昵称、密码,自动生成六位随机无重复QQ号;并将账户存入数据库
*/
class QRegisterFrame extends JFrame{
private JPanel pan;
private JLabel labWelcome1 ;//欢迎文字标签1
private JLabel labWelcome2 ;//欢迎文字标签2
private JLabel labWelcome3 ;
private JTextField textAccount;//账号文本框
private JTextField textTelenum;//手机号码文本框
private JPasswordField textPassword;//密码框
private JButton miniButton; //最小化按钮
private JButton closeButton; //关闭窗口按钮
private JButton registerButton; //立即注册按钮
private JRadioButton agree; //协议勾选按钮
private final int REGISTERWIDTH = 900;//658
private final int REGISTERHEIGHT = 586;//429
private final String BACKGROUND_PATH = "src/bc_009_900.586.png";
private final String CLOSEBUTTON_PATH1 = "src/CloseButton_2.png";
private final String MINIBUTTON_PATH1 = "src/24gl-minimization_2.png";
private final String CLOSEBUTTON_PATH2 = "src/coloseButton_Pressed_1.png";
private final String MINIBUTTON_PATH2 = "src/mini_Button_Pressed_1.png";
private final String REGISTERBUTTON_PATH = "src/loginBtBacground.png";
private String url = "jdbc:mysql://localhost:3306/qaccountsdb?useUnicode=true&characterEncoding=utf8&useSSL=true";
private String username="root";
private String password="123456";
private int xOn, yOn, idnum;
QRegisterFrame(String s){
super(s);
this.setSize(REGISTERWIDTH, REGISTERHEIGHT);
this.setLocationRelativeTo(null); //窗口居中
pan = getRegisterPanel();
this.setContentPane(pan);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setUndecorated(true);//取消默认边框
this.setResizable(false);//大小不可更改
/** 处理窗口拖动事件*/
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
xOn = e.getX();//记录鼠标按下时的坐标
yOn = e.getY();
}
});
/** 处理窗口拖动事件*/
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int xOnScreen = e.getXOnScreen();
int yOnScreen = e.getYOnScreen();
int xx = xOnScreen - xOn;
int yy = yOnScreen - yOn;
QRegisterFrame.this.setLocation(xx, yy);//设置拖拽后,窗口的位置
}
});
this.setVisible(true);
}
/**
* 注册面板创建函数
* @return
*/
public JPanel getRegisterPanel() {
JPanel pan = new JPanel();
pan.setLayout(null);
pan.setPreferredSize(new Dimension(REGISTERWIDTH, REGISTERHEIGHT));
ImageIcon background = new ImageIcon(BACKGROUND_PATH); //创建背景图片
ImageIcon icon_11 = new ImageIcon(CLOSEBUTTON_PATH1); //关闭窗口按钮图标
ImageIcon icon_12 = new ImageIcon(MINIBUTTON_PATH1); //最小化按钮图标
ImageIcon icon_21 = new ImageIcon(CLOSEBUTTON_PATH2); //关闭按钮按下图标
ImageIcon icon_22 = new ImageIcon(MINIBUTTON_PATH2); //最小化按钮按下图标
ImageIcon loginbt = new ImageIcon("src/loginBtBacground.png");
ImageIcon loginbt_pressed = new ImageIcon("src/loginBtBacground_pressed.png");
JLabel lab = new JLabel(background); //背景图片添加到标签
lab.setBounds(0, 0, REGISTERWIDTH, REGISTERHEIGHT);
lab.setOpaque(false); //将标签设置为不可视
this.getLayeredPane().add(lab,new Integer(Integer.MIN_VALUE)); //背景设置为最底层
pan.setOpaque(false);
//**********************两个按钮各项设置*************************************
closeButton = new JButton(icon_11);
miniButton = new JButton(icon_12);
closeButton.setRolloverIcon(icon_21);//设置按钮按下效果
closeButton.setPressedIcon(icon_21);
miniButton.setRolloverIcon(icon_22);
miniButton.setPressedIcon(icon_22);
closeButton.setBounds(REGISTERWIDTH-42,2,30,30);//设置按钮尺寸
miniButton.setBounds(REGISTERWIDTH-77,2,30,30);
closeButton.setToolTipText("关闭");//设置按钮的鼠标放置显示
miniButton.setToolTipText("最小化");
closeButton.setContentAreaFilled(false);//设置按钮透明
miniButton.setContentAreaFilled(false);
closeButton.setFocusPainted(false);//隐藏focus焦点状态
miniButton.setFocusPainted(false);
closeButton.setBorderPainted(false);//隐藏border
miniButton.setBorderPainted(false);
//**********************其他组件设置******************************************
labWelcome1 = new JLabel("欢迎注册QQ");
labWelcome1.setFont(new Font("微软雅黑",Font.BOLD,35));
labWelcome1.setBounds(200, 10, 250, 100);
labWelcome2 = new JLabel("每一天,乐在沟通");
labWelcome2.setFont(new Font("微软雅黑",Font.BOLD,20));
labWelcome2.setBounds(200, 90, 200, 30);
labWelcome3 = new JLabel("Hi~");
labWelcome3.setFont(new Font("微软雅黑",Font.BOLD,50));
labWelcome3.setBounds(500, 50, 200, 70);
textAccount = new JTextField("昵称",20); //昵称文本框
textAccount.setFont(new Font("宋体",Font.BOLD,25));
textAccount.setForeground(Color.GRAY);
textAccount.setBounds(200, 180, 280, 40);// 设置文本框大小
textTelenum = new JTextField("手机号码",20); //账号文本框
textTelenum.setFont(new Font("宋体",Font.BOLD,22));
textTelenum.setForeground(Color.GRAY);
textTelenum.setBounds(200, 225, 280, 41);// 设置文本框大小
textPassword = new JPasswordField("密码",20); //密码文本框
textPassword.setFont(new Font("宋体",Font.BOLD,24));
textPassword.setBounds(200,271,280,41);// 设置文本框大小
agree = new JRadioButton("我已阅读并同意服务协议和QQ隐私保护指引");
agree.setBounds(200, 320, 300, 20);
agree.setOpaque(false);
agree.setForeground(Color.WHITE);
ImageIcon registerbt = new ImageIcon(REGISTERBUTTON_PATH);
registerButton = new JButton("立即注册",registerbt);
registerButton.setRolloverIcon(loginbt_pressed);
registerButton.setPressedIcon(loginbt_pressed);
registerButton.setBorder(BorderFactory.createLineBorder(Color.BLACK,0)); //设置边框宽度为0(去掉边框)
registerButton.setContentAreaFilled(false); // 设置按钮透明
registerButton.setFocusPainted(false); //取消点击后的焦点
registerButton.setVerticalTextPosition(SwingConstants.CENTER); //设置按钮字体位置
registerButton.setHorizontalTextPosition(SwingConstants.CENTER);
registerButton.setForeground(new Color(255, 255, 255)); //文字颜色
registerButton.setFont(new Font("微软雅黑",Font.BOLD,16));
registerButton.setBounds(200,350,270,40);
//***************添加组件*****************************************
pan.add(closeButton);
pan.add(miniButton);
pan.add(labWelcome1);
pan.add(labWelcome2);
pan.add(labWelcome3);
pan.add(textAccount);
pan.add(textPassword);
pan.add(textTelenum);
pan.add(agree);
pan.add(registerButton);
//*****************添加事件*************************************
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}); //为按钮添加事件
miniButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
QRegisterFrame.this.setState(Frame.ICONIFIED);
}
});
textAccount.addFocusListener(new textAccountListener());
textTelenum.addFocusListener(new textTelenumListener());
textPassword.addFocusListener(new textPasswordListener());
registerButton.addActionListener(new registerButtonListener());
return pan;
}
/**添加文本框焦点事件*/
public class textAccountListener implements FocusListener{
public void focusGained(FocusEvent e) {
//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
if(textAccount.getText().equals("昵称")){
textAccount.setText(""); //将提示文字清空
textAccount.setBorder(BorderFactory.createLineBorder(new Color(255,255,255)));
}
}
@Override
public void focusLost(FocusEvent e) {
//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
if (textAccount.getText().equals("")){
textAccount.setForeground(Color.gray); //将提示文字设置为灰色
textAccount.setText("昵称"); //显示提示文字
}
}
}
/**添加电话号码框焦点事件*/
public class textTelenumListener implements FocusListener{
@Override
public void focusGained(FocusEvent e) {
//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
if(textTelenum.getText().equals("手机号码")){
textTelenum.setText(""); //将提示文字清空
textTelenum.setBorder(BorderFactory.createLineBorder(new Color(100,149,238)));
}
}
@Override
public void focusLost(FocusEvent e) {
//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
if (textTelenum.getText().equals("")){
textTelenum.setForeground(Color.gray); //将提示文字设置为灰色
textTelenum.setText("手机号码"); //显示提示文字
}
}
}
/**添加密码框焦点事件*/
public class textPasswordListener implements FocusListener{
textPasswordListener() {
textPassword.setText("密码");
textPassword.setEchoChar((char) (0));//不设置回显 ????
textPassword.setForeground(Color.gray);
}
@Override
public void focusGained(FocusEvent e) {
if (textPassword.getText().equals("密码")) {
textPassword.setText("");
textPassword.setEchoChar('*'); // 将回显设置为'*'
textPassword.setForeground(Color.BLACK);
textPassword.setBorder(BorderFactory.createLineBorder(new Color(100,149,238)));
}
}
@Override
public void focusLost(FocusEvent e) {
if (textPassword.getText().equals("")) {
textPassword.setEchoChar((char) (0));
textPassword.setForeground(Color.gray);
textPassword.setText("密码");
}
}
}
/**添加立即注册按钮事件*/
public class registerButtonListener implements ActionListener{
private String usernameQ,passwordQ;
@Override
public void actionPerformed(ActionEvent e){
if((!textAccount.getText().equals("昵称"))&&(!textPassword.getText().equals("密码"))) {
usernameQ = textAccount.getText();
passwordQ = textPassword.getText();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//.连接成功,数据库对象 Connection
Connection connection;
connection = DriverManager.getConnection(url,username,password);
//4.执行SQL对象Statement,执行SQL的对象
Statement statement;
statement = connection.createStatement();
System.out.println("-==================--");
//5.执行SQL的对象去执行SQL
//String sql = "SELECT * FROM account where username = '"+usernameQ+"'";
String sql0 = " SELECT * FROM account where user_n = '"+usernameQ+"';";
System.out.println(sql0);
ResultSet resultSet = statement.executeQuery(sql0);
if(resultSet.getRow() == 0) {
int r = (new Random()).nextInt()%89999;//用于生成随机账号
idnum = 10000 + Math.abs(r);
String sql2 = "INSERT INTO account VALUES(null,"+"'"+usernameQ+"','"+passwordQ+"',"+idnum+");";
System.out.println(sql2);
PreparedStatement pstm = connection.prepareStatement(sql2);
pstm.executeUpdate();
pstm.close();
JOptionPane.showMessageDialog(null, "注册成功!您的QQ账号为"+idnum,"提示信息",JOptionPane.WARNING_MESSAGE,new ImageIcon("src/QQ_1_1.png"));
}
else {
JOptionPane.showMessageDialog(null, "昵称已存在","提示信息",JOptionPane.WARNING_MESSAGE,new ImageIcon("src/QQ_1_1.png"));
return;
}
statement.close();
connection.close();
} catch (SQLException | ClassNotFoundException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
else JOptionPane.showMessageDialog(null, "请输入昵称和密码","提示信息",JOptionPane.WARNING_MESSAGE,new ImageIcon("src/QQ_1_1.png"));
}
}
}
/**
* 欢迎界面类
* @author AGATHA
* @date 2022年10月24日
* @todo
*/
class QWelcomeFrame extends JFrame{
private JPanel pan;
private JLabel labWelcome1 ;//欢迎文字标签1
private JLabel labWelcome2 ;//欢迎文字标签2
private JLabel labWelcome3 ;
private JLabel labWelcome4 ;
private JLabel labWelcome5 ;
private JLabel labWelcome6 ;
private JLabel labWelcome7 ;
private JLabel labWelcome8 ;
private JButton miniButton; //最小化按钮
private JButton closeButton; //关闭窗口按钮
private JButton welcomeInButton; //欢迎进入QQ按钮
private final int REGISTERWIDTH = 440;//658
private final int REGISTERHEIGHT = 880;//429
private final String BACKGROUND_PATH = "src/bc_005_1.png";//背景图片路径
private final String BACKGROUND_PATH_NATIONALDAY = "src/guoqing_1.png";//国庆节背景图路径
private final String BACKGROUND_PATH_NEWYEARDAY = "src/元旦_1.png";//元旦节背景图路径
private final String BACKGROUND_PATH_1213 = "src/12.13_1.png";//918背景图路径
private final String CLOSEBUTTON_PATH1 = "src/CloseButton_2.png";
private final String MINIBUTTON_PATH1 = "src/24gl-minimization_2.png";
private final String CLOSEBUTTON_PATH2 = "src/coloseButton_Pressed_1.png";
private final String MINIBUTTON_PATH2 = "src/mini_Button_Pressed_1.png";
private final String REGISTERBUTTON_PATH = "src/loginBtBacground.png";
private int xOn, yOn;
/**构造函数*/
QWelcomeFrame(String username){
super(username);
JFrame frm = new Test84("WELCOME","WE ARE NOT ALONE.");
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
frm.pack();
frm.setVisible(true);
this.setSize(REGISTERWIDTH, REGISTERHEIGHT);
this.setLocationRelativeTo(null); //窗口居中
pan = getQWelcomeFrame(username);
this.setContentPane(pan);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setUndecorated(true);//取消默认边框
this.setResizable(false);//大小不可更改
/** 处理窗口拖动事件*/
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
xOn = e.getX();//记录鼠标按下时的坐标
yOn = e.getY();
}
});
/** 处理窗口拖动事件*/
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int xOnScreen = e.getXOnScreen();
int yOnScreen = e.getYOnScreen();
int xx = xOnScreen - xOn;
int yy = yOnScreen - yOn;
QWelcomeFrame.this.setLocation(xx, yy);//设置拖拽后,窗口的位置
}
});
this.setVisible(true);
}
/**
* 欢迎界面面板设置
* @param username 用户名
* @return
*/
public JPanel getQWelcomeFrame(String username){
JPanel pan = new JPanel();
ImageIcon background;
pan.setLayout(null);
pan.setPreferredSize(new Dimension(REGISTERWIDTH, REGISTERHEIGHT));
Date date = new Date();//获取登录日期 时间
int hours = date.getHours();
int month = date.getMonth();
int day = date.getDay();
boolean festivalFlag = false;
background = new ImageIcon(BACKGROUND_PATH); //创建背景图片
if(month == 10 && day == 1) {
background = new ImageIcon(BACKGROUND_PATH_NATIONALDAY); //创建背景图片国庆节
festivalFlag = true;
}
if(month == 12 && day == 13) {
background = new ImageIcon(BACKGROUND_PATH_1213); //创建背景图片918
festivalFlag = true;
}
if(month == 1 && day == 1) {
background = new ImageIcon(BACKGROUND_PATH_NEWYEARDAY);//元旦节背景图
festivalFlag = true;
}
ImageIcon icon_11 = new ImageIcon(CLOSEBUTTON_PATH1); //关闭窗口按钮图标
ImageIcon icon_12 = new ImageIcon(MINIBUTTON_PATH1); //最小化按钮图标
ImageIcon icon_21 = new ImageIcon(CLOSEBUTTON_PATH2); //关闭按钮按下图标
ImageIcon icon_22 = new ImageIcon(MINIBUTTON_PATH2); //最小化按钮按下图标
ImageIcon loginbt = new ImageIcon("src/loginBtBacground.png");
ImageIcon loginbt_pressed = new ImageIcon("src/loginBtBacground_pressed.png");
JLabel lab = new JLabel(background); //背景图片添加到标签
lab.setBounds(0, 0, REGISTERWIDTH, REGISTERHEIGHT);
lab.setOpaque(false); //将标签设置为不可视
this.getLayeredPane().add(lab,new Integer(Integer.MIN_VALUE)); //背景设置为最底层
pan.setOpaque(false);
//**********************两个按钮各项设置*************************************
closeButton = new JButton(icon_11);
miniButton = new JButton(icon_12);
closeButton.setRolloverIcon(icon_21);//设置按钮按下效果
closeButton.setPressedIcon(icon_21);
miniButton.setRolloverIcon(icon_22);
miniButton.setPressedIcon(icon_22);
closeButton.setBounds(REGISTERWIDTH-42,2,30,30);//设置按钮尺寸
miniButton.setBounds(REGISTERWIDTH-77,2,30,30);
closeButton.setToolTipText("关闭");//设置按钮的鼠标放置显示
miniButton.setToolTipText("最小化");
closeButton.setContentAreaFilled(false);//设置按钮透明
miniButton.setContentAreaFilled(false);
closeButton.setFocusPainted(false);//隐藏focus焦点状态
miniButton.setFocusPainted(false);
closeButton.setBorderPainted(false);//隐藏border
miniButton.setBorderPainted(false);
//**********************其他组件设置******************************************
//******根据登陆的时间(五个时间段)和日期(是否节日)推送不同的祝福语和欢迎语/图标*******************
String[] greetings = {"早上好,活力满满~","中午好,心情暖暖~","下午好,为你柔风徐徐",
"晚上好,今天辛苦啦~","夜深了,晚安~"};
ImageIcon[] icons = { new ImageIcon("src/树苗.png"),new ImageIcon("src/彩虹.png"),new ImageIcon("src/河流.png"),
new ImageIcon("src/落日.png"),new ImageIcon("src/星空.png"),};
ImageIcon[] icons1 = { new ImageIcon("src/树苗_80.png"),new ImageIcon("src/彩虹_80.80.png"),new ImageIcon("src/河流_80.80.png"),
new ImageIcon("src/落日_80.80.png"),new ImageIcon("src/stars_1.png"),};
ImageIcon[] icons2 = { new ImageIcon("src/树苗_110.png"),new ImageIcon("src/彩虹_110.png"),new ImageIcon("src/河流_110.png"),
new ImageIcon("src/落日_110.png"),new ImageIcon("src/星空_100.100.png"),};
int timeflag = -1;
if(hours >= 4 && hours <10 ) timeflag = 0;
if(hours >= 10 && hours <14 ) timeflag = 1;
if(hours >= 14 && hours <18 ) timeflag = 2;
if(hours >= 18 && hours <23 ) timeflag = 3;
if(hours >= 23 && hours <24 ) timeflag = 4;
if(hours >= 0 && hours <4 ) timeflag = 4;
labWelcome1 = new JLabel(greetings[timeflag]);
labWelcome1.setFont(new Font("微软雅黑",Font.BOLD,40));
labWelcome1.setBounds(20, 60, 600, 100);
labWelcome2 = new JLabel(username);
labWelcome2.setFont(new Font("微软雅黑",Font.BOLD,40));
labWelcome2.setBounds(20, 10, 300, 100);
labWelcome3 = new JLabel("每一天,乐在沟通");
labWelcome3.setFont(new Font("微软雅黑",Font.BOLD,20));
labWelcome3.setBounds(20, 300, 200, 30);
labWelcome6 = new JLabel("WELCOME TO QQ");
labWelcome6.setFont(new Font("微软雅黑",Font.BOLD,20));
labWelcome6.setBounds(200, 730, 200, 30);
labWelcome6.setForeground(Color.WHITE);
labWelcome4 = new JLabel(icons[timeflag]);
labWelcome4.setBounds(10, 100, icons[timeflag].getIconWidth(), icons[timeflag].getIconHeight());
labWelcome5 = new JLabel(icons1[timeflag]);
labWelcome5.setBounds(180, 190, icons1[timeflag].getIconWidth(), icons1[timeflag].getIconHeight());
labWelcome7 = new JLabel(icons2[timeflag]);
labWelcome7.setBounds(290, 120, icons2[timeflag].getIconWidth(), icons2[timeflag].getIconHeight());
//***************添加组件*****************************************
pan.add(closeButton);
pan.add(miniButton);
if(festivalFlag == false) {
pan.add(labWelcome1);
pan.add(labWelcome2);
pan.add(labWelcome3);
pan.add(labWelcome6);
pan.add(labWelcome4);
pan.add(labWelcome5);
pan.add(labWelcome7);
}
//*****************添加事件*************************************
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}); //为按钮添加事件
miniButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
QWelcomeFrame.this.setState(Frame.ICONIFIED);
}
});
return pan;
}
}
/**
* 欢迎界面滚动字幕
* @author AGATHA
* @date 2022年10月27日
* @todo
*/
class Test84 extends JFrame {
private Timer timer;
private JLabel view;
private JViewport window;
private int xOn,yOn;
public Test84(String title,String s) throws HeadlessException{
super(title);
initComponents(s);
this.setLocation(900,900);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e){
anchor = new Point();
anchor.x = -window.getExtentSize().width;
timer.start();
}
});
timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e){
animate();
}
});
timer.setInitialDelay(0);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setUndecorated(true);//取消默认边框
/** 处理窗口拖动事件*/
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
xOn = e.getX();//记录鼠标按下时的坐标
yOn = e.getY();
}
});
/** 处理窗口拖动事件*/
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int xOnScreen = e.getXOnScreen();
int yOnScreen = e.getYOnScreen();
int xx = xOnScreen - xOn;
int yy = yOnScreen - yOn;
Test84.this.setLocation(xx, yy);//设置拖拽后,窗口的位置
}
});
}
private void initComponents(String s)
{
view = new JLabel(s);
view.setFont(Font.decode("Dialog-BOLD-36"));
view.setForeground(Color.BLUE);
window = new JViewport();
window.setView(view);
getContentPane().add(window);
}
Point anchor;
private void animate()
{
Dimension extSize = window.getExtentSize();
Dimension viewSize = view.getPreferredSize();
anchor.x += 5;//设置移动的速度
window.setViewPosition(anchor);
if (anchor.x > viewSize.width)
anchor.x = -extSize.width;
}
}
/**测试类*/
public class QQRegisterTest {
public static void main(String args[]) {
QLoginFrame lr = new QLoginFrame("QQ");
//QWelcomeFrame test = new QWelcomeFrame("AGATHA");
}
}