📄 login.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
//import javax.swing.JComponent;
class loglogin extends JFrame implements ActionListener
{
JTextField nametext;
JPasswordField paswtext;
JLabel namelabel;
JLabel paswlabel;
JButton login;
JButton cancel;
JButton b1;
JButton b2;
public loglogin()
{
super("用户登录界面");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
b2 = new JButton(" 取消");
b1 = new JButton("登录");
b1.addActionListener(this);
b2.addActionListener(this);
nametext = new JTextField(8);
paswtext = new JPasswordField(8);
namelabel = new JLabel("用户名:");
paswlabel = new JLabel("密码:");
p1.setLayout(new GridLayout(2,2));
Container ct = getContentPane();
ct.setLayout(new BorderLayout());
ct.add(p1,BorderLayout.NORTH);
ct.add(p2,BorderLayout.SOUTH);
p1.add(namelabel);
p1.add(nametext);
p1.add(paswlabel);
p1.add(paswtext);
p2.setLayout(new FlowLayout());
p2.add(b1);
p2.add(b2);
setVisible(true);
setBounds(200,200,200,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
checklogin();
}
if(e.getSource()==b2)
{
dispose();
new login();
}
}
public void checklogin()
{
boolean flag = true;
contest c =new contest();
String command="select * from userinfo ";
Statement sm=null;
ResultSet rs=null;
try
{
sm=c.getConnection().createStatement();
rs=sm.executeQuery(command);
while(rs.next())
{
if(rs.getString(1).trim().equals(nametext.getText()))
{
flag = false;
if(paswtext.getText().equals(rs.getString(2).trim()))
{
JOptionPane.showMessageDialog(null,"用户登录成功!","",2);
break;
}
else
{
JOptionPane.showMessageDialog(null,"密码不正确,请重新登录!","",0);
paswtext.setText("");
break;
}
}
}
if(flag)
{
nametext.setText("");
paswtext.setText("");
JOptionPane.showMessageDialog(null,"用户名不正确,请重新登录!","",0);
}
sm.close();
c.getConnection().close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class contest
{
public static final String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=userinfo";
public static final String user="SA";
public static final String password="admin";
private Connection con=null;
public Connection getConnection()
{
try
{
Class.forName(drivername);
con=DriverManager.getConnection(url,user,password);
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
return con;
}
}
class rejlogin extends JFrame implements ActionListener
{
JTextField nametext;
JPasswordField paswtext;
JLabel namelabel;
JLabel paswlabel;
JButton login;
JButton cancel;
JButton b1;
JButton b2;
public rejlogin()
{
super("用户注册界面");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
b1 = new JButton("注册");
b2 = new JButton(" 取消");
b1.addActionListener(this);
b2.addActionListener(this);
nametext = new JTextField(8);
paswtext = new JPasswordField(8);
namelabel = new JLabel("用户名:");
paswlabel = new JLabel("密码:");
p1.setLayout(new GridLayout(2,2));
Container ct = getContentPane();
ct.setLayout(new BorderLayout());
ct.add(p1,BorderLayout.NORTH);
ct.add(p2,BorderLayout.SOUTH);
p1.add(namelabel);
p1.add(nametext);
p1.add(paswlabel);
p1.add(paswtext);
p2.setLayout(new FlowLayout());
p2.add(b1);
p2.add(b2);
setVisible(true);
setBounds(400,300,280,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
getrset();
nametext.setText("");
paswtext.setText("");
}
else if(e.getSource()==b2)
{
dispose();
new login();
}
}
public void getrset()
{
contest c = new contest();
String command1="INSERT INTO userinfo VALUES("+"'"+nametext.getText()+"'"+","+"'"+paswtext.getText()+"'"+")";
String command2 ="select * from userinfo ";
Statement sm=null;
ResultSet rs=null;
if(!nametext.getText().equals(""))
{
if(!paswtext.getText().equals(""))
{
try
{
sm=c.getConnection().createStatement();
rs=sm.executeQuery(command2);
while(rs.next())
{
if(nametext.getText().equals(rs.getString(1).trim()))
{
JOptionPane.showMessageDialog(null,"该用户名已经存在,请换名","",0);
return;
}
}
sm.executeUpdate(command1);
JOptionPane.showMessageDialog(null,"用户注册成功,是否继续?","",3);
sm.close();
c.getConnection().close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
else
JOptionPane.showMessageDialog(null,"密码不能为空,请重新注册!","",0);
}
else
JOptionPane.showMessageDialog(null,"用户名不能为空,请重新注册!","",0);
}
}
public class login extends JFrame implements ActionListener
{
JButton rej;
JButton log;
JButton del;
JButton can;
JLabel l1;
JLabel l2;
JLabel l3;
public login()
{
super("用户主界面");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
rej = new JButton("注册");
can = new JButton(" 取消");
log = new JButton("登录");
del = new JButton("删除");
rej.addActionListener(this);
can.addActionListener(this);
log.addActionListener(this);
del.addActionListener(this);
ImageIcon im = new ImageIcon("D:\\mn.gif");
l1= new JLabel(im,0);
l2= new JLabel("厚德载物");
l3= new JLabel("自强不息");
p1.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p5.setLayout(new FlowLayout());
p1.add(l1);
p2.add(rej);
p2.add(log);
p3.add(del);
p3.add(can);
p4.add(l2);
p5.add(l3);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
add(p3,BorderLayout.SOUTH);
add(p4,BorderLayout.EAST);
add(p5,BorderLayout.WEST);
setVisible(true);
setBounds(350,350,300,320);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==rej)
{
dispose();
new rejlogin();
}
if(e.getSource()==log)
{
dispose();
new loglogin();
}
if(e.getSource()==del)
{
dispose();
//new dellogin();
}
if(e.getSource()==can)
{
System.exit(0);
}
}
public static void main(String[] args)
{
new login();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -