1 //package newpackage; 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.Vector; 6 7 public class StudentFrame extends JFrame { //建立一个登陆界面 8 Vector<Student> v = new Vector<Student>(); //建立一个动态数组的对象 9 // Student student; 10 StudentLogin student = new StudentLogin(); //建立一个atm对象 11 int num; 12 String num1; 13 Container pane; //content pane是container的一个对象 14 CardLayout cc = new CardLayout(); //卡片布局 15 JLabel labTop = new JLabel("学生信息管理系统"); //建立了一个标签写着欢迎你 16 JLabel labTop2 = new JLabel(" "); //建立了另一个空标签 17 JLabel labTop3 = new JLabel(" "); //建立了另一个空标签 18 JLabel labTop4 = new JLabel(" "); //建立了另一个空标签 19 JLabel labTop5 = new JLabel(" "); //建立了另一个空标签 20 21 JTextField txtName = new JTextField(15); //名字输入控制在15个字符之内 22 JPasswordField txtPassword = new JPasswordField(15); //密码输入控制在15个字符之内 23 JButton butOK = new JButton(" 登陆 "); //建立一个OK按钮 24 JButton butexit = new JButton(" 退出 "); 25 public StudentFrame() { //登陆窗体标题框内容 26 super("用户登陆"); 27 initalInterface(); 28 } 29 private void initalInterface() { //初始化设置 30 //设置登陆按钮的字体 31 Font font = new Font("Dialog", Font.BOLD, 16); 32 butOK.setFont(font); 33 //设置退出按钮的字体 34 Font font2 = new Font("Dialog", Font.BOLD, 16); 35 butexit.setFont(font2); 36 //设置"学生信息管理系统"的字体 37 Font font1 = new Font("Dialog", Font.BOLD, 30); 38 labTop.setFont(font1); 39 //开始构建菜单 40 //****************************************************** 41 JMenuBar menuBar; 42 JMenu menu; 43 JMenuItem menuItem; 44 //建立菜单条 45 menuBar = new JMenuBar(); 46 this.setJMenuBar(menuBar); 47 //建立File菜单 48 menu = new JMenu("文件"); 49 menuBar.add(menu); 50 menuItem=new JMenuItem("打开"); 51 menu.add(menuItem); 52 menu.addSeparator(); 53 menuItem = new JMenuItem("退出"); 54 //给菜单项Exit添加活动事件处理程序 55 menuItem.addActionListener(new java.awt.event.ActionListener() { 56 57 public void actionPerformed(java.awt.event.ActionEvent evt) { 58 finish(); 59 System.exit(0); 60 } 61 }); 62 menu.add(menuItem); 63 //建立Help菜单 64 menu=new JMenu("帮助"); 65 menuBar.add(menu); 66 menuItem=new JMenuItem("关于"); 67 menu.add(menuItem); 68 //给菜单项About添加活动事件处理程序 69 menuItem.addActionListener(new java.awt.event.ActionListener() { 70 71 public void actionPerformed(java.awt.event.ActionEvent evt) { 72 aboutActionPerformed(evt); 73 } 74 }); 75 //****************************************************** 76 //结束构建菜单 77 78 //构建第一张卡片panLogin 79 //******************************************************* 80 JPanel panLogin = new JPanel(); 81 BoxLayout v = new BoxLayout(panLogin, BoxLayout.Y_AXIS); 82 panLogin.setLayout(v); 83 JPanel pt = new JPanel(); 84 panLogin.add(pt); 85 pt = new JPanel(); 86 labTop.setForeground(Color.BLACK); 87 pt.add(labTop); 88 panLogin.add(pt); 89 pt = new JPanel(); 90 pt.add(new JLabel("用户名:")); 91 pt.add(txtName); 92 panLogin.add(pt); 93 pt = new JPanel(); 94 pt.add(new JLabel("密码:")); 95 pt.add(txtPassword); 96 panLogin.add(pt); 97 //给OK按钮添加活动事件处理程序 98 butOK.addActionListener(new java.awt.event.ActionListener() { 99 100 public void actionPerformed(java.awt.event.ActionEvent evt) { 101 butOKActionPerformed(evt); 102 } 103 }); 104 pt = new JPanel(); 105 pt.add(butOK); 106 panLogin.add(pt); 107 panLogin.add(new JPanel()); 108 //第一张卡片panLogin构建结束**************************************************** 109 110 //构建第二张卡片panOperation 111 //******************************************************************** 112 JPanel panOperation = new JPanel(); 113 panOperation.setLayout(new BorderLayout()); 114 panOperation.add(labTop2, BorderLayout.NORTH); 115 panOperation.add(new JLabel(" "), BorderLayout.SOUTH); 116 JLabel labTop7; 117 JPanel p2 = new JPanel(); 118 p2.setLayout(new GridLayout(5, 3)); 119 JButton bPut = new JButton("添加学生信息"); 120 JButton bGet = new JButton(""); 121 JButton bSearch = new JButton("查询学生信息"); 122 JButton bQuit = new JButton("退出"); 123 //给四个按钮分别添加活动事件处理程序 124 bPut.addActionListener(new java.awt.event.ActionListener() { 125 public void actionPerformed(java.awt.event.ActionEvent evt) { 126 bPutActionPerformed(evt); 127 } 128 }); 129 bSearch.addActionListener(new java.awt.event.ActionListener() { 130 131 public void actionPerformed(java.awt.event.ActionEvent evt) { 132 bSearchActionPerformed(evt); 133 } 134 }); 135 bQuit.addActionListener(new java.awt.event.ActionListener() { 136 137 public void actionPerformed(java.awt.event.ActionEvent evt) { 138 bQuitActionPerformed(evt); 139 } 140 }); 141 p2.add(new JLabel(" ")); 142 p2.add(new JLabel(" ")); 143 p2.add(new JLabel(" ")); 144 145 p2.add(new JLabel(" ")); 146 p2.add(bPut); 147 p2.add(new JLabel(" ")); 148 149 p2.add(new JLabel(" ")); 150 p2.add(new JLabel(" ")); 151 // p2.add(bGet); 152 p2.add(new JLabel(" ")); 153 154 p2.add(new JLabel(" ")); 155 p2.add(bSearch); 156 p2.add(new JLabel(" ")); 157 158 p2.add(new JLabel(" ")); 159 // p2.add(bQuit); 160 p2.add(new JLabel(" ")); 161 162 panOperation.add(p2, BorderLayout.CENTER); 163 //第二张卡片panOperation构建结束*************************************************** 164 165 //在主窗口中添加前面构建的两张卡片panLogin和panOperation 166 pane = getContentPane(); 167 pane.setLayout(cc); 168 pane.add(panLogin, "login"); 169 pane.add(panOperation, "operation"); 170 //添加窗口关闭事件 171 this.addWindowListener(new WindowAdapter() { 172 173 //表示下面的方法是覆盖父类的方法 174 @Override 175 public void windowClosing(WindowEvent e) { 176 finish(); 177 } 178 }); 179 setSize(550, 550); 180 //让起始窗口停在屏幕的中央 181 Toolkit kit = Toolkit.getDefaultToolkit(); 182 Dimension dim = kit.getScreenSize(); 183 setLocation(dim.width / 2 - 150, dim.height / 2 - 100); 184 //禁止用户改变窗口的大小 185 this.setResizable(false); 186 setVisible(true); 187 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 188 189 } 190 private void finish(){ 191 student.saveData(); 192 } 193 //由菜单About调用的方法 194 private void aboutActionPerformed(java.awt.event.ActionEvent evt){ 195 JOptionPane.showMessageDialog(this,"欢迎使用温娉哲开发的学生信息管理系统!","欢迎使用",JOptionPane.INFORMATION_MESSAGE); 196 } 197 //分别由五个按钮活动事件处理程序调用的方法 198 private void butOKActionPerformed(java.awt.event.ActionEvent evt) { 199 String no = txtName.getText(); 200 String pw = new String(txtPassword.getPassword()); 201 if (student.check(no, pw)) { 202 labTop.setForeground(Color.black); 203 labTop.setText("欢迎使用"); 204 txtName.setText(""); 205 txtPassword.setText(""); 206 cc.show(pane, "operation"); 207 } else { 208 labTop.setForeground(Color.red); 209 labTop.setText("您的帐号或密码错误,请从新输入!"); 210 } 211 } 212 public void bPutActionPerformed(java.awt.event.ActionEvent evt) { 213 num = Integer.parseInt(JOptionPane.showInputDialog(this, "请添加学生学号:")); 214 labTop2.setText("添加的学生学号为:" + num); 215 216 num1 = String.format(JOptionPane.showInputDialog(this, "请添加学生姓名:")); 217 //num1 = student.getCurrent(). inputName(num1); 218 labTop2.setText("添加的学生姓名为:" + num1); 219 } 220 public void bSearchActionPerformed(java.awt.event.ActionEvent evt) { 221 222 num = Integer.parseInt(JOptionPane.showInputDialog(this, "请输入要查询的学生学号:")); 223 // num = student.getCurrent().inputID(num); 224 225 // num1 = student.getCurrent().getinformation(); 226 227 labTop2.setText("学号为" + num+"的学生姓名为"+num1); 228 } 229 private void bQuitActionPerformed(java.awt.event.ActionEvent evt) { 230 labTop2.setText(""); 231 cc.show(pane, "login"); 232 txtName.requestFocusInWindow(); 233 } 234 public static void main(String[] args) { 235 StudentFrame student = new StudentFrame(); 236 } 237 }
1 //package newpackage; 2 import java.io.*; 3 import java.util.*; 4 5 public class StudentLogin { 6 Scanner sc = new Scanner(System.in); //获取控制台输入 7 Vector<Student> v = new Vector<Student>(); //建立一个动态数组的对象 8 private Student atm; 9 //current是Account的对象,是Account类的实例化 10 11 public StudentLogin() { 12 try { //异常捕获 13 FileInputStream fi = new FileInputStream("accoun1t.txt"); 14 15 ObjectInputStream oi = new ObjectInputStream(fi); 16 v = (Vector<Student>) oi.readObject(); 17 } catch (FileNotFoundException e) { 18 v.add(new Student("admin", "1", 100)); 19 v.add(new Student("user", "2", 100)); 20 v.add(new Student("windy", "3", 100)); 21 v.add(new Student("jason", "4", 100)); 22 } catch (Exception e) { 23 System.out.println("Error - " + e.toString()); 24 } 25 } 26 27 public Student getCurrent() { //获取当前余额 28 return atm; 29 } 30 31 public boolean check(String account, String pw) { //检查帐号,密码是否正确 32 int i; //定义了一个整型变量 33 Student a; 34 for (i = 0; i < v.size(); i++) { 35 a = v.get(i); 36 if (a.check(account, pw)) { 37 atm = a; 38 return true; 39 } 40 } 41 atm = null; 42 return false; 43 } 44 public void saveData() { //保存数据 45 try { 46 FileOutputStream fo = new FileOutputStream("account.txt"); 47 ObjectOutputStream oo = new ObjectOutputStream(fo); 48 oo.writeObject(v); 49 oo.close(); 50 } catch (IOException e) { 51 System.out.println("Error -" + e.toString()); 52 } 53 54 } 55 }
1 import java.io.*; 2 public class Student implements Serializable{ 3 private String no; //帐号 4 private String password; //密码 5 private int information; //余额 6 private int ID; //余额 7 private String name;//姓名 8 public Student(String accounts,String pw,int num) { // 9 no=new String(accounts); 10 password=new String(pw); 11 information=(num>0?num:0); 12 } 13 // public String getNo(){ //获取学号 14 // return no; 15 // } 16 public int getinformation() { //获取学生信息 17 return information; 18 } 19 public int inputID(int num) { //获取学号 20 21 return ID; 22 } 23 public String inputName(String num1) { //获取学生姓名 24 return name; 25 } 26 public boolean setPassword(String pw) { //修改密码 27 password=new String(pw); 28 return true; 29 } 30 public boolean check(String accounts,String pw) { //核对密码 31 if(no.equals(accounts)&&password.equals(pw)) 32 return true; 33 else 34 return false; 35 } 36 int Add(int num) { 37 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 38 } 39 }
1 package newpackage; 2 3 import java.io.*; 4 import java.util.*; 5 6 public class FunctionFrame { 7 StudentLogin student = new StudentLogin(); //建立一个student对象 8 Scanner sc = new Scanner(System.in); //建立一个sc对象 scanner获取控制台输入,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给scanner,做为扫描对象 system.in 是输入的意思 9 10 public void inputNoPassword() { //定义了一个无返回值的输入密码的方法 11 String account, pw; //定义了俩个字符串类型的变量 12 boolean b; //定义了一个布尔类型的变量 13 do { //循环语句, 14 System.out.print("Please enter account:"); //提示输入帐号 15 account = sc.next(); //sc是scanner类中的一个对象,此时表示从结果集中连续取值 16 System.out.print("Please enter password:"); //提示输入密码 17 pw = sc.next(); //查找并返回来自此扫描器的下一个完整标记 18 b = student.check(account, pw); //检查帐号,密码是否正确 19 if (b == false) { //若是帐号,密码不相等,提示请从新输入 20 System.out.println("您的帐号或密码错误,请从新输入!"); 21 } 22 } while (b == false); 23 } 24 25 public void operate() { //定义了一个返回值为空的操做方法 26 int i; //定义了一个整型变量 27 do { //设置了四个不一样的功能 28 System.out.println("please choose:"); 29 System.out.println("添加学生信息"); 30 System.out.println(""); 31 System.out.println("查询学生信息"); 32 System.out.println(""); 33 try { 34 i = sc.nextInt(); 35 if (i == 1) { 36 inputID(); 37 // getMoney(); 38 } else if (i == 2) { 39 // putID(); 40 } else if (i == 3) { 41 search(); 42 } else if (i == 4) { 43 break; 44 } else { 45 System.out.println("输入有误,请从新输入!"); 46 } 47 } catch (InputMismatchException e) { 48 System.out.println("输入有误\nPlease choose1-4:"); 49 sc.nextLine(); 50 } 51 } while (true); 52 } 53 private void inputID() { 54 int i, num; 55 System.out.print("Please enter ID : "); 56 i = sc.nextInt(); 57 num = student.getCurrent().inputID(i); 58 System.out.println("You have put into: " + num); 59 } 60 61 private void search() { 62 int num; 63 num = student.getCurrent().getBalance(); 64 System.out.println("学生信息为: " + num); 65 } 66 67 public void start() { 68 do { 69 inputNoPassword(); 70 operate(); 71 System.out.print("你肯定要退出吗? y/n"); 72 if (sc.next().equals("y")) { 73 student.saveData(); 74 break; 75 } 76 } while (true); 77 } 78 79 public static void main(String[] args) { 80 FunctionFrame atmNoFrame = new FunctionFrame(); 81 atmNoFrame.start(); 82 } 83 }