📄 loginframe.java
字号:
//用户登陆界面
import javax.swing.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.Properties;
public class LoginFrame extends JFrame implements ActionListener{
static final long serialVersionUID=80;
JLabel[] label=new JLabel[6];
JTextField text[]=new JTextField[4];
JPasswordField passwordText;
JButton[] button=new JButton[3];
JButton getbutton;
Checkbox auto;
Toolkit tool;
Image myimage;
Socket socket;
Connection con;
Statement sql;
URL audiourl;
AudioClip clip;
Dimension screenSize;
DataInputStream in;DataOutputStream out;
String validate,reply;
LoginFrame(String s){
super(s);
setSize(350,225);
setResizable(false);
tool=getToolkit();
audiourl=getClass().getResource("/sound/Hi.wav");
URL url = getClass().getResource("/images/USER.GIF");
if(url!=null){
myimage=tool.getImage(url);
setIconImage(myimage);
}
auto=new Checkbox("自动登录");
label[0]=new JLabel("服务器");
label[1]=new JLabel("用户名");
label[2]=new JLabel("密码");
label[3]=new JLabel("端口");
label[4]=new JLabel("验证码");
label[5]=new JLabel("验证码位置");
label[5].setForeground(Color.RED);
text[0]=new JTextField(18);
text[0].setText("127.0.0.1");
text[1]=new JTextField(17);//用户名
text[2]=new JTextField(5);//端口
text[3]=new JTextField(5);
text[3].setText("1433");
passwordText=new JPasswordField(17);
getbutton=new JButton("获取验证码");
getbutton.setBackground(Color.LIGHT_GRAY);
getbutton.addActionListener(this);
button[0]=new JButton("登录");
button[1]=new JButton("注册");
button[2]=new JButton("取消");
for(int i=0;i<3;i++){
button[i].setBackground(Color.LIGHT_GRAY);
button[i].addActionListener(this);
}
Container c=getContentPane();
c.setBackground(Color.ORANGE);
c.setLayout(null);
c.add(label[0]);
label[0].setBounds(10,5,40,30);
c.add(text[0]);
text[0].setBounds(50,9,160,22);
c.add(label[3]);
label[3].setBounds(220,5,40,30);
c.add(text[3]);
text[3].setBounds(260,9,60,22);
c.add(label[1]);
label[1].setBounds(10,40,40,30);
c.add(text[1]);
text[1].setBounds(50,44,160,22);
c.add(label[2]);
label[2].setBounds(20,75,30,30);
c.add(passwordText);
passwordText.setBounds(50,79,160,22);
c.add(getbutton);
getbutton.setBounds(220,78,100,24);
c.add(auto);
auto.setBounds(240,40,80,30);
c.add(label[4]);
label[4].setBounds(10,110,40,30);
c.add(text[2]);
text[2].setBounds(50,114,160,22);
c.add(label[5]);
label[5].setBounds(240,110,80,30);
c.add(button[0]);
button[0].setBounds(30,145,70,30);
c.add(button[1]);
button[1].setBounds(130,145,70,30);
c.add(button[2]);
button[2].setBounds(240,145,70,30);
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=getSize();
setLocation((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height) / 2);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
sendMessage("cancel");
System.exit(0);
}
});
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(this,e);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==getbutton){
validate=null;
if(text[0].getText()==null){
JOptionPane.showMessageDialog(this,"服务器不能为空","警告对话框",JOptionPane.WARNING_MESSAGE);
}
else{
GetValidate mythread=new GetValidate();
try{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
mythread.start();
Thread.sleep(2000);
if(validate!=null){
label[5].setText(validate);
}
else{
label[5].setText("验证码");
JOptionPane.showMessageDialog(this,"获取验证码失败");
}
setCursor(null);
}
catch(InterruptedException e3){
}
}
}
else if(e.getSource()==button[2]){
sendMessage("cancel");
System.exit(0);
}
else if(e.getSource()==button[1]){
String s=text[0].getText().trim();
String t=text[3].getText().trim();
String url="jdbc:microsoft:sqlserver://"+s+":"+t+";DatabaseName=Library";
try{
con=DriverManager.getConnection(url,"warcrafthero","331310981");
if(con!=null){
sql=con.createStatement();
new RegisterFrame("注册",sql);
}
else{
JOptionPane.showMessageDialog(this,"连不上服务器");
}
}
catch(Exception e1){
JOptionPane.showMessageDialog(this,e);
}
}
else{
reply=null;
String server=text[0].getText().trim();
String port=text[3].getText().trim();
String usertemp=text[1].getText().trim();
String user="'"+usertemp+"'";
String passwordtemp=new String(passwordText.getPassword());
String password="'"+passwordtemp+"'";
String validate=text[2].getText().trim();
if(server.length()==0||port.length()==0||user.length()==0||password.length()==0||validate.length()==0){
JOptionPane.showMessageDialog(this,"文本框不能为空","警告对话框",JOptionPane.WARNING_MESSAGE);
}
else{
String url="jdbc:microsoft:sqlserver://"+server+":"+port+";DatabaseName=Library";
String query="SELECT UserPurview FROM User_Info WHERE UserName="+user+" AND UserPassword="+password;
try{
if(con==null){
con=DriverManager.getConnection(url,"warcrafthero","331310981");
}
if(sql==null){
sql=con.createStatement();
}
ResultSet rt=sql.executeQuery(query);
if(rt.next()){
String purview=rt.getString(1).trim();
sendMessage(validate);
PassValidate pass=new PassValidate();
pass.start();
Thread.sleep(2000);
pass.interrupt();
if(reply.equals("pass")){
sendMessage("pass");
String uptime="'"+new Timestamp(System.currentTimeMillis()).toString()+"'";
String s="INSERT INTO User_LoginLog VALUES ("+user+","+uptime+","+null+")";
sql.executeUpdate(s);
try{
socket.close();
}
catch(IOException e3){
JOptionPane.showMessageDialog(this,"与服务器断开拉");
}
dispose();
String name="AutoLogin.properties";
File file=new File(name);
if(auto.getState()){
Properties props = new Properties();
props.setProperty("Address",server);
props.setProperty("Port",port);
props.setProperty("User",usertemp);
props.setProperty("Password",passwordtemp);
if(file.exists()){
file.delete();
}
try{
PrintStream out=new PrintStream(new FileOutputStream(name));
props.list(out);
out.close();
}
catch(IOException e2){
}
}
else{
if(file.exists()){
file.delete();
}
}
new MainFrame("邑大图书管理系统",purview,usertemp,passwordtemp,uptime,con,server,port);
if(audiourl!=null){
clip=Applet.newAudioClip(audiourl);
clip.play();
}
}
else{
JOptionPane.showMessageDialog(this,"登录超时或验证码出错,请重新登录");
}
}
else{
JOptionPane.showMessageDialog(this,"用户名或密码错误","警告对话框",JOptionPane.WARNING_MESSAGE);
sendMessage("Notpass");
PassValidate pass=new PassValidate();
pass.start();
try{
Thread.sleep(1000);
}
catch(InterruptedException e3){
}
pass.interrupt();
if(reply!=null){
label[5].setText(reply);
}
}
}
catch(SQLException e2){
JOptionPane.showMessageDialog(this,"连不上服务器");
label[5].setText("验证码");
}
catch(InterruptedException e3){
}
}
}
}
//获取验证码
private String getValidate(String s,int t){
try{
if(socket==null){
socket=new Socket(s,t);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
if(out!=null){
out.writeUTF("need number");
String validate=in.readUTF();
return validate;
}
}
catch(IOException e){
return null;
}
return null;
}
//发送信息给服务器
private void sendMessage(String s){
if(socket!=null){
try{
out.writeUTF(s);
}
catch(IOException e){
JOptionPane.showMessageDialog(this,"你已经与服务器断开连接","警告对话框",JOptionPane.WARNING_MESSAGE);
}
}
}
//获取验证码线程
private class GetValidate extends Thread{
public void run(){
validate=getValidate(text[0].getText(),4331);
}
}
//接收验证码线程
private class PassValidate extends Thread{
public void run(){
try{
reply=in.readUTF();
}
catch(IOException e){
JOptionPane.showMessageDialog(null,"你与服务器断开连接");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -