里面有备注的需看自己的数据库和MySQL的密码和表进行修改名称。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import com.mysql.*;
public class login extends Jframe implements ActionListener {
Jframe jf;
JLabel jl1,jl2;
JPanel p1,p2;
JLabel jl3,jl4;
JButton jb1,jb2;
JTextField jtf1;
JPasswordField pswf;
JRadioButton rad1,rad2,rad3;
Container content;
static String url="jdbc:mysql://localhost:3306/databasework?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";//databasework为自己建好的数据库名
static String driver="com.mysql.cj.jdbc.Driver";
static String user="root";
static String password="12345";//安装mysql设定好的密码
public login()//登录界面
{
jf=new Jframe();
content=this.getContentPane();
content.setLayout(null);
p1 = new JPanel();
jl1=new JLabel("用户类别:");
jl1.setBounds(new Rectangle(150,150, 150, 60));
jl1.setFont(new Font("黑体",Font.BOLD,25));
rad1=new JRadioButton("学生",true);
rad2=new JRadioButton("教师");
rad3=new JRadioButton("管理员");
rad1.setFont(new Font("黑体",Font.BOLD,14));
rad2.setFont(new Font("黑体",Font.BOLD,14));
rad3.setFont(new Font("黑体",Font.BOLD,14));
ButtonGroup group=new ButtonGroup();
group.add(this.rad1);
group.add(this.rad2);
group.add(this.rad3);
p1.add(this.rad1);
p1.add(this.rad2);
p1.add(this.rad3);
p1.setBounds(new Rectangle(280,150, 200, 60));
jl3=new JLabel("账号:");
jl3.setBounds(new Rectangle(200,200, 100, 60));
jl3.setFont(new Font("黑体",Font.BOLD,25));
jtf1=new JTextField();
jtf1.setBounds(new Rectangle(270, 210, 250,40 ));
jtf1.setFont(new Font("黑体",Font.BOLD,25));
jl4=new JLabel("密码:");
jl4.setBounds(new Rectangle(200, 280,100, 60));
jl4.setFont(new Font("黑体",Font.BOLD,25));
pswf=new JPasswordField();
pswf.setBounds(new Rectangle(270, 290,250,40) );
pswf.setFont(new Font("",Font.BOLD,25));
p2=new JPanel();
jl2=new JLabel("系统登录界面");
jl2.setBounds(new Rectangle(250, 0, 300, 100));
jl2.setFont(new Font("黑体",Font.BOLD,40));
content.add(jl2);
content.add(jl1);
content.add(p1);
content.add(jl3);
content.add(jl4);
content.add(jtf1);
content.add(pswf);
jb1=new JButton("登录");
jb1.setPreferredSize(new Dimension(100,60));
jb1.setFont(new Font("黑体",Font.BOLD,25));
jb1.addActionListener(this);
jb2=new JButton("退出");
jb2.setPreferredSize(new Dimension(100,60));
jb2.setFont(new Font("黑体",Font.BOLD,25));
jb2.addActionListener(this);
jf.add(content);
p2.add(jb1);
p2.add(jb2);
jf.add(p2,BorderLayout.SOUTH);
jf.pack();
jf.show();
jf.setTitle("登录界面");
jf.setSize(800,500);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new login();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String sql=null;
Class.forName(driver);
Connection con=DriverManager.getConnection(url,user,password);
DefaultButtonModel m1=(DefaultButtonModel) rad1.getModel();
DefaultButtonModel m2=(DefaultButtonModel) rad2.getModel();
DefaultButtonModel m3=(DefaultButtonModel) rad3.getModel();
if(m1.getGroup().isSelected(m1)) {
sql="select * from student";//student为databasework数据库中的表,以下teacher和administrator也一样
}else if(m2.getGroup().isSelected(m2)) {
sql="select * from teacher";
}else if(m3.getGroup().isSelected(m3)) {
sql="select * from administrator";
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(e.getSource() == jb1)
{
String num1;
String num2;
num1 = jtf1.getText();
num2 = pswf.getText();
while(rs.next()) {
if(num1.equals("")||num2.equals("")) {
JOptionPane.showMessageDialog(null, "账号或 密码不能为空","提示",2);
return;
}
if(rs.getString("username").equals(num1))
{
if(rs.getString("password").equals(num2))
{
JOptionPane.showMessageDialog(null, "登陆成功","提示",2);
jf.dispose();
if(m1.getGroup().isSelected(m1))
{
new StudentUI(rs.getString("username"));
return;
}
else if(m2.getGroup().isSelected(m2))
{
new TeacherUI(rs.getString("username"));
return;
}
else if(m3.getGroup().isSelected(m3))
{
new AdministratorUI();
return;
}
}
else {JOptionPane.showMessageDialog(null, "密码错误","提示",2);
jtf1.setText("");
pswf.setText("");
return;}
}
}
{
JOptionPane.showMessageDialog(null, "账号不存在","提示",2);
jtf1.setText("");
pswf.setText("");
}
}
rs.close();
con.close();
}catch(SQLException el) {
el.printStackTrace();
}
catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == jb2)
{
System.exit(0);
}
}
}
运行的界面:



