java简易学生管理系统---登录界面(一)
login部分
// 内容包含简单的增删该查以及用户登录界面
// 技术有限,大佬勿喷---
// 此部分不小心将登录跟注册写在一起了。嘤嘤嘤~
中间部分将注册写成了内部类,本应该分开写的,只是练习一下基础语法
此部分在swing注释比较详细,之后就省略了哦
package StudentMajor; import java.awt.*; import java.awt.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import javax.swing.*; public class Login { private JFrame jf;//窗口 private JPanel jp1,jp2,jp3,jp4;//面板 private JLabel jlb1,jlb2,jlb3;//标签 private JButton jb1,jb2,jb3;//按钮 private JTextField jtf;//文本 private JPasswordField jpf;//密码 private JRadioButton jrb1,jrb2;//选项(点)按钮 private ButtonGroup bg; //控制点的选择(按钮) final private File filestu = new File("e:\\abc\\file\\Student.txt");//学生文件 final private File filetea = new File("e:\\abc\\file\\Teacher.txt");//教师文件 public Login() { } private class RegisterInfo { private JFrame jfr;//窗口 private JPanel jp1,jp2,jp3,jp4;//面板 private JLabel jlb1,jlb2,jlb3;//标签 private JButton jb1,jb2,jb3;//按钮 private JTextField jtf;//文本 private JPasswordField jpf;//密码 private JRadioButton jrb1,jrb2;//选项(点)按钮 private ButtonGroup bg; //控制点的选择(按钮) public RegisterInfo() { init(); } public void init() { if(!filestu.exists()) try { filestu.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if(!filetea.exists()) try { filetea.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //<1>设置主页面 jfr = new JFrame("学生管理系统"); jfr.setSize(300, 250);//窗体大小 jfr.setLocationRelativeTo(null);//居中显示 jfr.setLayout(new FlowLayout());//设置布局 jfr.setResizable(false); //设置窗口不能改变大小 //一初始化就有面板 jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp4 = new JPanel(); //(1)设置标签 jlb1 = new JLabel("用户名"); jlb2 = new JLabel("密 码"); jlb3 = new JLabel("身 份"); //(2)设置按钮 jb1 = new JButton("确定"); jb2 = new JButton("重置"); jb3 = new JButton("返回"); jrb1 = new JRadioButton("教师"); jrb2 = new JRadioButton("学生"); bg = new ButtonGroup(); bg.add(jrb1); bg.add(jrb2); jrb2.setSelected(true);//默认学生按钮 //(3)创建文本框 jtf = new JTextField(10); //(4)密码框 jpf = new JPasswordField(10); //<2>添加部件(添加到面板) jp1.add(jlb1); jp1.add(jtf); jp2.add(jlb2); jp2.add(jpf); jp3.add(jb1); jp3.add(jb2); jp3.add(jb3); jp4.add(jlb3); jp4.add(jrb1); jp4.add(jrb2); //(1)将组件都添加到窗体上 jfr.add(jp1); jfr.add(jp2); jfr.add(jp4); jfr.add(jp3); //<3>添加窗体事件 myEvent(); //<4>设置窗体可见 jfr.setVisible(true); } public void myEvent() { //(1)窗体关闭 jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //(2)监听---确定 jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(jtf.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "请输入用户名", "提示信息", JOptionPane.WARNING_MESSAGE); return; }else if(jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "请输入密码", "提示信息", JOptionPane.WARNING_MESSAGE); return; } boolean flag = false; if(jrb1.isSelected()) { flag = WriteInFile(1,jtf.getText(),jpf.getText()); }else { flag = WriteInFile(2,jtf.getText(),jpf.getText()); } if(flag) { JOptionPane.showMessageDialog(null, "注册成功", "提示信息", JOptionPane.WARNING_MESSAGE); //jfr.setVisible(false);//dispose() //jf.setVisible(true); jfr.dispose();//关掉当前页面 new Login().init();;//打开新的页面 }else { JOptionPane.showMessageDialog(null, "注册失败,请重新注册!", "提示信息", JOptionPane.ERROR_MESSAGE); jtf.setText(""); jpf.setText(""); } } }); //重置按钮 jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtf.setText(""); jpf.setText(""); } }); //返回 jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jfr.setVisible(false); jf.setVisible(true); } }); } } public void init() { if(!filestu.exists()) try { filestu.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if(!filetea.exists()) try { filetea.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //<1>设置主页面 jf = new JFrame("学生管理系统"); jf.setSize(300, 250);//窗体大小 jf.setLocationRelativeTo(null);//居中显示 jf.setLayout(new FlowLayout());//设置布局 jf.setResizable(false); //设置窗口不能改变大小 //一初始化就有面板 jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp4 = new JPanel(); //(1)设置标签 jlb1 = new JLabel("用户名"); jlb2 = new JLabel("密 码"); jlb3 = new JLabel("权 限"); //(2)设置按钮 jb1 = new JButton("登录"); jb2 = new JButton("重置"); jb3 = new JButton("注册"); jrb1 = new JRadioButton("教师"); jrb2 = new JRadioButton("学生"); bg = new ButtonGroup(); bg.add(jrb1); bg.add(jrb2); jrb2.setSelected(true);//默认学生按钮 //(3)创建文本框 jtf = new JTextField(10); //(4)密码框 jpf = new JPasswordField(10); //<2>添加部件(添加到面板) jp1.add(jlb1); jp1.add(jtf); jp2.add(jlb2); jp2.add(jpf); jp3.add(jb1); jp3.add(jb2); jp3.add(jb3); jp4.add(jlb3); jp4.add(jrb1); jp4.add(jrb2); //(1)将组件都添加到窗体上 jf.add(jp1); jf.add(jp2); jf.add(jp4); jf.add(jp3); //<3>添加窗体事件 myEvent(); //<4>设置窗体可见 jf.setVisible(true); } //窗体事件 public void myEvent() { //(1)窗体关闭 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //(2)监听---登录按钮 jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { userChoice(); } }); //重置按钮 jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtf.setText(""); jpf.setText(""); } }); //注册按钮 jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //jf.setVisible(false); jf.dispose(); new RegisterInfo(); } }); } //登录提示信息 public void showinfo(int num) { boolean flag = false; if(jtf.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "请输入用户名", "提示信息", JOptionPane.WARNING_MESSAGE); return; }else if(jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "请输入密码", "提示信息", JOptionPane.WARNING_MESSAGE); return; } if(num==1) { flag = readFileOfStudent(filetea,jtf.getText(),jpf.getText()); }else if(num==2) { flag = readFileOfStudent(filestu,jtf.getText(),jpf.getText()); } if(flag&&num==1) { JOptionPane.showMessageDialog(null, "登录成功", "提示信息", JOptionPane.WARNING_MESSAGE); jf.dispose(); new TeacherUser().init(); }else if(flag&&num==2) { JOptionPane.showMessageDialog(null, "登录成功", "提示信息", JOptionPane.WARNING_MESSAGE); jf.dispose(); new StudentUser().init(jtf.getText()); }else { JOptionPane.showMessageDialog(null, "用户名或密码错误!", "提示信息", JOptionPane.ERROR_MESSAGE); jtf.setText(""); jpf.setText(""); } } //用户选择 public void userChoice() { int num=-1; if(jrb2.isSelected()) { num=2; }else { num=1; } //System.out.println(num); showinfo(num); } //将信息写入文件 public boolean WriteInFile(int num,String user,String pwd) { if(user==null || pwd==null) return false; if(num==2) { if(WriteToFile(filestu,user,pwd)) { return true; } }else if(num==1) { if(WriteToFile(filetea,user,pwd)) { return true; } }else { //System.out.println("没有任何学生和老师,请重新注册!"); return false; } return false; } public boolean WriteToFile(File file,String user,String pwd) { if(user==null || pwd == null) return false; if(file==null) { System.out.println("没有文件"); return false; } BufferedWriter bufw = null; try { bufw = new BufferedWriter(new FileWriter(file,true)); if(readFileOfStudent(file,user,pwd)) { JOptionPane.showMessageDialog(null, "输入已存在!", "提示信息", JOptionPane.ERROR_MESSAGE); //jtf.setText(""); //jpf.setText(""); return false; } bufw.write(user+" "+pwd); bufw.newLine(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(bufw!=null) bufw.close(); } catch (IOException e) { System.out.println("文件关闭失败"); } } return true; } //读文件 public boolean readFileOfStudent(File file,String name,String pwd) { BufferedReader bufr = null; //System.out.println(name+pwd); boolean flag = false; try { bufr = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { new RuntimeException("该文件不存在!"); } String len = null; String infoReg = " +"; String[]s = null; try { while((len = bufr.readLine())!=null) { s = len.split(infoReg); if(s[0].equals(name)&&s[1].equals(pwd)) { return true; } } } catch (IOException e) { e.printStackTrace(); } return false; } public static void main(String[] args) { new Login().init(); } }
效果展示:
// 注册界面