1.登录页面
package com.ff.chat.chatclient.frame;
import com.ff.chat.chatclient.bean.User;
import com.ff.chat.chatclient.dao.LoginDao;
import com.mysql.cj.jdbc.Driver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.sql.*;
import java.util.ArrayList;
public class LoginFrame extends JFrame {
JTextField accountField = null;
//创建窗口
public void creatFrame(){
this.setTitle("聊天窗口");
this.setSize(400,400);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
/*
* 创建一个4行1列的面板
* */
JPanel jp = new JPanel(new GridLayout(4,1));
//欢迎登陆面板
JPanel welcomePanel = new JPanel();
JLabel welcomLabel = new JLabel("欢迎登陆");
welcomePanel.add(welcomLabel);
//账号面板
JPanel accountPanel = new JPanel();
JLabel accountLabel = new JLabel("账号");
accountField = new JTextField(15);
accountPanel.add(accountLabel);
accountPanel.add(accountField);
//密码面板
JPanel passwordPanel = new JPanel();
JLabel passwordLabel = new JLabel("密码");
JPasswordField passwordField = new JPasswordField(15);
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
//登录按钮面板
JPanel btnPanel = new JPanel();
JButton loginBtn = new JButton("登录");
JButton regBtn = new JButton("注册");
btnPanel.add(loginBtn);
btnPanel.add(regBtn);
jp.add(welcomePanel);
jp.add(accountPanel);
jp.add(passwordPanel);
jp.add(btnPanel);
this.add(jp);
this.setVisible(true);
//组件绑定事件监听
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获得账号密码
String account = accountField.getText();
String password = new String(passwordField.getPassword());
if(account.length() == 0){
JOptionPane.showMessageDialog(null,"账号不能为空",
"操作提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(password.length() == 0){
JOptionPane.showMessageDialog(null,"密码不能为空",
"操作提示",JOptionPane.WARNING_MESSAGE);
return;
}
//与数据库交互
LoginDao loginDao = new LoginDao();
try{
User user = loginDao.checkLogin(account,password);
if(user!=null){
//连接服务器,创建Socket对象
try {
Socket socket = new Socket("127.0.0.1",9998);//该地址为要连接的服务器的地址
new ClientFrame(socket,user.getName()).creatFrame();
dispose(); //释放当前登录窗口
} catch (IOException ioException) {
ioException.printStackTrace();
JOptionPane.showMessageDialog(null,"服务器连接失败",
"操作提示",JOptionPane.WARNING_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(null,"密码或账号输入错误!",
"操作提示",JOptionPane.WARNING_MESSAGE);
}
}catch (Exception throwables){
throwables.printStackTrace();
JOptionPane.showMessageDialog(null,"数据库连接失败!",
"操作提示",JOptionPane.WARNING_MESSAGE);
}
}
});
//注册绑定事件监听
regBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new RegFrame().creatFrame();
dispose();
}
});
}
}
与数据库交互部分
package com.ff.chat.chatclient.dao;
import com.ff.chat.chatclient.bean.User;
import com.mysql.cj.jdbc.Driver;
import java.sql.*;
public class LoginDao {
public User checkLogin(String account,String password){
User user = new User();