import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Login implements ActionListener{
private JFrame app;
private JButton 登录,退出;
private JLabel jl1,jl2,jl3;
private JPanel jp1,jp2,jp3,jp4;
public static JTextField jt1;
private JPasswordField jpf1,jpf2;
private JDialog jd;
private int message=0;
public static ResultSet rs;
public Login(){
app=new JFrame("TankWar2.0");
app.setSize(350,200);
app.setLocation(360,240);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=app.getContentPane();
c.setLayout(new GridLayout(4,1));
jl1=new JLabel("用户账号");
jt1=new JTextField(10);
jp1=new JPanel();
jp1.add(jl1);
jp1.add(jt1);
c.add(jp1);
jl2=new JLabel("用户密码");
jpf1=new JPasswordField(10);
jp2=new JPanel();
jp2.add(jl2);
jp2.add(jpf1);
c.add(jp2);
jl3=new JLabel("确认密码");
jpf2=new JPasswordField(10);
jp3=new JPanel();
jp3.add(jl3);
jp3.add(jpf2);
c.add(jp3);
jd=new JDialog();
jd.setSize(340,80);
jd.setLocation(app.getX()+100,app.getY()+100);
jd.setLayout(new FlowLayout());
退出=new JButton("退出");
退出.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
}
);
登录=new JButton("登录");
登录.addActionListener(this);
jp4=new JPanel();
jp4.add(退出);
jp4.add(登录);
c.add(jp4);
app.setVisible(true);
jp1.setBackground(Color.PINK);
jp2.setBackground(Color.PINK);
jp3.setBackground(Color.PINK);
jp4.setBackground(Color.PINK);
app.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==登录){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/tankwar?characterEncoding=utf-8","root","hua");
String sql="select * from records";
Statement stmt = con.createStatement();
String st1=null;
String st2=null;
rs=stmt.executeQuery(sql);
while(rs.next()){
st1=rs.getString(1);
st2=rs.getString(2);
if((jt1.getText().equals(st1)) && (jpf1.getText().equals(st2))&&(jpf2.getText().equals(jpf1.getText()))){
message=1;
app.setVisible(false);
JOptionPane.showMessageDialog(app, "输入正确,欢迎使用本系统","系统提示",JOptionPane.INFORMATION_MESSAGE);
new FirstFrame().setVisible(true);
app.dispose();
con.close();
}
}
{
JOptionPane.showMessageDialog(null, "错误", "系统提示", JOptionPane.ERROR_MESSAGE);
}
}catch(Exception e2){
System.out.println(e2.getStackTrace());
}
}
}
public static void main(String[] args) {
new Login();
}
}