学生管理系统
1.学生管理系统(控制台界面实现)
//学生类,继承Serializeable接口,将其序列化写入文件
class Student implements Comparable<Student>,Serializable
{
private int id;
private String name;
private int age;
public Student(){
id=0;
name=null;
age=0;
}
public Student(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
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;
}
@Override
public String toString(){
return "id:"+id+" name:"+name+" age:"+age;
}
//重载equals和compareTo函数,分别后续比较
@Override
public boolean equals(Object obj){
if(this==obj)
return true;
if(getClass()!=obj.getClass()){
return false;
}
Student other=(Student)obj;
if(other.name==null){
if(this.name!=null)
return false;
else if(this.id==other.id&&this.age==other.age)
return true;
else
return false;
}
if(this.name.equals(other.name)){
if((this.age==other.age)&&(this.id==other.id))
return true;
}
return false;
}
@Override
public int compareTo(Student other){
if(this.id>other.id)
return 1;
else if(this.id<other.id)
return -1;
else
{
if(this.age>other.age)
return 1;
else if(this.age<other.age)
return -1;
else
return this.name.compareTo(other.name);
}
}
}
class Manage
{
private ArrayList<Student>arrayList=new ArrayList<>();
private final String filename="student.dat";
public Manage(){
try{
FileInputStream file=new FileInputStream(new File(filename));
ObjectInputStream inputStream=new ObjectInputStream(file);
while(true)
{
Student stu=(Student)inputStream.readObject();
if(stu==null)
break;
arrayList.add(stu);
}
//先关闭对象流,再关闭文件流
inputStream.close();
file.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public void Menu(){
boolean flag=true;
Scanner input=new Scanner(System.in);
while(flag)
{
System.out.println("-----------------------------------");
System.out.println("| |");
System.out.println("|-------------1.添加学生-----------|");
System.out.println("| |");
System.out.println("|-------------2.修改学生-----------|");
System.out.println("| |");
System.out.println("|-------------3.删除学生-----------|");
System.out.println("| |");
System.out.println("|-------------4.查找学生-----------|");
System.out.println("| |");
System.out.println("|-------------5.显示学生-----------|");
System.out.println("| |");
System.out.println("|-------------6.退出系统-----------|");
System.out.println("| |");
System.out.println("-----------------------------------");
System.out.print("请输入您的选择:");
int choice=input.nextInt();
switch(choice){
case 1:
this.AddStudent();
break;
case 2:
this.ModifyStudent();
break;
case 3:
this.DeleteStudent();
break;
case 4:
this.FindStudent();
break;
case 5:
this.ShowStudent();
break;
case 6:
{
try{
FileOutputStream outputStream=new FileOutputStream(filename);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
for(Student stu:arrayList){
objectOutputStream.writeObject(stu);
}
objectOutputStream.close();
outputStream.close();
}
catch(Exception e){
e.printStackTrace();
}
flag=false;
break;
}
default:
break;
}
}
System.out.println("欢迎下次使用");
}
public void AddStudent(){
Scanner input=new Scanner(System.in);
try{
FileOutputStream outputStream=new FileOutputStream(filename,true);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
String name;
int id,age;
boolean flag=true;
while(flag){
System.out.print("请输入学号:");
id=input.nextInt();
//重新new Scanner,防止将换行符赋给name
input=new Scanner(System.in);
System.out.print("请输入姓名:");
name=input.nextLine();
System.out.print("请输入年龄:");
age=input.nextInt();
Student stu=new Student(id,name,age);
objectOutputStream.writeObject(stu);
arrayList.add(stu);
System.out.println("是否继续添加?");
System.out.println("1.Yes/2.No");
int choice=input.nextInt();
if(choice==2)
flag=false;
}
objectOutputStream.close();
outputStream.close();
System.out.println("添加成功");
}
catch(Exception e){
e.printStackTrace();
}
//等待1秒,方便实验者观察结果
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
public void ModifyStudent(){
Scanner input=new Scanner(System.in);
System.out.print("请输入要修改的学生姓名:");
String name=input.nextLine();
boolean flag=false;
for(int i=0;i<arrayList.size();++i)
{
if(name.equals(arrayList.get(i).getName()))
{
flag=true;
System.out.println(i+" "+arrayList.get(i).toString());
}
}
if(!flag)
{
System.out.println("修改失败");
}
else
{
System.out.print("请输入要修改的学生对应的序号:");
input=new Scanner(System.in);
int index=input.nextInt();
System.out.print("请输入学生学号:");
int id=input.nextInt();
System.out.print("请输入学生姓名:");
input=new Scanner(System.in);
name=input.nextLine();
System.out.print("请输入学生年龄:");
int age=input.nextInt();
arrayList.set(index,new Student(id,name,age));
try{
FileOutputStream outputStream=new FileOutputStream(filename);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
for(Student stu:arrayList){
objectOutputStream.writeObject(stu);
}
objectOutputStream.close();
outputStream.close();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("修改成功");
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
public void DeleteStudent(){
Map<Integer,Student>map=new HashMap<>();
Scanner input=new Scanner(System.in);
System.out.print("请输入学生姓名:");
String name=input.nextLine();
boolean flag=false;
for(int i=0;i<arrayList.size();++i)
{
if(name.equals(arrayList.get(i).getName()))
{
flag=true;
System.out.println(i+":"+arrayList.get(i).toString());
map.put(i,arrayList.get(i));
}
}
if(!flag)
{
System.out.println("删除失败");
}
else
{
System.out.print("请输入要删除的学生序号:");
int id=input.nextInt();
if(map.containsKey(id)){
arrayList.remove(map.get(id));
try{
FileOutputStream outputStream=new FileOutputStream(filename);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
for(Student stu:arrayList){
objectOutputStream.writeObject(stu);
}
objectOutputStream.close();
outputStream.close();
}
catch(Exception e){
e.printStackTrace();
}
}
else
{
System.out.println("删除失败");
}
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
public void FindStudent(){
Scanner input=new Scanner(System.in);
System.out.println("请输入学生姓名:");
String stuname=input.nextLine();
if(arrayList.size()==0)
{
System.out.println("当前系统无学生信息");
return;
}
else
{
boolean flag=false;
for(Student stu:arrayList){
if(stuname.equals(stu.getName()))
{
flag=true;
System.out.println(stu.toString());
}
}
if(!flag)
System.out.println("查无此人");
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
public void ShowStudent(){
if(arrayList.size()==0)
{
System.out.println("当前系统中无学生信息");
return;
}
int num=0;
for(Student stu:arrayList){
System.out.println((num++)+":"+stu.toString());
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
public class test{
public static void main(String[]args){
Manage manage=new Manage();
manage.Menu();
}
}
学生管理系统效果图
2.学生管理系统(图形化界面实现)
使用Java提供的Javax库来实现图形化界面,在使用这个库的时候,我发现它和Qt十分相似,但和Qt相比感觉更方便使用。
package project.demo;
import java.io.*;
import java.util.ArrayList;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//因为要写入对象文件,所以必须序列化
class Students implements Serializable{
private int id;
private String name;
private int age;
public Students(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String toString(){
return id+" "+name+" "+age;
}
public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
}
class ManageSystem{
private final static String filename="students.dat";
private ArrayList<Students>arrayList=new ArrayList<>();
//主界面
JFrame jFrame=new JFrame("Student Manage System");
//按钮
JButton addButton=new JButton("添加学生");
JButton modifyButton=new JButton("修改学生");
JButton deleteButton=new JButton("删除学生");
JButton searchButton=new JButton("查找学生");
JButton showButton=new JButton("显示学生");
JButton exitButton=new JButton("退出系统");
public ManageSystem(){
//设置界面的大小,位置,以及组件
jFrame.setSize(800,700);
jFrame.setLocation(600,200);
jFrame.setLayout(null);
addButton.setBounds(200,50,400,75);
modifyButton.setBounds(200,150,400,75);
deleteButton.setBounds(200,250,400,75);
searchButton.setBounds(200,350,400,75);
showButton.setBounds(200,450,400,75);
exitButton.setBounds(200,550,400,75);
jFrame.add(addButton);
jFrame.add(modifyButton);
jFrame.add(deleteButton);
jFrame.add(searchButton);
jFrame.add(showButton);
jFrame.add(exitButton);
addButton.setVisible(true);
modifyButton.setVisible(true);
deleteButton.setVisible(true);
searchButton.setVisible(true);
showButton.setVisible(true);
exitButton.setVisible(true);
//读取文件
try{
FileInputStream fileInputStream=new FileInputStream(filename);
ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
Students students=null;
while((students=(Students)objectInputStream.readObject())!=null)
arrayList.add(students);
objectInputStream.close();
fileInputStream.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//添加学生
public void AddStudent(){
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置新界面
JDialog addDialog=new JDialog(jFrame);
addDialog.setLayout(new FlowLayout());
//界面标题
addDialog.setTitle("添加学生");
addDialog.setSize(800,700);
addDialog.setLocation(600,200);
//设置相关标签和文本框
JLabel idLabel=new JLabel("学号:");
JTextField idField=new JTextField("");
idField.setPreferredSize(new Dimension(100,50));
JLabel nameLabel=new JLabel("姓名:");
JTextField nameField=new JTextField("");
nameField.setPreferredSize(new Dimension(100,50));
JLabel ageLabel=new JLabel("年龄:");
JTextField ageField=new JTextField("");
ageField.setPreferredSize(new Dimension(100,50));
idLabel.setVisible(true);
idField.setVisible(true);
nameLabel.setVisible(true);
nameField.setVisible(true);
ageLabel.setVisible(true);
ageField.setVisible(true);
//将组件添加进入副界面addDialog
addDialog.add(idLabel);
addDialog.add(idField);
addDialog.add(nameLabel);
addDialog.add(nameField);
addDialog.add(ageLabel);
addDialog.add(ageField);
addDialog.setVisible(true);
//设置提交按钮
JButton submitButton=new JButton("确定");
submitButton.setBounds(300,400,200,75);
submitButton.setVisible(true);
addDialog.add(submitButton);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取输入的内容
int id=Integer.parseInt(idField.getText());
String name=nameField.getText();
int age=Integer.parseInt(ageField.getText());
Students students=new Students(id,name,age);
arrayList.add(students);
//提交后返回主页面
addDialog.setVisible(false);
}
});
//设置返回按钮
JButton returnbutton=new JButton("返回");
returnbutton.setBounds(300,500,200,75);
returnbutton.setLayout(null);
returnbutton.setVisible(true);
addDialog.add(returnbutton);
returnbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//返回主页面
addDialog.setVisible(false);
}
});
jFrame.add(addDialog);
}
});
}
//修改学生信息
public void ModifyStudent(){
modifyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置新界面
JDialog modifyDialog=new JDialog(jFrame);
modifyDialog.setTitle("修改学生");
modifyDialog.setSize(800,700);
modifyDialog.setLocation(600,200);
modifyDialog.setLayout(null);
modifyDialog.setVisible(true);
//搜索条件
JLabel namelabel=new JLabel("姓名");
JTextField namefield=new JTextField("");
namelabel.setBounds(100,0,60,40);
namefield.setBounds(160,0,200,40);
namelabel.setLayout(null);
namefield.setLayout(null);
namelabel.setVisible(true);
namefield.setVisible(true);
modifyDialog.add(namelabel);
modifyDialog.add(namefield);
//设置查询按钮
JButton searchbutton=new JButton("查询");
searchbutton.setBounds(0,0,60,40);
searchbutton.setLayout(null);
searchbutton.setVisible(true);
modifyDialog.add(searchbutton);
searchbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//将符合条件的学生加入哈希表
Map<Integer,Students>map=new HashMap<>();
for(int i=0;i<arrayList.size();++i)
{
if(namefield.getText().equals(arrayList.get(i).getName())){
map.put(i,arrayList.get(i));
}
}
//显示结果
if(map.size()==0)
{
JButton resultbutton=new JButton("查无此人");
resultbutton.setBounds(300,200,200,50);
resultbutton.setLayout(null);
resultbutton.setVisible(true);
modifyDialog.add(resultbutton);
}
else
{
int num=0;
int height=40;
for(Map.Entry<Integer,Students>entry:map.entrySet()){
JLabel idlabel=new JLabel("学号");
JTextField idfield=new JTextField(entry.getValue().getId()+"");
JLabel nameLabel=new JLabel("姓名");
JTextField namefield=new JTextField(entry.getValue().getName());
JLabel ageLabel=new JLabel("年龄");
JTextField agefield=new JTextField(entry.getValue().getAge()+"");
idlabel.setBounds(60,40+height*num,60,height);
idfield.setBounds(120,40+height*num,180,height);
namelabel.setBounds(300,40+height*num,60,height);
namefield.setBounds(360,40+height*num,190,height);
ageLabel.setBounds(550,40+height*num,60,height);
agefield.setBounds(610,40+height*num,190,height);
idlabel.setLayout(null);
idfield.setLayout(null);
nameLabel.setLayout(null);
namefield.setLayout(null);
ageLabel.setLayout(null);
agefield.setLayout(null);
idlabel.setVisible(true);
idfield.setVisible(true);
namelabel.setVisible(true);
namefield.setVisible(true);
ageLabel.setVisible(true);
agefield.setVisible(true);
modifyDialog.add(idlabel);
modifyDialog.add(idfield);
modifyDialog.add(nameLabel);
modifyDialog.add(namefield);
modifyDialog.add(ageLabel);
modifyDialog.add(agefield);
//修改按钮
JButton yes=new JButton("修改");
yes.setBounds(0,40+num*height,60,height);
yes.setLayout(null);
yes.setVisible(true);
yes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String tempname=namefield.getText();
int tempid=Integer.parseInt(idfield.getText());
int tempage=Integer.parseInt(agefield.getText());
arrayList.get(entry.getKey()).setName(tempname);
arrayList.get(entry.getKey()).setId(tempid);
arrayList.get(entry.getKey()).setAge(tempage);
//修改后返回主页面
modifyDialog.setVisible(false);
}
});
modifyDialog.add(yes);
++num;
}
}
}
});
//设置返回按钮
JButton returnbutton=new JButton("返回");
returnbutton.setBounds(200,600,200,50);
returnbutton.setLayout(null);
returnbutton.setVisible(true);
modifyDialog.add(returnbutton);
returnbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
modifyDialog.setVisible(false);
}
});
jFrame.add(modifyDialog);
}
});
}
//删除学生
public void DeleteStudent(){
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置新界面
JDialog deleteDialog=new JDialog(jFrame);
deleteDialog.setTitle("删除学生");
deleteDialog.setSize(800,700);
deleteDialog.setLocation(600,200);
deleteDialog.setLayout(null);
deleteDialog.setVisible(true);
//搜索条件
JLabel namelabel=new JLabel("姓名");
JTextField namefield=new JTextField("");
namelabel.setBounds(100,0,60,40);
namefield.setBounds(160,0,200,40);
namelabel.setLayout(null);
namefield.setLayout(null);
namelabel.setVisible(true);
namefield.setVisible(true);
deleteDialog.add(namelabel);
deleteDialog.add(namefield);
//设置查询按钮
JButton searchbutton=new JButton("查询");
searchbutton.setBounds(0,0,60,40);
searchbutton.setLayout(null);
searchbutton.setVisible(true);
searchbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//筛选符合条件的学生
Map<Integer,Students>map=new HashMap<>();
for(int i=0;i<arrayList.size();++i)
{
if(namefield.getText().equals(arrayList.get(i).getName())){
map.put(i,arrayList.get(i));
}
}
if(map.size()==0)
{
JButton resultbutton=new JButton("查无此人");
resultbutton.setBounds(300,200,200,50);
resultbutton.setLayout(null);
resultbutton.setVisible(true);
deleteDialog.add(resultbutton);
}
else
{
int num=0;
int height=40;
for(Map.Entry<Integer,Students>entry:map.entrySet()){
JButton del=new JButton("删除");
del.setBounds(0,40+num*height,60,height);
del.setLayout(null);
del.setVisible(true);
del.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
arrayList.remove(entry.getValue());
//删除后返回主页面
deleteDialog.setVisible(false);
}
});
deleteDialog.add(del);
JTextField resultfield=new JTextField(entry.getValue().toString());
resultfield.setBounds(60,40+height*num,740,height);
resultfield.setLayout(null);
resultfield.setVisible(true);
deleteDialog.add(resultfield);
++num;
}
}
}
});
//设置返回按钮
JButton returnbutton=new JButton("返回");
returnbutton.setBounds(200,600,200,50);
returnbutton.setLayout(null);
returnbutton.setVisible(true);
deleteDialog.add(returnbutton);
returnbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteDialog.setVisible(false);
}
});
deleteDialog.add(searchbutton);
jFrame.add(deleteDialog);
}
});
}
//查找学生
public void SearchStudent(){
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置新界面
JDialog searchDialog=new JDialog(jFrame);
searchDialog.setTitle("查找学生");
searchDialog.setSize(800,700);
searchDialog.setLocation(600,200);
searchDialog.setLayout(null);
searchDialog.setVisible(true);
//设置相关标签和文本框
JLabel idLabel=new JLabel("学号:");
JTextField idField=new JTextField("0");
idField.setPreferredSize(new Dimension(100,50));
JLabel nameLabel=new JLabel("姓名:");
JTextField nameField=new JTextField("");
nameField.setPreferredSize(new Dimension(100,50));
JLabel ageLabel=new JLabel("年龄:");
JTextField ageField=new JTextField("0");
ageField.setPreferredSize(new Dimension(100,50));
int width=250;
int height=50;
idLabel.setBounds(60,0,40,40);
idField.setBounds(100,0,200,40);
nameLabel.setBounds(300,0,50,40);
nameField.setBounds(350,0,200,40);
ageLabel.setBounds(550,0,50,40);
ageField.setBounds(600,0,200,50);
idLabel.setLayout(null);
idField.setLayout(null);
nameLabel.setLayout(null);
nameField.setLayout(null);
ageLabel.setLayout(null);
ageField.setLayout(null);
idLabel.setVisible(true);
idField.setVisible(true);
nameLabel.setVisible(true);
nameField.setVisible(true);
ageLabel.setVisible(true);
ageField.setVisible(true);
//将组件添加进入副界面searchDialog
searchDialog.add(idLabel);
searchDialog.add(idField);
searchDialog.add(nameLabel);
searchDialog.add(nameField);
searchDialog.add(ageLabel);
searchDialog.add(ageField);
//设置查询按钮
JButton searchbutton=new JButton("查询");
searchbutton.setBounds(0,0,60,40);
searchbutton.setLayout(null);
searchbutton.setVisible(true);
searchDialog.add(searchbutton);
searchbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//根据查询条件进行筛选
int whichcase=0;
if(!nameField.getText().equals(""))
whichcase+=1;
if(!idField.getText().equals("0"))
whichcase+=2;
if(!ageField.getText().equals("0"))
whichcase+=4;
System.out.println(nameField.getText()+" "+idField.getText()+" "+ageField.getText());
//存储查询结果
Map<Integer,Students>map=new HashMap<>();
switch(whichcase){
case 0:
{
break;
}
case 1:
{
// System.out.println(tempname);
for(int i=0;i<arrayList.size();++i)
{
if(nameField.getText().equals(arrayList.get(i).getName()))
{
map.put(i,arrayList.get(i));
}
}
break;
}
case 2:
{
int tempid=Integer.parseInt(idField.getText());
for(int i=0;i<arrayList.size();++i)
{
if(tempid==arrayList.get(i).getId())
map.put(i,arrayList.get(i));
}
break;
}
case 3:
{
String tempname=nameField.getText();
int tempid=Integer.parseInt(idField.getText());
for(int i=0;i<arrayList.size();++i)
{
if(tempname.equals(arrayList.get(i).getName())&&tempid==arrayList.get(i).getId())
map.put(i,arrayList.get(i));
}
break;
}
case 4:
{
int tempage=Integer.parseInt(ageField.getText());
for(int i=0;i<arrayList.size();++i)
{
if(tempage==arrayList.get(i).getAge())
map.put(i,arrayList.get(i));
}
break;
}
case 5:
{
String tempname=nameField.getText();
int tempage=Integer.parseInt(ageField.getText());
for(int i=0;i<arrayList.size();++i)
{
if(tempname.equals(arrayList.get(i).getName())&&tempage==arrayList.get(i).getAge())
map.put(i,arrayList.get(i));
}
break;
}
case 6:
{
int tempid=Integer.parseInt(idField.getText());
int tempage=Integer.parseInt(ageField.getText());
for(int i=0;i<arrayList.size();++i)
{
if(tempid==arrayList.get(i).getId()&&tempage==arrayList.get(i).getAge()){
map.put(i,arrayList.get(i));
}
}
break;
}
case 7:
{
String tempname=nameField.getText();
int tempid=Integer.parseInt(idField.getText());
int tempage=Integer.parseInt(ageField.getText());
for(int i=0;i<arrayList.size();++i)
{
if(tempname.equals(arrayList.get(i).getName())&&tempid==arrayList.get(i).getId()
&&tempage==arrayList.get(i).getAge()){
map.put(i,arrayList.get(i));
}
}
break;
}
}
int num=0;
for(Map.Entry<Integer,Students>entry:map.entrySet()){
System.out.println(entry.getValue().toString());
JButton tempindexbutton=new JButton(entry.getKey()+"");
JButton tempidbutton=new JButton(entry.getValue().getId()+"");
JButton tempnamebutton=new JButton(entry.getValue().getName());
JButton tempagebutton=new JButton(entry.getValue().getAge()+"");
tempindexbutton.setBounds(0,40+height*num,50,height);
tempidbutton.setBounds(50,40+height*num,width,height);
tempnamebutton.setBounds(300,40+height*num,width,height);
tempagebutton.setBounds(550,40+height*num,width,height);
tempindexbutton.setLayout(null);
tempidbutton.setLayout(null);
tempnamebutton.setLayout(null);
tempagebutton.setLayout(null);
tempindexbutton.setVisible(true);
tempidbutton.setVisible(true);
tempnamebutton.setVisible(true);
tempagebutton.setVisible(true);
searchDialog.add(tempindexbutton);
searchDialog.add(tempidbutton);
searchDialog.add(tempnamebutton);
searchDialog.add(tempagebutton);
++num;
}
if(map.size()==0)
{
System.out.println("查无此人");
JButton resultbutton=new JButton("查无此人");
resultbutton.setBounds(300,200,200,50);
resultbutton.setLayout(null);
resultbutton.setVisible(true);
searchDialog.add(resultbutton);
}
}
});
//设置返回按钮
//设置返回按钮
JButton returnbutton=new JButton("返回");
returnbutton.setBounds(200,600,width,height);
returnbutton.setLayout(null);
returnbutton.setVisible(true);
searchDialog.add(returnbutton);
returnbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchDialog.setVisible(false);
}
});
}
});
}
//显示学生
public void ShowStudent(){
showButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置新界面
JDialog showDialog=new JDialog(jFrame);
showDialog.setTitle("显示学生");
showDialog.setSize(800,700);
showDialog.setLocation(600,200);
showDialog.setLayout(null);
showDialog.setVisible(true);
JButton idbutton=new JButton("id");
idbutton.setBounds(50,0,250,50);
idbutton.setLayout(null);
JButton namebutton=new JButton("name");
namebutton.setBounds(300,0,250,50);
namebutton.setLayout(null);
JButton agebutton=new JButton("age");
agebutton.setBounds(550,0,250,50);
agebutton.setLayout(null);
idbutton.setVisible(true);
namebutton.setVisible(true);
agebutton.setVisible(true);
showDialog.add(idbutton);
showDialog.add(namebutton);
showDialog.add(agebutton);
//数量
int num=0;
int height=40;
int width=250;
while(num<arrayList.size()){
//设置按钮来显示数据
JButton numbutton=new JButton(num+"");
JButton tempidbutton=new JButton(arrayList.get(num).getId()+"");
JButton tempnamebutton=new JButton(arrayList.get(num).getName());
JButton tempagebutton=new JButton(arrayList.get(num).getAge()+"");
numbutton.setBounds(0,50+height*num,50,height);
tempidbutton.setBounds(50,50+height*num,width,height);
tempnamebutton.setBounds(300,50+height*num,width,height);
tempagebutton.setBounds(550,50+height*num,width,height);
numbutton.setLayout(null);
tempidbutton.setLayout(null);
tempnamebutton.setLayout(null);
tempagebutton.setLayout(null);
numbutton.setVisible(true);
tempidbutton.setVisible(true);
tempnamebutton.setVisible(true);
tempagebutton.setVisible(true);
showDialog.add(numbutton);
showDialog.add(tempidbutton);
showDialog.add(tempnamebutton);
showDialog.add(tempagebutton);
++num;
}
//设置返回按钮
JButton returnbutton=new JButton("返回");
returnbutton.setBounds(200,600,width,height);
returnbutton.setLayout(null);
returnbutton.setVisible(true);
showDialog.add(returnbutton);
returnbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showDialog.setVisible(false);
}
});
jFrame.add(showDialog);
}
});
}
//退出系统
public void ExitButton(){
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//前期的添加,删除,修改等工作,都只作用于ArrayList,最后退出系统时,再更新文件
try{
FileOutputStream fileOutputStream=new FileOutputStream(filename);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
for(Students stu:arrayList)
{
objectOutputStream.writeObject(stu);
}
objectOutputStream.close();
fileOutputStream.close();
}
catch(Exception ex){
ex.printStackTrace();
}
jFrame.setVisible(false);
}
});
}
//菜单
public void Menu(){
//添加监听器
this.AddStudent();
this.ModifyStudent();
this.DeleteStudent();
this.SearchStudent();
this.ShowStudent();
this.ExitButton();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
public class hello {
public static void main(String[]args)
{
ManageSystem manageSystem=new ManageSystem();
manageSystem.Menu();
}
}
效果图:
1.主页面
2.添加学生页面
3.查找学生页面
4.总结
该图形化界面核心代码其实和第一个在控制台操作的管理系统基本一样,只是进行图形化,使操作更方便而已,但是在图形化的时候,发现有许多bug,首先就是每次操作后,必须按退出系统按钮,进行的操作才能写入文件。其次就是删除,查找和修改页面,输入查询条件后,鼠标必须经过相应的区域,该区域上的结果才会显示出来。最后还有一点,就是重复的代码太多了,可以将一些代码封装成一个函数,需要时再调用。
3.学生管理系统(连接MySQL数据库)
1.打开CMD,要以管理员的身份运行
cd "Mysql所在文件夹"
//启动mysql
net start mysql
//登录MySQL
mysql -u root -p
然后输入密码文章来源:https://www.toymoban.com/news/detail-491308.html
//创建数据库
CREATE DATABASE shop;
创建shop数据库中的表
在表中插入信息
在MySQL进行上述操作后,编写下面的JAVA代码文章来源地址https://www.toymoban.com/news/detail-491308.html
package project.demo;
import java.sql.*;
import java.util.ArrayList;
import java.util.*;
import java.io.*;
import java.io.*;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
class ManageSystem
{
//驱动名,Java8及以上都是这个
private String driver="com.mysql.cj.jdbc.Driver";
//数据库用户名
private String user="root";
//数据库密码
private String password="3fa4d180";
//要使用的数据库
private String database="shop";
//数据库路径
private String url=null;
public ManageSystem(){
//初始化数据库路径
url="jdbc:mysql://localhost:3306/"+database+"?useSSL=false&serverTimezone=UTC";
}
public void Menu(){
boolean flag=true;
Scanner input=new Scanner(System.in);
while(flag)
{
System.out.println("----------------------------------");
System.out.println("| |");
System.out.println("|-------------1.添加学生-----------");
System.out.println("| |");
System.out.println("|-------------2.修改学生-----------");
System.out.println("| |");
System.out.println("|-------------3.删除学生-----------");
System.out.println("| |");
System.out.println("|-------------4.查找学生-----------");
System.out.println("| |");
System.out.println("|-------------5.显示学生-----------");
System.out.println("| |");
System.out.println("|-------------6.退出系统-----------");
System.out.println("| |");
System.out.println("|---------------------------------");
System.out.print("请输入您的选择:");
//输入选择
int choice=input.nextInt();
switch(choice)
{
case 1:
this.AddStudent();
break;
case 2:
this.ModifyStudent();
break;
case 3:
this.DeleteStudent();
break;
case 4:
this.SearchStudent();
break;
case 5:
this.ShowStudent();
break;
case 6:
flag=false;
break;
default:
break;
}
}
System.out.println("欢迎下次使用");
}
//添加学生
public void AddStudent(){
Scanner input=new Scanner(System.in);
try{
//加载驱动
Class.forName(driver);
//连接到数据库
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println("连接成功");
//设置容器
Statement stmt=conn.createStatement();
boolean flag=true;
while(flag)
{
System.out.print("请输入学生id:");
int id=input.nextInt();
input=new Scanner(System.in);
System.out.print("请输入学生姓名:");
String name=input.nextLine();
System.out.print("请输入学生年龄:");
int age=input.nextInt();
//SQL语句
String execute="INSERT INTO Student VALUES";
//在Mysql的添加语句中,若添加id=202004,name=刘六,age=20,添加语句为INSERT INTO Student VALUES('202004','刘六',20);
//即字符需要用"'"包起来
execute+=("("+"'"+id+"'"+","+"'"+name+"'"+","+age+");");
//将SQL语句上传到数据库执行
stmt.executeUpdate(execute);
System.out.println("插入成功");
System.out.println("是否继续插入数据?");
System.out.println("1.是");
System.out.println("2.否");
System.out.print("请输入您的选择:");
int choice=input.nextInt();
if(choice!=1)
{
flag=false;
}
}
//关闭容器和通道
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
//停顿1秒,方便用户查看结果
try{
Thread.sleep(1000);
}
catch (Exception e){
e.printStackTrace();
}
}
//修改学生
public void ModifyStudent(){
Scanner input=new Scanner(System.in);
System.out.print("请输入要修改的学生姓名:");
String name=input.nextLine();
System.out.println("请输入要修改的内容:");
System.out.println("1.id");
System.out.println("2.name");
System.out.println("3.age");
System.out.println("4.all");
System.out.print("请输入您的选择:");
int choice=input.nextInt();
//SQL语句
String execute="";
boolean flag=true;
while(flag){
if(choice<1||choice>4)
{
System.out.println("输入有误,请重新输入");
}
else
{
flag=false;
if(choice==1){
System.out.print("请输入新id:");
int newid=input.nextInt();
execute="UPDATE Student SET stu_id="+"'"+newid+"'";
}
else if(choice==2){
input=new Scanner(System.in);
System.out.print("请输入新姓名:");
String newname=input.nextLine();
execute="UPDATE Student SET stu_name="+"'"+newname+"'";
}
else if(choice==3){
System.out.print("请输入新年龄:");
int newage=input.nextInt();
execute="UPDATE Student SET stu_age="+"'"+newage+"'";
}
else
{
System.out.print("请输入新id:");
int newid=input.nextInt();
input=new Scanner(System.in);
System.out.print("请输入新姓名:");
String newname=input.nextLine();
System.out.print("请输入新年龄:");
int newage=input.nextInt();
execute="UPDATE Student SET stu_id="+"'"+newid+"'";
execute+=",stu_name="+"'"+newname+"'";
execute+=",stu_age="+"'"+newage+"'";
}
}
}
execute+=" WHERE stu_name="+"'"+name+"'"+";";
try{
//设置驱动
Class.forName(driver);
//连接数据库
Connection conn=DriverManager.getConnection(url,user,password);
//设置容器
Statement stmt=conn.createStatement();
//执行SQL语句
stmt.executeUpdate(execute);
System.out.println("修改成功");
//关闭容器和数据库
stmt.close();
conn.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
//停顿1秒,方便用户观察结果
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
//删除学生
public void DeleteStudent(){
Scanner input=new Scanner(System.in);
System.out.println("请输入要删除的学生姓名:");
String name=input.nextLine();
try{
//加载驱动
Class.forName(driver);
//连接数据库
Connection conn=DriverManager.getConnection(url,user,password);
//设置容器
Statement stmt=conn.createStatement();
//SQL语句
String exceute="DELETE FROM Student WHERE ";
exceute+="stu_name="+"'"+name+"'"+";";
//执行SQL语句
stmt.executeUpdate(exceute);
System.out.println("删除成功");
//关闭容器和数据库
stmt.close();
conn.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
//停顿1秒,方便用户观察
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
//查找学生
public void SearchStudent(){
Scanner input=new Scanner(System.in);
System.out.println("请输入查询条件");
System.out.println("1.id");
System.out.println("2.name");
System.out.println("3.age");
System.out.print("请输入您的选择:");
String execute="";
boolean flag=true;
while(flag){
int choice=input.nextInt();
if(choice<1||choice>3)
{
System.out.println("输入有误,请重新输入");
}
else if(choice==1){
System.out.print("请输入要查询的id:");
int id=input.nextInt();
execute="SELECT * FROM Student WHERE stu_id="+"'"+id+"'"+";";
flag=false;
}
else if(choice==2){
input=new Scanner(System.in);
System.out.print("请输入要查询的姓名:");
String name=input.nextLine();
execute="SELECT * FROM Student WHERE stu_name="+"'"+name+"'"+";";
flag=false;
}
else
{
System.out.print("请输入要查询的年龄:");
int age=input.nextInt();
execute="SELECT * FROM Student WHERE stu_age="+age+";";
flag=false;
}
}
try{
//加载驱动
Class.forName(driver);
//连接数据库
Connection conn=DriverManager.getConnection(url,user,password);
//设置容器
Statement stmt=conn.createStatement();
//获得集合
ResultSet rs=stmt.executeQuery(execute);
int num=0;
//遍历
while(rs.next())
{
int id=Integer.parseInt(rs.getString("stu_id"));
String name=rs.getString("stu_name");
int age=Integer.parseInt(rs.getString("stu_age"));
System.out.println(String.format("id:%-10d name:%-20s age:%-5d",id,name,age));
num++;
}
if(num!=0)
System.out.println("查询成功");
else
System.out.println("查无此人");
//关闭集合,容器和数据库
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
//停顿1秒,方便用户观察
try{
Thread.sleep(1000);
}
catch (Exception e){
e.printStackTrace();
}
}
//显示学生
public void ShowStudent(){
try{
//加载驱动
Class.forName(driver);
//连接数据库
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println("连接成功");
//设置容器
Statement stmt=conn.createStatement();
//SQL语句
String execute="SELECT * FROM Student";
//获得集合
ResultSet rs= stmt.executeQuery(execute);
System.out.println("查询成功");
while(rs.next()){
int id=Integer.parseInt(rs.getString("stu_id"));
String name=rs.getString("stu_name");
int age=Integer.parseInt(rs.getString("stu_age"));
System.out.println(String.format("id:%-10d name:%-20s age:%-5d",id,name,age));
}
//关闭集合,容器和数据库
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
//停顿1秒,方便用户观察
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class test{
public static void main(String[]args){
ManageSystem manageSystem=new ManageSystem();
manageSystem.Menu();
}
}
到了这里,关于学生管理系统(Java实现)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!