代码直接上,一共十个类,三个对话框类,一个学生信息类,一个学生操作类,一个密码信息类,一个密码操作类,另加界面类和一个主类
package jiemian;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import jiemian.STxinxi;
import jiemian.Student;
public class Jm extends JFrame implements ActionListener {
private JButton button1,button2,button3,button4,button5,button6,button7;
private JTextField text1,text2,text3,text4;
private JTextField text5,text6;
private JLabel label1,label2,label3,label4,label5;
private JTable tabel;
private Mydialog dialog=new Mydialog(this);
List<STxinxi>sc=new ArrayList<STxinxi>();
public Jm() throws IOException {
this.setTitle("学生管理系统");
this.setBounds(100, 100, 800, 800);
this.setLayout(null);
button1=new JButton("添加信息");
button1.setBounds(400,350, 100, 40);
button2=new JButton("修改信息");
button2.setBounds(600,350, 100, 40);
button3=new JButton("查询信息");
button3.setBounds(600, 520, 100, 40);
button7=new JButton("删除信息");
button7.setBounds(400, 410, 100, 40);
Font f = new Font("黑体", Font.BOLD, 40);
label1=new JLabel("学号:");label1.setBounds(400, 10, 70, 60);
label2=new JLabel("姓名:");label2.setBounds(400, 70, 70, 60);
label3=new JLabel("专业:");label3.setBounds(400, 130, 70, 60);
label4=new JLabel("年龄:");label4.setBounds(400, 190, 70, 60);
label5=new JLabel("请输入查询学号:");label5.setBounds(10, 520, 140, 60);
text1=new JTextField();text1.setBounds(470, 10, 200, 60);
text2=new JTextField();text2.setBounds(470, 70, 200, 60);
text3=new JTextField();text3.setBounds(470, 130, 200, 60);
text4=new JTextField();text4.setBounds(470, 190, 200, 60);
text5=new JTextField();text5.setBounds(150, 520, 300, 60);
text6=new JTextField();text6.setBounds(10, 600, 500, 60);
Student a=new Student();
a.read();
sc=a.getlist();
int t=sc.size();
String[] tabelValues= {"学号","姓名","专业","年龄"};
String[][] columnName=new String[t+1][4];
for (int i=0;i<sc.size();i++) {
columnName[i][0]=sc.get(i).getStudent_number();
columnName[i][1]=sc.get(i).getStudent_name();
columnName[i][2]=sc.get(i).getStudent_type();
columnName[i][3]=sc.get(i).getStudent_age()+"";
}
tabel=new JTable(columnName,tabelValues);
JScrollPane scrotable=new JScrollPane(tabel);
scrotable.setBounds(0, 0, 400, 500);
this.add(button1);
this.add(button2);
this.add(button3);
this.add(button7);
this.add(label1);
this.add(label2);
this.add(label3);
this.add(label4);
this.add(label5);
this.add(text1);
this.add(text2);
this.add(text3);
this.add(text4);
this.add(text5);
this.add(text6);
this.add(scrotable);
button1.addActionListener(this);
button3.addActionListener(this);
button7.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
String ss=null;
String button=arg0.getActionCommand();
if (button.equals("删除信息")) {
Mydialog2 p=new Mydialog2();
p.setVisible(true);
}
if (button.equals("添加信息")) {
button5=new JButton("确定");
button6=new JButton("取消");
String id=text1.getText();
String name=text2.getText();
String type=text3.getText();
int age=Integer.parseInt(text4.getText());
Student a=new Student();
try {
a.read();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
dialog.setVisible(true);
if(dialog.getorder().equals("确定"))
{ a.add(id, name,type, age);
try {
a.writer();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
}
if(dialog.getorder().equals("取消")) {
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
}
}
if (button.equals("查询信息")) {
Student a=new Student();
boolean bool=false;
try {
a.read();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
sc=a.getlist();
for (int i=0;i<sc.size();i++) {
if (sc.get(i).getStudent_number().equals(text5.getText()))
{
ss="学号:"+sc.get(i).getStudent_number()+" 姓名:"+sc.get(i).getStudent_name()+" 专业:"+sc.get(i).getStudent_type()+" 年龄:"+sc.get(i).getStudent_age();
bool=true;
}
}
if (bool) {
text6.setText(ss);}
else {
JDialog dg=new JDialog();
JLabel lb=new JLabel("查无此学生");
dg.setLayout(new GridLayout(1,0));
dg.setTitle("警告");
dg.setLocation(600, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
try {
a.writer();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
package jiemian;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Mydialog extends JDialog {
private JButton button1,button2;
private JLabel label1,label2;
private String s1;
public Mydialog(JFrame parent) {
super(parent,"警告",true);
this.setLocation(600, 400);
this.setSize(200, 160);
this.setLayout(new GridLayout(2,2));
button1=new JButton("确定");
button2=new JButton("取消");
label1=new JLabel("确定添加信息");
label2=new JLabel();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s1="确定";
dispose();
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s1="取消";
dispose();
}
});
this.add(label1);
this.add(label2);
this.add(button1);
this.add(button2);
}
public String getorder() {
return s1;
}
}
package jiemian;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Mydialog1 extends JDialog implements ActionListener{
private JLabel label1,label2;
private JTextField text1,text2;
private JButton button1,button2,button3;
List<userinform> user=new ArrayList<userinform>();
public Mydialog1(JFrame parent) {
//super(parent,"警告",true);
this.setTitle("注冊信息");
this.setLayout(null);
this.setSize(500, 400);
this.setLocation(600, 500);
label1=new JLabel("用户名");label1.setBounds(120, 120, 50, 30);
label2=new JLabel("密码");label2.setBounds(120, 160, 50, 30);
text1=new JTextField();text1.setBounds(170, 120, 190, 30);
text2=new JTextField();text2.setBounds(170, 160, 190, 30);
button1=new JButton("确定");button1.setBounds(200, 300, 80, 30);
button2=new JButton("取消");button2.setBounds(300, 300, 80, 30);
button3=new JButton("验证");button3.setBounds(390, 120,80, 30);
this.add(button1);
this.add(button2);
this.add(text1);
this.add(text2);
this.add(button3);
this.add(label1);
this.add(label2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String button=e.getActionCommand();
if (button.equals("验证")) {
user a=new user();
boolean bool=true;
try {
a.read();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
user=a.getlist();
for (int i=0;i<user.size();i++) {
if (user.get(i).getName().equals(text1.getText())) {
bool=false;
}
}
if (bool) {
JDialog dg=new JDialog();
JLabel lb=new JLabel("用户名可用");
dg.setLayout(new GridLayout(1,0));
dg.setTitle("恭喜");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
else {
JDialog dg=new JDialog();
JLabel lb=new JLabel("用户名存在");
dg.setLayout(new GridLayout(1,0));
dg.setTitle("警告");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
}
if(button.equals("确定")) {
user a=new user();
boolean bool=true;
try {
a.read();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
user=a.getlist();
for (int i=0;i<user.size();i++) {
if (user.get(i).getName().equals(text1.getText())) {
bool=false;
}
}
if (bool) {
a.add(text1.getText(),text2.getText());
try {
a.writer();
} catch (IOException d) {
// TODO 自动生成的 catch 块
d.printStackTrace();
}
this.setVisible(false);
JDialog dg=new JDialog();
JLabel lb=new JLabel("添加成功");
dg.setLayout(new GridLayout(1,0));
dg.setTitle("恭喜");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
else {
JDialog dg=new JDialog();
JLabel lb=new JLabel("用户已存在");
dg.setLayout(new GridLayout(1,0));
dg.setTitle("警告!");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
}
if(button.equals("取消")) {
this.setVisible(false);
}
}
}
package jiemian;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Mydialog2 extends JDialog implements ActionListener {
private JLabel label1;
private JButton button1, button2;
private JTextField text1;
List<STxinxi> sc = new ArrayList<STxinxi>();
public Mydialog2() {
this.setTitle("删除学生信息");
this.setSize(500, 400);
this.setLocation(600, 500);
this.setLayout(null);
label1 = new JLabel("输入删除学号");
label1.setBounds(110, 100, 80, 30);
text1 = new JTextField();
text1.setBounds(210, 100, 150, 30);
button1 = new JButton("确定");
button1.setBounds(130, 260, 80, 30);
button2 = new JButton("取消");
button2.setBounds(280, 260, 80, 30);
this.add(label1);
this.add(text1);
this.add(button1);
this.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent arg0) {
String button = arg0.getActionCommand();
List<STxinxi> ss = new ArrayList<STxinxi>();
boolean bool = false;
if (button.equals("确定")) {
Student a = new Student();
try {
a.read();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
sc = a.getlist();
for (STxinxi sc1 : sc) {
if (sc1.getStudent_number().equals(text1.getText())) {
ss.add(sc1);
bool = true;
}
}
if (bool) {
sc.removeAll(ss);
JDialog dg = new JDialog();
JLabel lb = new JLabel("删除成功");
dg.setLayout(new GridLayout(1, 0));
dg.setTitle("恭喜");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
this.setVisible(false);
dg.setVisible(true);
try {
a.writer();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} else {
JDialog dg = new JDialog();
JLabel lb = new JLabel("未找到用户");
dg.setLayout(new GridLayout(1, 0));
dg.setTitle("抱歉");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
}
if (button.equals("取消")) {
this.setVisible(false);
}
}
}
package jiemian;
import java.util.*;
import java.io.*;
public class Student {
Scanner cin = new Scanner(System.in);
List<STxinxi> sc = new ArrayList<STxinxi>();
public void read() throws IOException {
File fileNew = new File("D:\\student\\student.txt");
File rootFile = fileNew.getParentFile();
if (!fileNew.exists()) {
try {
rootFile.mkdirs();
fileNew.createNewFile();
} catch (IOException e) {
System.out.println(fileNew + "文件创建失败");
}
}
BufferedReader read = new BufferedReader(new FileReader("D:\\student\\student.txt"));
String stu = read.readLine();
while (null != stu) {
String[] stu1 = stu.split(" ");
STxinxi sc1 = new STxinxi();
sc1.setStudent_number(stu1[0]);
sc1.setStudent_name(stu1[1]);
sc1.setStudent_type(stu1[2]);
sc1.setStudent_age(Integer.parseInt(stu1[3]));
sc.add(sc1);
stu = read.readLine();
}
read.close();
}
public void add() {
System.out.println("请输入学号,姓名,年龄,专业");
String student_id = cin.next();
String student_name = cin.next();
int student_age = cin.nextInt();
String student_type = cin.next();
STxinxi sc1 = new STxinxi();
sc1.setStudent_age(student_age);
sc1.setStudent_name(student_name);
sc1.setStudent_number(student_id);
sc1.setStudent_type(student_type);
sc.add(sc1);
}
public void add(String student_id, String student_name, String student_type, int student_age) {
STxinxi sc1 = new STxinxi();
sc1.setStudent_age(student_age);
sc1.setStudent_name(student_name);
sc1.setStudent_number(student_id);
sc1.setStudent_type(student_type);
sc.add(sc1);
}
public List getlist() {
return sc;
}
public void show() {
for (STxinxi sc1 : sc) {
System.out.println(sc1.showAll());
}
}
public void writer() throws IOException {
FileOutputStream out = new FileOutputStream("D:\\student\\student.txt");
for (STxinxi sc1 : sc) {
out.write(sc1.getAll().getBytes());
}
out.close();
}
}
package jiemian;
public class STxinxi {
private int student_age;
private String student_type;
private String student_number;
private String student_name;
public STxinxi(String student_number,String student_name,String student_type,int student_age) {
super();
this.student_age= student_age;
this.student_type = student_type;
this.student_number = student_number;
this.student_name = student_name;
}
public STxinxi() {
super();
}
public int getStudent_age() {
return student_age;
}
public void setStudent_age(int student_grade) {
this.student_age = student_grade;
}
public String getStudent_type() {
return student_type;
}
public void setStudent_type(String student_type) {
this.student_type = student_type;
}
public String getStudent_number() {
return student_number;
}
public void setStudent_number(String student_number) {
this.student_number = student_number;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String showAll() {
return (getStudent_number()+" "+getStudent_name()+" "+getStudent_type()+" "+getStudent_age());
}
public String getAll() {
return (getStudent_number()+" "+getStudent_name()+" "+getStudent_type()+" "+getStudent_age()+"\r\n");
}
}
package jiemian;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class user {
List<userinform> user=new ArrayList<userinform>();
public void read() throws IOException {
File fileNew = new File("D:\\use\\use.txt");
File rootFile = fileNew.getParentFile();
if (!fileNew.exists()) {
try {
rootFile.mkdirs();
fileNew.createNewFile();
} catch (IOException e) {
System.out.println(fileNew + "文件创建失败");
}
}
BufferedReader read = new BufferedReader(new FileReader("D:\\use\\use.txt"));
String use = read.readLine();
while (null != use) {
String[] stu1 = use.split(" ");
userinform use1 = new userinform();
use1.setName(stu1[0]);
use1.setMima(stu1[1]);
user.add(use1);
use = read.readLine();
}
read.close();
}
public List getlist() {
return user;
}
public void add(String name,String mima) {
userinform use=new userinform();
use.setName(name);
use.setMima(mima);
user.add(use);
}
public void writer() throws IOException {
FileOutputStream out = new FileOutputStream("D:\\use\\use.txt");
for (userinform sc1 : user) {
out.write(sc1.getAll().getBytes());
}
out.close();
}
}
package jiemian;
public class userinform {
private String name;
private String mima;
public userinform(String name,String mima) {
super();
this.name=name;
this.mima=mima;
}
public userinform() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
public String getAll() {
return getName()+" "+getMima()+"\r\n";
}
}
package jiemian;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Yanzheng extends JFrame implements ActionListener {
private JTextField text1,text3;
private JButton button1,button2,button3;
private JLabel label1,label2;
private JPasswordField text2;
List<userinform> user=new ArrayList<userinform>();
public Yanzheng() {
this.setTitle("用户登录");
this.setLayout(null);
this.setSize(500, 400);
this.setLocation(300, 200);
label1=new JLabel("用户名:");label1.setBounds(150, 130, 50, 30);
label2=new JLabel("密码:");label2.setBounds(150, 160, 50, 30);
text1=new JTextField();text1.setBounds(200, 130, 200, 30);
text2=new JPasswordField();text2.setBounds(200, 160, 200, 30);
button1=new JButton("确定");button1.setBounds(130, 300, 80, 30);
button2=new JButton("取消");button2.setBounds(320, 300, 80, 30);
button3=new JButton("注册");button3.setBounds(400, 130, 60, 30);
this.add(label1);
this.add(label2);
this.add(text1);
this.add(text2);
this.add(button1);
this.add(button2);
this.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
String button=arg0.getActionCommand();
if (button.equals("确定")) {
user a=new user();
boolean bool=false;
try {
a.read();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
user=a.getlist();
for (int i=0;i<user.size();i++) {
if (user.get(i).getName().equals(text1.getText())) {
if (user.get(i).getMima().equals(text2.getText())) {
bool=true;
try {
Jm b=new Jm();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
this.setVisible(false);
}
}
}
if (bool) {
JDialog dg=new JDialog();
JLabel lb=new JLabel("登陆成功");
dg.setLayout(new GridLayout(1,0));
dg.setTitle("恭喜");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
else {
JDialog dg=new JDialog();
JLabel lb=new JLabel("输入信息有误");
dg.setLayout(new GridLayout(1,0));
dg.setTitle("警告");
dg.setLocation(1000, 580);
dg.setSize(200, 100);
dg.add(lb);
dg.setVisible(true);
}
}
if (button.equals("取消"))this.setVisible(false);
if (button.equals("注册")) {
Mydialog1 e=new Mydialog1(this);
e.setVisible(true);
}
}
}
package jiemian;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Yanzheng a=new Yanzheng();
}
}