文章目录
1.前言
做的不是很好,很丑,请小伙伴们多多留情.
2.程序入口
package gui.SuchAs;
public class StudentManageSystem {
public static void main(String[] args) {
new LoginAndRegister();
}
}
3.登录注册页面
为了模拟多个用户注册,所以做了个用户库,可以注册多次.
package gui.SuchAs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class LoginAndRegister extends Frame {
private final UserInformationLibrary userInformationLibrary = new UserInformationLibrary();
private TextField userText;
private TextField passwordText;
private Label user;
private Label password;
private Button login;
private Button register;
private int id = 0;
public LoginAndRegister() {
initFrame();
initLayout();
addComponent();
setPasswordText();
setLoginThing();
setRegisterThing();
}
private void initFrame() {
setTitle("登录");
setVisible(true);
setBackground(Color.lightGray);
setBounds(400, 300, 400, 300);
setLayout(new GridLayout(3, 2, 0, 100));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void initLayout() {
user = new Label("user:");
userText = new TextField(40);
password = new Label("password");
passwordText = new TextField(40);
login = new Button("login");
register = new Button("register");
}
private void addComponent() {
add(user);
add(userText);
add(password);
add(passwordText);
add(login);
add(register);
}
private void setPasswordText() {
passwordText.setEchoChar('*');
}
private void setLoginThing() {
login.addActionListener(e -> {
//登录成功跳转
//核对信息
UserInformation user = userInformationLibrary.findUser(userText.getText());
if (user != null)
if (user.getPassword().equals(passwordText.getText())) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "登录成功");
setVisible(false);
new Main();
} else {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "密码错误");
}
else {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "用户不存在");
}
userText.setText("");
passwordText.setText("");
});
}
private void setRegisterThing() {
register.addActionListener(e -> {
//注册
id++;
if (id == 100)
id = 0;
if (id % 2 == 1) {
setTitle("注册");
user.setText("set user");
password.setText("set password");
login.setVisible(false);
register.setLabel("submit");
} else if (id % 2 == 0) {
//收集信息
UserInformation userName = new UserInformation();
userName.setUser(userText.getText());
userName.setPassword(passwordText.getText());
//送入库
userInformationLibrary.addUser(userName);
//清空数据
userText.setText("");
passwordText.setText("");
setTitle("登录");
user.setText("user");
password.setText("password");
login.setVisible(true);
register.setLabel("register");
}
});
}
}
图示:
3.主界面
目前展示所有学生信息没完成,尽快补上.
package gui.SuchAs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main extends Frame {
private final StudentLibrary studentLibrary;
private Button addStudent;
private Button findStudent;
private Button deleteStudent;
private Button getStudent;
private Button showStudent;
private Button deleteAllStudent;
private Button size;
private Button exit;
public Main() {
studentLibrary = new StudentLibrary();
initFrame();
initButton();
initLayout();
setAddStudentThing();
setFindStudentThing();
setDelStudentThing();
setDeleteAllStudentThing();
setExitThing();
setShowStudentThing();
setSizeThing();
setGetStudentThing();
}
private void initFrame() {
setTitle("学生管理系统");
setVisible(true);
setLayout(new GridLayout(8, 1));
setBackground(Color.lightGray);
setBounds(500, 250, 600, 400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void initButton() {
addStudent = new Button(" addStudent");
findStudent = new Button("findStudent");
deleteStudent = new Button("deleteStudent");
getStudent = new Button("getStudent");
showStudent = new Button("showStudent");
deleteAllStudent = new Button("deleteAllStudent");
size = new Button("size");
exit = new Button("exit");
}
private void initLayout() {
add(addStudent);
add(findStudent);
add(deleteStudent);
add(getStudent);
add(showStudent);
add(deleteAllStudent);
add(size);
add(exit);
}
private void setAddStudentThing() {
addStudent.addActionListener(e -> {
//添加学生
new AddStudent(studentLibrary);
});
}
private void setDelStudentThing() {
deleteStudent.addActionListener(e -> {
//删除学生
new DeleteOneStudent(studentLibrary);
});
}
private void setDeleteAllStudentThing() {
deleteAllStudent.addActionListener(e -> {
//删除所有学生
if (studentLibrary.isEmpty()) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "当前学生列表为空");
return;
}
studentLibrary.deleteAll();
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "删除成功");
});
}
private void setFindStudentThing() {
findStudent.addActionListener(e -> {
//按名字查找学生
new FindStudent(studentLibrary);
});
}
private void setGetStudentThing() {
getStudent.addActionListener(e -> {
//获取指定索引的学生信息
new GetStudent(studentLibrary);
});
}
private void setShowStudentThing() {
showStudent.addActionListener(e -> {
//展示所有学生信息
});
}
private void setSizeThing() {
size.addActionListener(e -> {
//返回学生人数
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "当前表中有" + studentLibrary.size() + "名学生");
});
}
private void setExitThing() {
exit.addActionListener(e -> {
//退出
System.exit(0);
});
}
public static void main(String[] args) {
new Main();
}
}
图示:
4.添加学生界面
package gui.SuchAs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AddStudent extends Frame {
private final StudentLibrary studentLibrary;
private PSouth pSouth;
private PCenter pCenter;
private PWest pWest;
private static class PWest extends Panel {
private Label name;
private Label age;
private Label sum;
private Label phone;
private Label hometown;
public PWest() {
initPanel();
initLabel();
initLayout();
}
private void initPanel() {
setVisible(true);
setLayout(new GridLayout(5, 1));
}
private void initLabel() {
name = new Label("name");
age = new Label("age");
sum = new Label("sum");
phone = new Label("phone");
hometown = new Label("hometown");
}
private void initLayout() {
add(name);
add(age);
add(sum);
add(phone);
add(hometown);
}
}
private class PSouth extends Panel {
private Button determine;
public PSouth() {
initPanel();
initButton();
initLayout();
}
private void initPanel() {
setVisible(true);
// pack();
}
private void initButton() {
determine = new Button("Determine");
}
private void initLayout() {
add(determine);
}
public void setDetermineThing() {
determine.addActionListener(e -> {
String name = pCenter.nameText.getText();
String age = pCenter.ageText.getText();
String num = pCenter.numText.getText();
String phone = pCenter.phoneText.getText();
String hometown = pCenter.hometownText.getText();
if (name.equals("") || age.equals("") || num.equals("") || phone.equals("") || hometown.equals("")) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "输入不能为空");
return;
}
Student student = new Student();
student.setName(name);
student.setAge(Integer.parseInt(age));
student.setNum(num);
student.setPhone(phone);
student.setHometown(hometown);
studentLibrary.addStudent(student);
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "添加成功");
pCenter.nameText.setText("");
pCenter.ageText.setText("");
pCenter.numText.setText("");
pCenter.phoneText.setText("");
pCenter.hometownText.setText("");
});
}
}
private static class PCenter extends Panel {
private TextField nameText;
private TextField ageText;
private TextField numText;
private TextField phoneText;
private TextField hometownText;
public PCenter() {
initPanel();
initTextField();
initLayout();
}
private void initPanel() {
setVisible(true);
setSize(100, 100);
setLayout(new GridLayout(5, 1));
}
private void initTextField() {
nameText = new TextField(100);
ageText = new TextField(100);
numText = new TextField(100);
phoneText = new TextField(100);
hometownText = new TextField(100);
}
private void initLayout() {
add(nameText);
add(ageText);
add(numText);
add(phoneText);
add(hometownText);
}
}
public AddStudent(StudentLibrary studentLibrary) {
this.studentLibrary = studentLibrary;
initFrame();
initPanel();
initLayout();
pSouth.setDetermineThing();
}
private void initFrame() {
setTitle("添加学生");
setVisible(true);
setLayout(new BorderLayout());
setBackground(Color.lightGray);
setBounds(500, 250, 400, 400);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
}
private void initPanel() {
pSouth = new PSouth();
pWest = new PWest();
pCenter = new PCenter();
}
private void initLayout() {
add(pWest, BorderLayout.WEST);
add(pSouth, BorderLayout.SOUTH);
add(pCenter, BorderLayout.CENTER);
}
}
图示:
5.查找学生界面
package gui.SuchAs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FindStudent extends Frame {
private StudentLibrary studentLibrary;
private Button determine;
private Label name;
private TextField nameText;
private Panel up;
private Panel down;
public FindStudent(StudentLibrary studentLibrary) {
this.studentLibrary = studentLibrary;
initFrame();
initPanel();
initButton();
initLabel();
initTextField();
initLayout();
setButtonThing();
}
private void initFrame() {
setTitle("查找学生");
setVisible(true);
setBounds(500, 250, 300, 100);
setLayout(new GridLayout(2, 1));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
}
private void initPanel() {
up = new Panel();
up.setBackground(Color.lightGray);
up.setLayout(new FlowLayout());
down = new Panel();
down.setBackground(Color.pink);
}
private void initButton() {
determine = new Button("Determine");
}
private void initLabel() {
name = new Label("Name");
}
private void initTextField() {
nameText = new TextField(10);
}
private void initLayout() {
up.add(name);
up.add(nameText);
add(up);
add(down);
down.add(determine);
}
public void setButtonThing() {
determine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取编辑框信息
String name = nameText.getText();
//查找学生
Student student = studentLibrary.findStudent(name);
if (student == null) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "该学生不存在");
nameText.setText("");
return;
}
//调用信息窗口
new StudentInformation(student);
//清空编辑框
nameText.setText("");
}
});
}
}
图示:
6.显示学生信息
package gui.SuchAs;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class StudentInformation extends Frame {
private final Student student;
private Label name;
private Label age;
private Label num;
private Label phone;
private Label hometown;
private Label nameInformation;
private Label ageInformation;
private Label sumInformation;
private Label phoneInformation;
private Label hometownInformation;
public StudentInformation(Student student) {
this.student = student;
initFrame();
initLabel();
initLayout();
}
private void initFrame() {
Toolkit.getDefaultToolkit().beep();
setVisible(true);
setTitle("学生信息");
setBackground(Color.lightGray);
setBounds(500, 250, 400, 400);
setLayout(new GridLayout(5, 2));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
}
private void initLabel() {
name = new Label("Name:");
age = new Label("Age:");
num = new Label("Num:");
phone = new Label("Phone:");
hometown = new Label("Hometown:");
nameInformation = new Label(student.getName());
ageInformation = new Label("" + student.getAge());
sumInformation = new Label(student.getNum());
phoneInformation = new Label(student.getPhone());
hometownInformation = new Label(student.getHometown());
}
private void initLayout() {
add(name);
add(nameInformation);
add(age);
add(ageInformation);
add(num);
add(sumInformation);
add(phone);
add(phoneInformation);
add(hometown);
add(hometownInformation);
}
public static void main(String[] args) {
}
}
图示:
7.删除学生界面(和查找学生界面相似,自己太菜,没提高代码复用率)
package gui.SuchAs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class DeleteOneStudent extends Frame {
private StudentLibrary studentLibrary;
private Button determine;
private Label name;
private TextField nameText;
private Panel up;
private Panel down;
public DeleteOneStudent(StudentLibrary studentLibrary) {
this.studentLibrary = studentLibrary;
initFrame();
initPanel();
initButton();
initLabel();
initTextField();
initLayout();
setButtonThing();
}
private void initFrame() {
setTitle("删除学生");
setVisible(true);
setBounds(500, 250, 300, 100);
setLayout(new GridLayout(2, 1));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
}
private void initPanel() {
up = new Panel();
up.setBackground(Color.lightGray);
up.setLayout(new FlowLayout());
down = new Panel();
down.setBackground(Color.pink);
}
private void initButton() {
determine = new Button("Determine");
}
private void initLabel() {
name = new Label("Name");
}
private void initTextField() {
nameText = new TextField(10);
}
private void initLayout() {
up.add(name);
up.add(nameText);
add(up);
add(down);
down.add(determine);
}
public void setButtonThing() {
determine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取编辑框信息
String name = nameText.getText();
//查找学生
Student student = studentLibrary.findStudent(name);
if (student == null) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "该学生不存在");
nameText.setText("");
return;
}
//调用删除方法
studentLibrary.deleteStudent(name);
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "删除成功");
//清空编辑框
nameText.setText("");
}
});
}
}
图示:
8.根据序号查找学生(相似界面)
package gui.SuchAs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GetStudent extends Frame {
private StudentLibrary studentLibrary;
private Button determine;
private Label name;
private TextField nameText;
private Panel up;
private Panel down;
public GetStudent(StudentLibrary studentLibrary) {
this.studentLibrary = studentLibrary;
initFrame();
initPanel();
initButton();
initLabel();
initTextField();
initLayout();
setButtonThing();
}
private void initFrame() {
setTitle("按序号查找学生");
setVisible(true);
setBounds(500, 250, 300, 100);
setLayout(new GridLayout(2, 1));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
}
private void initPanel() {
up = new Panel();
up.setBackground(Color.lightGray);
up.setLayout(new FlowLayout());
down = new Panel();
down.setBackground(Color.pink);
}
private void initButton() {
determine = new Button("Determine");
}
private void initLabel() {
name = new Label("Index");
}
private void initTextField() {
nameText = new TextField(10);
}
private void initLayout() {
up.add(name);
up.add(nameText);
add(up);
add(down);
down.add(determine);
}
public void setButtonThing() {
determine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取编辑框信息
int index = Integer.parseInt(nameText.getText());
if (index <= 0 || index > studentLibrary.size()) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "序号非法");
return;
}
//查找学生
Student student = studentLibrary.getStudent(index - 1);
//调用信息窗口
new StudentInformation(student);
//清空编辑框
nameText.setText("");
}
});
}
}
图示:
9.显示所以学生信息(未完成)
10.删除所有学生信息,获取学生人数,退出
程序均在主界面上
11.学生类
package gui.SuchAs;
public class Student {
private String name;
private int age;
private String num;
private String phone;
private String hometown;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHometown() {
return hometown;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", num='" + num + '\'' +
", phone='" + phone + '\'' +
", hometown='" + hometown + '\'' +
'}';
}
}
12.学生库类
package gui.SuchAs;
import java.util.ArrayList;
public class StudentLibrary {
private final ArrayList<Student> studentArrayList;
public StudentLibrary() {
studentArrayList = new ArrayList<>();
}
public void addStudent(Student student) {
studentArrayList.add(student);
}
public Student deleteStudent(String name) {
Student student = null;
for (Student e : studentArrayList) {
if (e.getName().equals(name)) {
studentArrayList.remove(e);
student = e;
}
}
return student;
}
public Student findStudent(String name) {
Student student = null;
for (Student e : studentArrayList) {
if (e.getName().equals(name)) {
student = e;
}
}
return student;
}
public int size() {
return studentArrayList.size();
}
public Student getStudent(int index) {
return studentArrayList.get(index);
}
public void deleteAll() {
studentArrayList.clear();
}
public boolean isEmpty() {
return studentArrayList.isEmpty();
}
}
12.用户信息类
package gui.SuchAs;
/**
* 用户信息
*/
public class UserInformation {
private String user;
private String password;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "UserInformation{" +
"user='" + user + '\'' +
", password='" + password + '\'' +
'}';
}
}
13.用户库类
package gui.SuchAs;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
public class UserInformationLibrary {
private final ArrayList<UserInformation> userInformationLibrary;
public UserInformationLibrary() {
this.userInformationLibrary = new ArrayList<>();
}
public void addUser(UserInformation user) {
userInformationLibrary.add(user);
}
@SuppressWarnings("all")
public UserInformation deleteUser(String userName) {
UserInformation user = null;
for (UserInformation e : userInformationLibrary) {
if (e.getUser().equals(userName))
user = e;
}
userInformationLibrary.remove(user);
return user;
}
@SuppressFBWarnings("NP_UNWRITTEN_FIELD")
public UserInformation findUser(String userName) {
UserInformation user = null;
for (UserInformation e : userInformationLibrary) {
if (e.getUser().equals(userName))
user = e;
}
return user;
}
}