⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginframe.java

📁 利用JAVA的界面对MS SQL进行操作,实现了学生管理系统基本功能:登陆,录入,查询,删除,修改,内置程序详细讲解.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
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();
	JButton newMember = new JButton("注册新用户");
	Choice choice = new Choice();
	
	int mark=0;//标识用户类别
	
	register rgs;
	framForCustomer 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 framForCustomer(fam,"用户信息",false);
    	ffa=new framForAdmin(fam,"系统管理员",false);
    	
    	
    	//定义标签标题、字符大小和位置
    	nameLabel.setText("学号(帐号):");
    	passwordLabel.setText("密码:");
    	userCategoryLabel.setText("用户类别:");   	
    	//定义按钮的标题、动作字符串、字符大小、位置和加入 ,动作接收器
    	loginBtn.setText("登录");
    	loginBtn.addActionListener(this);
    	
    	exitBtn.setText("退出");
    	exitBtn.addActionListener(this);
    	newMember.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.add(new JLabel("试用版本2.0",JLabel.CENTER));
    	contentPane.add(newMember);
    	contentPane.setLayout(new GridLayout(5,2,9,9));
 
        fam.add(contentPane);
        fam.setVisible(true);       		
   }
  
    public static void main(String args []){
    	LoginFrame one =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");
   	 		}
   	 	
	 	 	 		
   	 	}
   	}
    
    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("");
    		break;
    		case 1://用户
    		ffc.setID(tempName); 
    	    ffc.setValueById(); 
    	   	passwordField.setText("");  		  
    		ffc.setDisplay(true);
    		break;
    		case -1://用户检验不通过
    	    JOptionPane.showMessageDialog(null, "帐号或密码有误!");
    	    passwordField.setText("");
    		break;
    	}

    	//JOptionPane.showMessageDialog(null,message);
    	}
    	
    	if(e.getSource()==exitBtn){
    		System.exit(0);
    	}
    	
    	if(e.getSource()==newMember){
    	  rgs.btnChange("注册");
          rgs.setDisplay(true);
    		
    	}
    	
    	
    }
}


//注册
class register implements ActionListener,TextListener,ItemListener
 { 

    JDialog jda;
    JPanel pnl;
    int t_Age[]=new int[20];

    TextField name,id,homeAddress,telNum;
    JButton btn_receave,btn_back;
    Choice choice;
    Choice Classes;
    JRadioButton jrb_male=new JRadioButton("男");
    JRadioButton jrb_female=new JRadioButton("女");    
    ButtonGroup sex= new ButtonGroup();
   	JPasswordField newPasswordField =new JPasswordField();//新密码
	JPasswordField confirmNewPasswordField =new JPasswordField();//确认新密码
	
	 String iId="",iName="",iSex="",iClass="",iAddtress="",iphoneNum="",ipassWord="";
     String newPass="",conPass="";
    int iAge;
	
	proceeSQL psql;
	framForCustomer ffc;
    
   register(JFrame f,String s,boolean b)
   {
   	jda=new JDialog(f,s,b);  
    jda.setSize(300,450); 
    jda.setLocation(350,200); 
    
    psql = new proceeSQL();
    ffc = new framForCustomer(f,"用户信息",b);
   
         
     jda.addWindowListener(new WindowAdapter()
        {public void windowClosing(WindowEvent e)
         {
            jda.setVisible(false);
         }});
       
       for(int i=10;i<30;i++)
       t_Age[i-10]=i;
       
     pnl=new JPanel();
     choice = new Choice();
     Classes = new Choice();
     
     for(int i=0;i<4;i++)
     {
     	Classes.addItem("计科"+(i+1)+"班");
     }
     
     for(int i=0;i<20;i++)
         choice.addItem(String.valueOf(t_Age[i])); 
     name=new TextField("",8);
     id=new TextField("",8);
     homeAddress=new TextField("",8);
     telNum=new TextField("",8);
     btn_receave=new JButton("注册");
     btn_back=new JButton("返回"); 
     pnl=new JPanel();

     sex.add(jrb_male);
     sex.add(jrb_female);
     pnl.add(jrb_male);
     pnl.add(jrb_female);
     
     name.addTextListener(this);
     id.addTextListener(this);
     homeAddress.addTextListener(this);
     telNum.addTextListener(this);
     jrb_male.addItemListener(this);
     jrb_female.addItemListener(this);
     choice.addItemListener(this);
     Classes.addItemListener(this);
     
     jda.add(new JLabel("学号",JLabel.CENTER));
     jda.add(id);
     jda.add(new JLabel("姓名",JLabel.CENTER));
     jda.add(name);
     jda.add(new JLabel("姓别",JLabel.CENTER));
     jda.add(pnl);
     jda.add(new JLabel("班级",JLabel.CENTER));
     jda.add(Classes);
     jda.add(new JLabel("年龄",JLabel.CENTER));
     jda.add(choice);
     jda.add(new JLabel("家庭住址",JLabel.CENTER));
     jda.add(homeAddress);
     jda.add(new JLabel("电话号码",JLabel.CENTER)); 
     jda.add(telNum);
     jda.add(new JLabel("创建密码:",JLabel.CENTER));
     jda.add(newPasswordField);
     jda.add(new JLabel("确认密码:",JLabel.CENTER));
     jda.add(confirmNewPasswordField);
     jda.add(btn_receave);
     jda.add(btn_back);
     btn_receave.addActionListener(this);
     btn_back.addActionListener(this);
     jda.setLayout(new GridLayout(10,2,20,9));
     jda.setResizable(false);
   }
   
   void setDisplay(Boolean tf){
   	jda.setVisible(tf);
   }
   
   public void actionPerformed(ActionEvent e){
   	
    //监听录取按扭
     if(e.getSource()==btn_receave){ 
      	
    	if(name.getText().equals("")||id.getText().equals(""))
    	{
    	JOptionPane.showMessageDialog(null, "学号或姓名不能为空!");
    	return;
    	}
    	
    	newPass=new String(newPasswordField.getPassword());
    	conPass=new String(confirmNewPasswordField.getPassword());
    	if(!newPass.equals(conPass)){
    			JOptionPane.showMessageDialog(null, "密码不一致!");
    			return;
    	}
    	
    	ipassWord=new String(newPasswordField.getPassword());    	
    	psql.setValue(iId,iName,iSex,iClass,iAge,iAddtress,iphoneNum,ipassWord);
    	int mark=psql.insertStudentDetail();

    	psql.insertIDTABLEofCustomer();
    	
    	if((btn_receave.getText()).equals("注册")&&mark==1){
    		ffc.setValue(iId,iName,iSex,iClass,iAge,iAddtress,iphoneNum);
    		JOptionPane.showMessageDialog(null, "注册成功!");
    		jda.setVisible(false);
    		ffc.setDisplay(true);
    	}
    
    	
    	name.setText("");
    	id.setText("");
    	homeAddress.setText("");
    	telNum.setText("");
    	newPasswordField.setText("");
    	confirmNewPasswordField.setText("");
    }  
    
    else if(e.getSource()==btn_back){
    	jda.setVisible(false);
    }
   	   	
   	}
   	
   	    public void textValueChanged(TextEvent e){
    	if(e.getSource()==id)
    	{
    		iId=id.getText();
    	}
    	
    	else if(e.getSource()==name){
    		iName = name.getText();
    	}
    	
        else if(e.getSource()==homeAddress){
        	iAddtress=homeAddress.getText();
        }
        
        else if(e.getSource()==telNum){
        	iphoneNum=telNum.getText();
        }   	
	 }
	 
	 public void itemStateChanged(ItemEvent e){
	 	
   	 	if(e.getItemSelectable()==jrb_male){
   	 		iSex="男";
   	 	}
   	 	
   	 	else if(e.getItemSelectable()==jrb_female){
   	 		iSex="女";
   	 	}
   	 	
   	 	else if(e.getSource()==choice){
   	 		
   	 		iAge = choice.getSelectedIndex()+10;
   	 	
   	 	}
   	 	
   	 	else if(e.getItemSelectable()==Classes){
   	 		iClass = Classes.getSelectedItem();
   	 	
   	 		
   	 	}
   	 }
   	 
   	 public void btnChange(String b){
   	 	btn_receave.setText(b);
   	 }
}

//管理员界面
class framForAdmin implements ActionListener{
	Insert ist;	
	updatePassword up; 
	JDialog jdl;
	JPanel jpl=new JPanel();
	JLabel welcomLabel = new JLabel("欢迎您,管理员",JLabel.CENTER);
    
	JButton updateInfo = new JButton("查找,浏览,修改信息");
	JButton updatePassword = new JButton("修改密码");
	JButton insertInfo = new JButton("添加学生信息");
	JButton exitBtn = new JButton("退出");
	Statist  stt;
	
	framForAdmin(JFrame f,String s,boolean b){
		
		jdl=new JDialog(f,s,b); 
		ist=new Insert(f,"学生管理系统",b);
		up=new updatePassword(f,"管理员密码修改",b);
		stt=new Statist(f,"浏览",b);
		
        jdl.setSize(380,180); 
        jdl.setLocation(350,240); 
        jdl.setResizable(false);
         
        jdl.addWindowListener(new WindowAdapter()
        {public void windowClosing(WindowEvent e)
         {
            jdl.setVisible(false);
         }});
         
		jdl.add(welcomLabel,BorderLayout.NORTH);
		jpl.add(new JLabel(""));
		jpl.add(new JLabel(""));		
		jpl.add(insertInfo);
        jpl.add(updateInfo);
        jpl.add(updatePassword);
	    jpl.add(exitBtn); 
	    jpl.setLayout(new GridLayout(3,2,5,5));
	    jdl.add(jpl);
	    insertInfo.addActionListener(this);
	    updateInfo.addActionListener(this);
	    updatePassword.addActionListener(this);
	    exitBtn.addActionListener(this);
	    
	    ist.btn_statist.addActionListener(this);//在这里监听插入的按钮
	}

   void setDisplay(Boolean tf){
   	jdl.setVisible(tf);
   }
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==insertInfo){
			ist.setDisplay(true);
		}
		
		else if(e.getSource()==updateInfo||e.getSource()==ist.btn_statist){
			stt.reFresh();
			stt.setDisplay(true);
		}
		
		else if(e.getSource()==updatePassword){
			
			up.setID("system");
			up.setState(0);
			up.setDisplay(true);
		}
		
		else if(e.getSource()==exitBtn){
			jdl.setVisible(false);
		}
		}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -