loginframe.java
来自「学籍管理系统:利用JAVA的界面对MS SQL进行操作,实现了学生学籍管理系统基」· Java 代码 · 共 176 行
JAVA
176 行
package E4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.io.File;
import java.io.*;
import java.util.StringTokenizer;
import java.sql.*;
//表STUDENT(sno(主键),sname,sex,class,age,address,tel,password) IDTABLE(ID,PASSWORD,MARK)
//登陆界面
public class LoginFrame implements ActionListener,ItemListener{
JFrame fam;
JPanel contentPane;
JLabel nameLabel = new JLabel("用户",JLabel.CENTER);
JLabel passwordLabel = new JLabel("密码",JLabel.CENTER);
JLabel userCategoryLabel = new JLabel("用户类型",JLabel.CENTER);
TextField nameTextField = new TextField("system");
JPasswordField passwordField =new JPasswordField();
JButton loginBtn = new JButton();
JButton exitBtn = new JButton();
Choice choice = new Choice();
int mark=0;//标识用户类别
frameForCustomer ffc;
framForAdmin ffa;
proceeSQL psql;
public LoginFrame(){
psql = new proceeSQL();
psql.openSQL();
fam = new JFrame("登陆");
fam.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//取得窗口面板
contentPane = new JPanel();
//定义窗口面板的布局
//定义窗口的大小和标题
fam.setSize(250,180);
fam.setLocation(350,240);
fam.setResizable(false);
//初始化注册
// rgs=new register(fam,"注册用户",false);
ffc=new frameForCustomer(fam,"用户信息",false);
ffa=new framForAdmin(fam,"系统管理员",false);
//定义标签标题、字符大小和位置
nameLabel.setText("学号(帐号):");
passwordLabel.setText("密码:");
userCategoryLabel.setText("用户类别:");
//定义按钮的标题、动作字符串、字符大小、位置和加入 ,动作接收器
loginBtn.setText("登录");
loginBtn.addActionListener(this);
exitBtn.setText("退出");
exitBtn.addActionListener(this);
//定义下拉列表框图的内容、第一个显示项和位置
choice.addItem("管理员");
choice.addItem("用户");
choice.addItemListener(this);
nameTextField.setEditable(false);
//为面板加入各个控件
contentPane.add(nameLabel,null);
contentPane.add(nameTextField,null);
contentPane.add(passwordLabel,null);
contentPane.add(passwordField,null);
contentPane.add(userCategoryLabel,null);
contentPane.add(choice,null);
contentPane.add(loginBtn,null);
contentPane.add(exitBtn,null);
contentPane.setLayout(new GridLayout(4,2,9,9));
fam.add(contentPane);
fam.setVisible(true);
}
public static void main(String args []){
new LoginFrame();
}
protected void processWindowEvent(WindowEvent e){
if(e.getID()==WindowEvent.WINDOW_CLOSED){
psql.closeSQL();
System.exit(0);
}
}
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==choice){
String temp;
temp=choice.getSelectedItem();
if(temp.equals("用户")){
mark=1;
nameTextField.setEditable(true);
nameTextField.setText("");
}
else if(temp.equals("管理员")){
mark=0;
nameTextField.setEditable(false);
nameTextField.setText("system");
}
}
}
void refreshForCustomer(){
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==loginBtn ){
String tempName=nameTextField.getText();
String tempPassword=String.valueOf(passwordField.getPassword());
if(nameTextField.getText().equals(""))
{
JOptionPane.showMessageDialog(fam, "学号或帐号不能为空!");
return;
}
//创建用户检测类
int result=psql.checkUser(tempName,tempPassword,mark);
switch(result){
case 0://管理员
ffa.setDisplay(true);
passwordField.setText("");
ffa.sst.pfc.dfc.setEnable(true);
break;
case 1://用户,从数据库中提取相应的值
ffc.setID(tempName);
// ffc.setValueById();
passwordField.setText("");
psql.SearchStudentDetailForCustormer(ffc.cd.StudentDetail,tempName);
psql.SearchStuInforForCustomer(ffc.ci.studentInfor,tempName);
psql.SearchCourseBySno(ffc.cc.tm.content,tempName);
ffc.cd.setValueDisplay();
ffc.ci.setValueDisplay();
ffc.cd.setId(tempName);//给学籍的ID传值
ffc.cc.setId(tempName);//给选课表表的ID传值
ffc.lg.setId(tempName);//给成绩表的ID传值
ffc.lg.reFresh();//刷新成绩表
ffc.cc.reFresh();//刷新选课表
ffc.cc.dfc.setEnable(false); //屏蔽某些按钮
ffc.setDisplay(true);
break;
case -1://用户检验不通过
JOptionPane.showMessageDialog(null, "帐号或密码有误!");
passwordField.setText("");
break;
}
}
if(e.getSource()==exitBtn){
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?