————- ps :多個包寫在一塊兒———–java
1 package test; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 import java.util.ArrayList; 6 7 class person{ 8 public String name; 9 public String age; 10 } 11 12 class Student extends person{ 13 public String no; 14 public String score1,score2,score3,score4; 15 public Student(String no, String name, String age,String score1,String score2,String score3,String score4) { 16 this.no = no; 17 this.name = name; 18 this.age = age; 19 this.score1 = score1; 20 this.score2 = score2; 21 this.score3 = score3; 22 this.score4 = score4; 23 } 24 25 public String getNo() { 26 return this.no; 27 } 28 public void setNo(String no) { 29 this.no = no; 30 } 31 public String getName() { 32 return this.name; 33 } 34 public void setName(String name) { 35 this.name = name; 36 } 37 public String getAge() { 38 return this.age; 39 } 40 public void setAge(String age) { 41 this.age = age; 42 } 43 public String getscore1() { 44 return this.score1; 45 } 46 public void setscore1(String score1) { 47 this.score1 = score1; 48 } 49 public String getscore2() { 50 return this.score2; 51 } 52 public void setscore2(String score2) { 53 this.score2 = score2; 54 } 55 public String getscore3() { 56 return this.score3; 57 } 58 public void setscore3(String score3) { 59 this.score3 = score3; 60 } 61 public String getscore4() { 62 return this.score4; 63 } 64 public void setscore4(String score4) { 65 this.score4 = score4; 66 } 67 68 } 69 70 class Class{ 71 ArrayList<Student> stuList; 72 public Class(){ 73 this.stuList=new ArrayList<>(); 74 } 75 public void addStudent(Student stu){ 76 this.stuList.add(stu); 77 } 78 public void deleteStudent(Student s){ 79 this.stuList.remove(s); 80 } 81 } 82 83 class menu extends JFrame{ 84 public menu() 85 { 86 this.setBounds(300, 150, 500, 450); 87 this.setTitle("北京大學 -- --學生信息管理系統"); 88 this.setLayout(null); 89 JLabel labTipw = new JLabel("歡迎進入 北京大學 學生信息管理系統"); 90 labTipw.setBounds(50, 2, 250, 45); 91 JLabel labTipws = new JLabel("(*新用戶初始密碼爲 身份證後六位數字)"); 92 labTipws.setBounds(50, 15, 250, 45); 93 JLabel labyonghu = new JLabel("用戶名:"); 94 labyonghu.setBounds(50, 60, 100, 50); 95 JTextField txtyonghu = new JTextField("請輸入登陸用戶名:",30); 96 txtyonghu.setBounds(130, 76, 180, 30); 97 JLabel labmima = new JLabel("密碼:"); 98 labmima.setBounds(50, 100, 50, 50); 99 JTextField txtmima = new JTextField("請輸入登陸6位密碼:",30); 100 txtmima.setBounds(130, 117, 180, 30); 101 JButton btndianji = new JButton("點擊登陸"); 102 btndianji.setBounds(130, 165, 180, 30); 103 btndianji.setForeground(Color.BLUE); 104 105 this.add(labTipw); 106 this.add(labTipws); 107 this.add(labyonghu); 108 this.add(txtyonghu); 109 this.add(labmima); 110 this.add(txtmima); 111 this.add(btndianji); 112 btndianji.addActionListener(new ActionListener() { 113 public void actionPerformed(ActionEvent e) { 114 new StudentFrame();} 115 } ); 116 setVisible(true); 117 } 118 } 119 120 public class StudentFrame extends JFrame { 121 Class c = new Class(); 122 JLabel labNo; 123 JLabel labName; 124 JLabel labAge; 125 JLabel labscore1; 126 JLabel labscore2; 127 JLabel labscore3; 128 JLabel labscore4; 129 JLabel labTip; 130 JLabel labavr; 131 132 JTextField txtNo; 133 JTextField txtName; 134 JTextField txtAge; 135 JTextField txtscore1; 136 JTextField txtscore2; 137 JTextField txtscore3; 138 JTextField txtscore4; 139 JTextField txtavr; 140 141 JButton btnAdd; 142 JButton btnRemove; 143 JButton btnModify; 144 JButton btnClear; 145 JButton btnFirst; 146 JButton btnPrev; 147 JButton btnNext; 148 JButton btnLast; 149 JButton btnCheck; 150 JOptionPane dialog; 151 152 public StudentFrame() { 153 this.setBounds(300, 100, 650, 530); 154 this.setTitle("北京大學 -- --學生信息管理系統"); 155 this.setLayout(null); 156 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 157 studentin(); 158 addStu(); 159 firstStudent(); 160 lastStudent(); 161 previous(); 162 next(); 163 remove(); 164 check(); 165 modify(); 166 clear(); 167 this.setVisible(true); 168 } 169 170 public void studentin() { 171 labTip = new JLabel("登錄成功 !您已經進入管理系統(僅輸入學號即可查找學生信息)."); 172 labTip.setBounds(50, 2, 550, 45); 173 labNo = new JLabel("學生學號:"); 174 labNo.setBounds(50, 20, 100, 50); 175 txtNo = new JTextField("請輸入6位學號:",30); 176 txtNo.setBounds(130, 35, 180, 30); 177 labName = new JLabel("學生姓名:"); 178 labName.setBounds(50, 60, 100, 50); 179 txtName = new JTextField("請輸入學生姓名:",30); 180 txtName.setBounds(130, 76, 180, 30); 181 labAge = new JLabel("班級:"); 182 labAge.setBounds(50, 100, 50, 50); 183 txtAge = new JTextField("請輸入學生所在班級:",30); 184 txtAge.setBounds(130, 117, 180, 30); 185 labscore1 = new JLabel("JAVA 成績:"); 186 labscore1.setBounds(50, 140, 100, 50); 187 txtscore1 = new JTextField("請輸入學生成績:",30); 188 txtscore1.setBounds(130, 158, 180, 30); 189 labscore2 = new JLabel("C語言成績:"); 190 labscore2.setBounds(50, 189, 100, 50); 191 txtscore2 = new JTextField("請輸入學生成績:",30); 192 txtscore2.setBounds(130, 199, 180, 30); 193 labscore3 = new JLabel("Python成績:"); 194 labscore3.setBounds(50, 230, 100, 50); 195 txtscore3 = new JTextField("請輸入學生成績:",30); 196 txtscore3.setBounds(130, 240, 180, 30); 197 labscore4 = new JLabel("C++ 成績:"); 198 labscore4.setBounds(50, 290, 100, 50); 199 txtscore4 = new JTextField("請輸入學生成績:",30); 200 txtscore4.setBounds(130, 301, 180, 30); 201 labavr = new JLabel("操做說明 ** |<< : 第一個學生信息 <:上一位學生信息 >:下一個學生信息 >>|:最後一個學生信息"); 202 labavr.setBounds(50, 330, 580, 50); 203 204 btnAdd = new JButton("添加"); 205 btnAdd.setBounds(50, 385, 80, 20); 206 btnAdd.setForeground(Color.BLACK); 207 btnModify = new JButton("修改"); 208 btnModify.setBounds(150, 385, 80, 20); 209 btnModify.setForeground(Color.BLUE); 210 btnRemove = new JButton("刪除"); 211 btnRemove.setBounds(250, 385, 80, 20); 212 btnRemove.setForeground(Color.RED); 213 btnClear = new JButton("清空"); 214 btnClear.setBounds(350, 385, 80, 20); 215 btnClear.setForeground(Color.GREEN); 216 btnCheck = new JButton("查找信息"); 217 btnCheck.setBounds(350, 145, 160, 50); 218 btnCheck.setForeground(Color.BLACK); 219 btnFirst = new JButton("|<<"); 220 btnFirst.setBounds(50, 432, 80, 20); 221 btnFirst.setForeground(Color.PINK); 222 btnPrev = new JButton("<"); 223 btnPrev.setBounds(150, 432, 80, 20); 224 btnPrev.setForeground(Color.BLACK); 225 btnNext = new JButton(">"); 226 btnNext.setBounds(250, 432, 80, 20); 227 btnNext.setForeground(Color.BLACK); 228 btnLast = new JButton(">>|"); 229 btnLast.setBounds(350, 432, 80, 20); 230 btnLast.setForeground(Color.PINK); 231 232 this.add(labNo); 233 this.add(labName); 234 this.add(labAge); 235 this.add(labscore1); 236 this.add(txtNo); 237 this.add(labTip); 238 this.add(txtName); 239 this.add(txtAge); 240 this.add(txtscore1); 241 this.add(btnAdd); 242 this.add(btnRemove); 243 this.add(btnModify); 244 this.add(btnClear); 245 this.add(btnFirst); 246 this.add(btnPrev); 247 this.add(btnNext); 248 this.add(btnLast); 249 this.add(btnCheck); 250 this.add(labavr); 251 this.add(labscore2); 252 this.add(txtscore2); 253 this.add(labscore3); 254 this.add(txtscore3); 255 this.add(labscore4); 256 this.add(txtscore4); 257 258 259 } 260 261 public boolean blank() { 262 if (txtNo.getText().equals("")) { 263 dialog.showMessageDialog(null, "學號輸入有誤 請從新輸入!"); 264 return false; 265 } 266 if (txtName.getText().equals("")) { 267 dialog.showMessageDialog(null, "名字輸入有誤 請從新輸入!"); 268 return false; 269 } 270 if (txtAge.getText().equals("")) { 271 dialog.showMessageDialog(null, "班級輸入有誤 請從新輸入!"); 272 return false; 273 }if (txtscore1.getText().equals("")) { 274 dialog.showMessageDialog(null, "成績輸入有誤 請從新輸入!"); 275 return false; 276 }if (txtscore2.getText().equals("")) { 277 dialog.showMessageDialog(null, "成績輸入有誤 請從新輸入!"); 278 return false; 279 } 280 if (txtscore3.getText().equals("")) { 281 dialog.showMessageDialog(null, "成績輸入有誤 請從新輸入!"); 282 return false; 283 } 284 if (txtscore4.getText().equals("")) { 285 dialog.showMessageDialog(null, "成績輸入有誤 請從新輸入!"); 286 return false; 287 } 288 return true; 289 } 290 291 public void addStu() { 292 btnAdd.addActionListener(new ActionListener() { 293 294 public void actionPerformed(ActionEvent e) { 295 if (blank() == true) { 296 c.addStudent(new Student(txtNo.getText(), txtName.getText(), txtAge.getText(),txtscore1.getText(),txtscore2.getText(),txtscore3.getText(),txtscore4.getText())); 297 dialog.showMessageDialog(null, "學生信息已添加成功"); 298 299 300 }}}); 301 } 302 public void firstStudent() { 303 btnFirst.addActionListener(new ActionListener() { 304 305 public void actionPerformed(ActionEvent e) { 306 txtNo.setText(c.stuList.get(0).getNo()); 307 txtName.setText(c.stuList.get(0).getName()); 308 txtAge.setText(c.stuList.get(0).getAge()); 309 txtscore1.setText(c.stuList.get(0).getscore1()); 310 txtscore2.setText(c.stuList.get(0).getscore2()); 311 txtscore3.setText(c.stuList.get(0).getscore3()); 312 txtscore4.setText(c.stuList.get(0).getscore4()); 313 314 } 315 }); 316 } 317 public void lastStudent() { 318 btnLast.addActionListener(new ActionListener() { 319 320 public void actionPerformed(ActionEvent e) { 321 txtNo.setText(c.stuList.get(c.stuList.size() - 1).getNo()); 322 txtName.setText(c.stuList.get(c.stuList.size() - 1).getName()); 323 txtAge.setText(c.stuList.get(c.stuList.size() - 1).getAge()); 324 txtscore1.setText(c.stuList.get(c.stuList.size() - 1).getscore1()); 325 txtscore2.setText(c.stuList.get(c.stuList.size() - 1).getscore2()); 326 txtscore3.setText(c.stuList.get(c.stuList.size() - 1).getscore3()); 327 txtscore4.setText(c.stuList.get(c.stuList.size() - 1).getscore4()); 328 } 329 }); 330 } 331 332 public void previous() { 333 btnPrev.addActionListener(new ActionListener() { 334 335 public void actionPerformed(ActionEvent e) { 336 int n = 0; 337 for (int i = 0; i < c.stuList.size(); i++) { 338 if (c.stuList.get(i).getNo().equals(txtNo.getText())) { 339 n = i - 1; 340 } 341 } 342 if (c.stuList.get(0).getNo().equals(txtNo.getText())) { 343 dialog.showMessageDialog(null, "再無上一個學生"); 344 } else { 345 txtNo.setText(c.stuList.get(n).getNo()); 346 txtName.setText(c.stuList.get(n).getName()); 347 txtAge.setText(c.stuList.get(n).getAge()); 348 txtscore1.setText(c.stuList.get(n).getscore1()); 349 txtscore2.setText(c.stuList.get(n).getscore2()); 350 txtscore3.setText(c.stuList.get(n).getscore3()); 351 txtscore4.setText(c.stuList.get(n).getscore4()); 352 } 353 } 354 }); 355 } 356 357 public void next() { 358 btnNext.addActionListener(new ActionListener() { 359 360 public void actionPerformed(ActionEvent e) { 361 int n = 0; 362 for (int i = 0; i < c.stuList.size(); i++) { 363 if (c.stuList.get(i).getNo().equals(txtNo.getText())) { 364 n = i + 1; 365 } 366 } 367 if (c.stuList.get(c.stuList.size() - 1).getNo().equals(txtNo.getText())) { 368 dialog.showMessageDialog(null, "再無下一個學生"); 369 } else { 370 txtNo.setText(c.stuList.get(n).getNo()); 371 txtName.setText(c.stuList.get(n).getName()); 372 txtAge.setText(c.stuList.get(n).getAge()); 373 txtscore1.setText(c.stuList.get(n).getscore1()); 374 txtscore2.setText(c.stuList.get(n).getscore2()); 375 txtscore3.setText(c.stuList.get(n).getscore3()); 376 txtscore4.setText(c.stuList.get(n).getscore4());} 377 } 378 }); 379 } 380 381 public void remove() { 382 btnRemove.addActionListener(new ActionListener() { 383 384 public void actionPerformed(ActionEvent e) { 385 if (c.stuList.size() == 0) { 386 JOptionPane.showMessageDialog(null, "沒有該學生信息"); 387 } else { 388 Student s = new Student(txtNo.getText(), txtName.getText(), txtAge.getText(),txtscore1.getText(),txtscore2.getText(),txtscore3.getText(),txtscore4.getText()); 389 if (blank() == true) { 390 System.out.println(c.stuList.contains(s)); 391 System.out.println(s); 392 if (!c.stuList.contains(s)) { 393 c.deleteStudent(s); 394 395 JOptionPane.showMessageDialog(null, "已經刪除成功"); 396 int a = 0; 397 for (int i = 0; i < c.stuList.size(); i++) { 398 if (c.stuList.get(i).getNo().equals(txtNo.getText())) { 399 a = i + 1; 400 } 401 } 402 if (c.stuList.size() > 0) { 403 404 if (c.stuList.get(c.stuList.size() - 1).getNo().equals(txtNo.getText())) { 405 txtNo.setText(c.stuList.get(0).getNo()); 406 txtName.setText(c.stuList.get(0).getName()); 407 txtAge.setText(c.stuList.get(0).getAge()); 408 txtscore1.setText(c.stuList.get(0).getscore1()); 409 txtscore2.setText(c.stuList.get(0).getscore2()); 410 txtscore3.setText(c.stuList.get(0).getscore3()); 411 txtscore4.setText(c.stuList.get(0).getscore4()); 412 } else { 413 txtNo.setText(c.stuList.get(a).getNo()); 414 txtName.setText(c.stuList.get(a).getName()); 415 txtAge.setText(c.stuList.get(a).getAge()); 416 txtscore1.setText(c.stuList.get(a).getscore1()); 417 txtscore2.setText(c.stuList.get(a).getscore2()); 418 txtscore3.setText(c.stuList.get(a).getscore3()); 419 txtscore4.setText(c.stuList.get(a).getscore4()); 420 } 421 } else { 422 clear(); 423 } 424 } else { 425 dialog.showMessageDialog(null, "將來找到對應學生的信息"); }} 426 } 427 } 428 }); 429 } 430 431 public void modify() { 432 btnModify.addActionListener(new ActionListener() { 433 434 public void actionPerformed(ActionEvent e) { 435 if (c.stuList.size() == 0) { 436 dialog.showMessageDialog(null, "沒有該學生信息"); 437 } 438 for (Student s : c.stuList) { 439 if (s.getNo().equals(txtNo.getText())) { 440 if (blank() == true) { 441 s.setName(txtName.getText()); 442 s.setAge(txtAge.getText()); 443 444 JOptionPane.showMessageDialog(null, "已經修改爲功"); 445 return; 446 } 447 } 448 } 449 JOptionPane.showMessageDialog(null, "修改失敗請從新操做"); 450 } 451 }); 452 } 453 454 public void check() { 455 btnCheck.addActionListener(new ActionListener() { 456 457 public void actionPerformed(ActionEvent e) { 458 if ((txtNo.getText().equals(""))) { 459 dialog.showMessageDialog(null, "學號不能爲空 請重新輸入"); 460 } else { 461 int m = 0; 462 for (int i = 0; i < c.stuList.size(); i++) { 463 if (c.stuList.get(i).getNo().equals(txtNo.getText())) { 464 JOptionPane.showMessageDialog(null, "已查到該學生信息"); 465 txtName.setText(c.stuList.get(i).getName()); 466 txtAge.setText(c.stuList.get(i).getAge()); 467 txtscore1.setText(c.stuList.get(i).getscore1()); 468 txtscore2.setText(c.stuList.get(i).getscore2()); 469 txtscore3.setText(c.stuList.get(i).getscore3()); 470 txtscore4.setText(c.stuList.get(i).getscore4()); 471 break;} 472 m++;} 473 if (m == c.stuList.size()) { 474 dialog.showMessageDialog(null, "沒有此學生的相關信息"); 475 return; } 476 } 477 } 478 }); 479 } 480 481 public void clear() { 482 btnClear.addActionListener(new ActionListener() { 483 484 public void actionPerformed(ActionEvent e) { 485 txtNo.setText(""); 486 txtName.setText(""); 487 txtAge.setText(""); 488 txtscore1.setText(""); 489 txtscore2.setText(""); 490 txtscore3.setText(""); 491 txtscore4.setText(""); 492 } 493 } 494 ); 495 } 496 497 public static void main(String[] args) { 498 new menu(); 499 } 500 }