做java,实现界面、文件和arraylist运用的结合,我对文件的使用不是很熟悉,所以不知道哪里出了问题,希望·能帮我让这个代码成功运行。
package 班级管理系统;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class ClassGui {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Myframe("欢迎");
}
}
class Myframe extends JFrame{
public Myframe(String title) {
super(title);
init();
}
public Myframe() {
super();
init();
}
private void init() {
// TODO Auto-generated method stub
Myframe thisframe=Myframe.this;
Container container=this.getContentPane();
this.setBounds(200, 200,500, 500);
this.setLayout(null);
ImageIcon img = new ImageIcon("src/班级管理系统/xu.jpg");
//要设置的背景图片
JLabel imgLabel = new JLabel(img);
//将背景图放在标签里。
this.getLayeredPane().add(imgLabel,new Integer(Integer.MIN_VALUE));
//将背景标签添加到jfram的LayeredPane面板里。
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
// 设置背景标签的位置
((JPanel) container).setOpaque(false);
// 将内容面板设为透明。将LayeredPane面板中的背景显示出来。
JLabel label=new JLabel("班级管理系统");
label.setFont(new Font("黑体",Font.BOLD,32));
label.setBounds(100, 10, 300, 100);
JPanel mainPanel=new JPanel();
// mainPanel.setLayout(new GridLayout(2,2));
mainPanel.setBounds(100, 100, 250, 270);
JLabel nameLabel=new JLabel("账号:");
JLabel pswLabel=new JLabel("密码:");
JTextField nameTF=new JTextField(18);
JPasswordField pswPF=new JPasswordField(18);
mainPanel.add(nameLabel);
mainPanel.add(nameTF);
mainPanel.add(pswLabel);
mainPanel.add(pswPF);
JPanel btnPanel=new JPanel();
btnPanel.setLayout(new GridLayout(2,1));
btnPanel.setBounds(120, 200, 230, 50);
JButton okBtn=new JButton("登陆");
okBtn.setSize(120,50);
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if((nameTF.getText().length()!=0)&&(new String(pswPF.getPassword()).length()!=0))
{
thisframe.setVisible(false);
new Myframe2(nameTF.getText());
}
if(nameTF.getText().length()==0) {
new NameJdialog();
}
}
});
JButton cancelBtn=new JButton("取消");
cancelBtn.setSize(120, 50);
cancelBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
// System.exit(0);//退出系统
thisframe.dispose();//窗口资源释放
}
});
btnPanel.add(okBtn);
btnPanel.add(cancelBtn);
container.add(label);
container.add(mainPanel);
container.add(btnPanel);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Myframe2 extends JFrame implements ActionListener{
public Myframe2(String msg) {
init(msg);
}
private void init(String msg) {
// TODO Auto-generated method stub
Myframe2 thisjframe=Myframe2.this;
Function func=new Function();
this.setBounds(200, 200, 400, 400);
this.setLayout(null);
Container cont=this.getContentPane();
ImageIcon img = new ImageIcon("src/班级管理系统/zhi.jpg");
//要设置的背景图片
JLabel imgLabel = new JLabel(img);
//将背景图放在标签里。
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
//将背景标签添加到jfram的LayeredPane面板里。
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
// 设置背景标签的位置
((JPanel) cont).setOpaque(false);
// 将内容面板设为透明。将LayeredPane面板中的背景显示出来。
JLabel label=new JLabel(msg+",你已经成功进入主菜单");
label.setBounds(100, 10, 200, 50);
JPanel btnPanel=new JPanel();
btnPanel.setLayout(new GridLayout(3,3));
btnPanel.setBounds(10, 100, 400, 70);
JButton add=new JButton("添加");
add.setSize(120,50);
add.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
thisjframe.setVisible(false);
new ReJframe();
func.studentAdd();
}
});
JButton delete=new JButton("删除");
delete.setSize(120, 50);
delete.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
thisjframe.setVisible(false);
new ReJframe();
func.studentDelete();;
}
});
JButton revise=new JButton("修改");
revise.setSize(120,50);
revise.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
thisjframe.setVisible(false);
new ReJframe();
func.studentRevise();
}
});
JButton find=new JButton("查找");
find.setSize(120, 50);
find.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
thisjframe.setVisible(false);
new ReJframe();
func.studentFind();;
}
});
JButton look=new JButton("显示");
look.setSize(120, 50);
look.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
thisjframe.setVisible(false);
new ReJframe();
func.studentLook();;
}
});
JButton close=new JButton("退出");
close.setSize(120, 50);
btnPanel.add(add);
btnPanel.add(delete);
btnPanel.add(revise);
btnPanel.add(find);
btnPanel.add(look);
btnPanel.add(close);
JButton but=new JButton("返回");
but.setBounds(270, 270, 100, 50);
but.addActionListener(this);
cont.add(label);
cont.add(btnPanel);
cont.add(but);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
this.setVisible(false);
new Myframe();
}
}
class NameJdialog extends JDialog{
public NameJdialog() {
init();
}
private void init() {
// TODO Auto-generated method stub
this.setBounds(300, 300, 200, 200);
Container cont=this.getContentPane();
JLabel label =new JLabel("账号名/密码均不能为空");
label.setBounds(150, 150, 100, 50);
cont.add(label);
this.setVisible(true);
}
}
class ReJframe extends JFrame implements ActionListener{
public ReJframe() {
init();
}
private void init() {
// TODO Auto-generated method stub
this.setBounds(200,200,300,300);
this.setLayout(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cont =this.getContentPane();
ImageIcon img = new ImageIcon("src/班级管理系统/zhi.jpg");
JLabel imgLabel = new JLabel(img);
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
((JPanel) cont).setOpaque(false);
JLabel label=new JLabel("如果完成此次操作,请点击返回");
label.setBounds(100,10,200,100);
JButton but=new JButton("返回");
but.setBounds(150, 150, 100, 50);
but.addActionListener(this);
cont.add(label);
cont.add(but);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
this.setVisible(false);
new Myframe2("你好");
}
}
package 班级管理系统;
public class Student {
private String id;
private String name;
private String gender;
private int age;
private int number;
public String toString() {
return id+"\t\t"+name+"\t\t"+gender+"\t\t"+age+"\t\t"+number;
}
public Student(String id, String name, String gender, int age, int number) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.number = number;
}
public Student() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
```java
package 班级管理系统;
import java.util.ArrayList;
import java.util.Scanner;
public class Function {
private ArrayList<Student> list = null;
Scanner sc=new Scanner(System.in);
public void studentAdd() { //添加
list = (ArrayList<Student>) StudentFile.findStudentListFromFile();
list.add(addStudent());
StudentFile.saveStudentListToFile(list);
System.out.println("添加完毕!");
}
public void studentLook() { //显示
list = (ArrayList<Student>) StudentFile.findStudentListFromFile();
if (list == null || list.isEmpty()) {
System.out.println("还没有学生信息存在,请先添加!");
return;
}
System.out.println("id"+"\t\t"+"学号"+"\t\t"+"姓名"+"\t\t"+"性别"+"\t\t"+"语文"+"\t\t"+"数学"+"\t\t"+"英语"+"\t\t");
for(int i=0;i<list.size();i++) {
System.out.println(i+"\t\t"+list.get(i));
}
System.out.println("共"+list.size()+"个信息!");
}
public void studentRevise() { //修改
// 先获取文件中保存的学生集合对象
list = (ArrayList<Student>) StudentFile.findStudentListFromFile();
if (list == null || list.isEmpty()) {
System.out.println("还没有学生信息存在,请先添加!");
return;
}
System.out.println("输入要修改的学生ID");
String index=sc.next();
for(int i=0;i<list.size();i++) {
if(list.get(i).getId().equals(index)) {
list.set(i, addStudent());
}
}
StudentFile.saveStudentListToFile(list);
System.out.println("修改完毕!");
}
public void studentDelete() { //删除
list = (ArrayList<Student>) StudentFile.findStudentListFromFile();
if (list == null || list.isEmpty()) {
System.out.println("还没有学生信息存在,请先添加!");
return;
}
System.out.println("输入要删除的学生ID");
String id=sc.next();
// 定义学生对象在集合中的索引位置
int index = -1;
int i = 0;// 集合的下标索引
for (Student stu : list) {
if (stu.getId().equals(id)) {
index = i;// 找到学生
break;
}
i++;// 移动下标位置
}
if (index != -1) {
// 从集合删除学生对象
list.remove(index);
// 保存删除后的学生集合对象到文件
StudentFile.saveStudentListToFile(list);
System.out.println("学生信息删除成功!");
} else {
System.out.println("学生信息删除失败!");
}
}
public void studentFind() { //查找
list = (ArrayList<Student>) StudentFile.findStudentListFromFile();
System.out.println("输入要查找的学生的学号:");
String id=sc.next();
for(int i=0;i<list.size();i++) {
if(list.get(i).getId().equals(id)) {
System.out.println(i+"\t\t"+list.get(i));
}
}
System.out.println("查找成功!");
}
private Student addStudent() { //输入信息
Student s=new Student();
System.out.println("输入学号:");
String id=sc.next();
s.setId(id);
System.out.println("输入姓名:");
String name=sc.next();
s.setName(name);
System.out.println("输入性别:");
String sex=sc.next();
s.setGender(sex);;
System.out.println("输入年龄:");
int age=sc.nextInt();
s.setAge(age);;
System.out.println("输入电话号码:");
int number=sc.nextInt();
s.setNumber(number);
return s;
}
}
package 班级管理系统;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class StudentFile {
private static File file=new File("stu.txt");//指定要操作的文件对象
//向文件中写入学生的集合对象(ArrayList集合)
public static void saveStudentListToFile(ArrayList<Student> lsst){
try {
//创建文件输出流对象
FileOutputStream fout=new FileOutputStream(file);
//创建缓冲区输出流对象
BufferedOutputStream bout=new BufferedOutputStream(fout);//加速
//创建对象输出流
ObjectOutputStream objOut=new ObjectOutputStream(bout);
objOut.writeObject(lsst);//将学生集合对象输出到文件保存
objOut.flush();
objOut.close();
System.out.println("学生集合写出到文件成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static List<Student> findStudentListFromFile(){
//首先判断文件是否存在
try {
//创建文件输入流对象
FileInputStream fin=new FileInputStream(file);
//创建缓冲区输入流对象
BufferedInputStream bin=new BufferedInputStream(fin);//加速
//创建对象输入流
ObjectInputStream objin=new ObjectInputStream(bin);
//将对象输入流的中的对象读给List集合
List<Student> lsst=(List<Student>) objin.readObject();
return lsst;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
```