本次Java期末课程设计题目为个人财务管理系统,在已编辑成型的图形界面上通过添加代码实现用户密码修改、用户登录、账户信息增删改等操作,出于对Java文件和流相关知识考查,本项目使用了文件而非数据库作为数据储存的工具。
源代码链接:https://pan.baidu.com/s/1j4rn1zjXj-cYeSzJKAz71g
提取码:cbpy
一、初始图形界面介绍
1.登录界面
要求对于某一特定的用户,将其密码储存在文件中,以便于对修改之后新密码的查看
2.主界面
主界面功能主要是实现某一时间段内收支信息查询与以收支类型为标准的数据筛选
3.系统管理界面(密码修改界面)
修改密码之后通过对储存密码文件的修改来保存新密码
*4.收支编辑界面(核心)
实现对储存信息的增删改操作
二、各功能模块展示
1.主函数
public class MoneyManager {
public static void main(String[] args) {
LoginFrame lf=new LoginFrame();
lf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2.登录界面
登录界面的图形界面部分不进行详细描述,只是一些标签、文本框和按钮的基本填充操作
核心的代码部分为密码的验证操作:在用户点击确定的前提下如果密码文本框(t_pwd)的中输入的内容为读取的pwd.txt文件中密码,则可进入用户界面
tips:要出现新的提示框可以采用下面的代码(以密码错误的情况为例)
JOptionPane.showMessageDialog(null,“用户名密码出错”, “警告”, JOptionPane.ERROR_MESSAGE);
//登录界面
class LoginFrame extends JFrame implements ActionListener{
private JLabel l_user,l_pwd; //用户名标签,密码标签
private JTextField t_user;//用户名文本框
private JPasswordField t_pwd; //密码文本框
private JButton b_ok,b_cancel; //登录按钮,退出按钮
public LoginFrame(){
super("欢迎使用个人理财账本!");
l_user=new JLabel("用户名:",JLabel.RIGHT);
l_pwd=new JLabel(" 密码:",JLabel.RIGHT);
t_user=new JTextField(31);
t_pwd=new JPasswordField(31);
b_ok=new JButton("登录");
b_cancel=new JButton("退出");
//布局方式FlowLayout,一行排满排下一行
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(l_user);
c.add(t_user);
c.add(l_pwd);
c.add(t_pwd);
c.add(b_ok);
c.add(b_cancel);
//为按钮添加监听事件
b_ok.addActionListener(this);
b_cancel.addActionListener(this);
//界面大小不可调整
this.setResizable(false);
this.setSize(455,150);
//界面显示居中
Dimension screen = this.getToolkit().getScreenSize();
this.setLocation((screen.width-this.getSize().width)/2,(screen.height-this.getSize().height)/2);
this.show();
}
//验证账号密码
public void actionPerformed(ActionEvent e) {
if(b_cancel==e.getSource()){
//添加退出代码 ok
System.exit(0);
//
}else if(b_ok==e.getSource()){
//添加代码,验证身份成功后显示主界面ok
try {
FileReader f=new FileReader("d:/io/pwd.txt");
BufferedReader b1=new BufferedReader(f);
String s=b1.readLine();
boolean flag=false;
if(t_user.getText().trim().equals("123")&&t_pwd.getText().equals(s)) {
new MainFrame(t_user.getText().trim());
flag=true;
}
else {
flag=false;
}
if(flag==false) {
JOptionPane.showMessageDialog(null,"用户名密码出错", "警告", JOptionPane.ERROR_MESSAGE);
}
}catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch(IOException e1) {
e1.printStackTrace();
}catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
3.主界面
//主界面
class MainFrame extends JFrame implements ActionListener{
private JMenuBar mb=new JMenuBar();
private JMenu m_system=new JMenu("系统管理");
private JMenu m_fm=new JMenu("收支管理");
private JMenuItem mI[]={new JMenuItem("密码重置"),new JMenuItem("退出系统")};
private JMenuItem m_FMEdit=new JMenuItem("收支编辑");
private JLabel l_type,l_fromdate,l_todate,l_bal,l_ps;
private JTextField t_fromdate,t_todate;
private JButton b_select1,b_select2;
private JComboBox c_type;
private JPanel p_condition,p_detail;
private String s1[]={"收入","支出"};
private double bal1,bal2;
private JTable table;
private String username;
//主界面构造函数
public MainFrame(String username){
super(username+",欢迎使用个人理财账本!");
this.username=username;
Container c=this.getContentPane();
c.setLayout(new BorderLayout());
c.add(mb,"North");
mb.add(m_system);
mb.add(m_fm);
m_system.add(mI[0]);
m_system.add(mI[1]);
m_fm.add(m_FMEdit);
m_FMEdit.addActionListener(this);
mI[0].addActionListener(this);
mI[1].addActionListener(this);
l_type=new JLabel("收支类型:");
c_type=new JComboBox(s1);
b_select1=new JButton("查询");
l_fromdate=new JLabel("起始时间");
t_fromdate=new JTextField(8);
l_todate=new JLabel("终止时间");
t_todate=new JTextField(8);
b_select2=new JButton("查询");
l_ps = new JLabel("注意:时间格式为YYYYMMDD,例如:20150901");
p_condition=new JPanel();
p_condition.setLayout(new GridLayout(3,1));
p_condition.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("输入查询条件"),
BorderFactory.createEmptyBorder(5,5,5,5)));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.add(l_type);
p1.add(c_type);
p1.add(b_select1);
p2.add(l_fromdate);
p2.add(t_fromdate);
p2.add(l_todate);
p2.add(t_todate);
p2.add(b_select2);
p3.add(l_ps);
p_condition.add(p1);
p_condition.add(p2);
p_condition.add(p3);
c.add(p_condition,"Center");
b_select1.addActionListener(this);
b_select2.addActionListener(this);
p_detail=new JPanel();
p_detail.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("收支明细信息"),
BorderFactory.createEmptyBorder(5,5,5,5)));
l_bal=new JLabel();
String[] cloum = {"编号", "日期", "类型","内容","金额",};
Object[][] row = new Object[50][5];
table = new JTable(row, cloum);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setPreferredSize(new Dimension(580,350));
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane.setViewportView(table);
p_detail.add(l_bal);
p_detail.add(scrollpane);
c.add(p_detail,"South");
//添加代码
//主界面显示
method m=new method();
m.showDetail(row);
//
bal1=0;
double b;
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
a1=m.loadList(a1);
for(int i=0;i<a1.size();i++) {
b=Double.parseDouble(a1.get(i).getAmount());
if(a1.get(i).getType().equals("收入")) {
bal1+=b;
}else {
bal1-=b;
}
}
if(bal1<0)
l_bal.setText("个人总收支余额为"+bal1+"元。您已超支,请适度消费!");
else
l_bal.setText("个人总收支余额为"+bal1+"元。");
this.setResizable(false);
this.setSize(600,580);
Dimension screen = this.getToolkit().getScreenSize();
this.setLocation((screen.width-this.getSize().width)/2,(screen.height-this.getSize().height)/2);
this.show();
}
//主界面构造函数重载
public MainFrame(String username,ArrayList<MyAccount> a1){
super(username+",欢迎使用个人理财账本!");
this.username=username;
Container c=this.getContentPane();
c.setLayout(new BorderLayout());
c.add(mb,"North");
mb.add(m_system);
mb.add(m_fm);
m_system.add(mI[0]);
m_system.add(mI[1]);
m_fm.add(m_FMEdit);
m_FMEdit.addActionListener(this);
mI[0].addActionListener(this);
mI[1].addActionListener(this);
l_type=new JLabel("收支类型:");
c_type=new JComboBox(s1);
b_select1=new JButton("查询");
l_fromdate=new JLabel("起始时间");
t_fromdate=new JTextField(8);
l_todate=new JLabel("终止时间");
t_todate=new JTextField(8);
b_select2=new JButton("查询");
l_ps = new JLabel("注意:时间格式为YYYYMMDD,例如:20150901");
p_condition=new JPanel();
p_condition.setLayout(new GridLayout(3,1));
p_condition.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("输入查询条件"),
BorderFactory.createEmptyBorder(5,5,5,5)));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.add(l_type);
p1.add(c_type);
p1.add(b_select1);
p2.add(l_fromdate);
p2.add(t_fromdate);
p2.add(l_todate);
p2.add(t_todate);
p2.add(b_select2);
p3.add(l_ps);
p_condition.add(p1);
p_condition.add(p2);
p_condition.add(p3);
c.add(p_condition,"Center");
b_select1.addActionListener(this);
b_select2.addActionListener(this);
p_detail=new JPanel();
p_detail.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("收支明细信息"),
BorderFactory.createEmptyBorder(5,5,5,5)));
l_bal=new JLabel();
String[] cloum = {"编号", "日期", "类型","内容","金额",};
Object[][] row = new Object[50][5];
table = new JTable(row, cloum);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setPreferredSize(new Dimension(580,350));
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane.setViewportView(table);
p_detail.add(l_bal);
p_detail.add(scrollpane);
c.add(p_detail,"South");
//添加代码
//主界面显示
method m=new method();
m.showDetail(row, a1);
//
bal1=0;
double b;
a1=m.loadList(a1);
for(int i=0;i<a1.size();i++) {
b=Double.parseDouble(a1.get(i).getAmount());
if(a1.get(i).getType().equals("收入")) {
bal1+=b;
}else {
bal1-=b;
}
}
if(bal1<0)
l_bal.setText("个人总收支余额为"+bal1+"元。您已超支,请适度消费!");
else
l_bal.setText("个人总收支余额为"+bal1+"元。");
this.setResizable(false);
this.setSize(600,580);
Dimension screen = this.getToolkit().getScreenSize();
this.setLocation((screen.width-this.getSize().width)/2,(screen.height-this.getSize().height)/2);
this.show();
}
public void actionPerformed(ActionEvent e) {
Object temp=e.getSource();
if(temp==mI[0]){
//密码重置
new ModifyPwdFrame(username);
}else if(temp==mI[1]){
//添加代码
//退出系统
System.exit(0);
}else if(temp==m_FMEdit){
//收支编辑
new BalEditFrame();
}else if(temp==b_select1){ //根据收支类型查询
//添加代码
//删除原界面
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
//创建方法类,获得数据
method m=new method();
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
a1=m.loadList(a1);
ArrayList<MyAccount> a2=new ArrayList<MyAccount>();//收入
ArrayList<MyAccount> a3=new ArrayList<MyAccount>();//支出
for(int i=0;i<a1.size();i++) {
if(a1.get(i).getType().equals("收入")) {
a2.add(a1.get(i));
}else {
a3.add(a1.get(i));
}
}
if(c_type.getSelectedItem().toString().equals("收入")) {
new MainFrame("123",a2);
}else {
new MainFrame("123",a3);
}
}else if(temp==b_select2){ //根据时间范围查询
//添加代码
//删除原界面
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
//添加方法类
method m=new method();
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
a1=m.loadList(a1);
//按时间获得所需数据,并查询
String from=t_fromdate.getText();
String to=t_todate.getText();
ArrayList<MyAccount> a2=new ArrayList<MyAccount>();
for(int i=0;i<a1.size();i++) {
if(a1.get(i).getDate().compareTo(from)>=0&&a1.get(i).getDate().compareTo(to)<=0) {
a2.add(a1.get(i));
}
}
new MainFrame("123",a2);
}
}
}
主界面由于界面元素较多故代码较为冗长。其核心便是要显示所有的用户信息,并具有一定的分类筛选收支信息的功能。
数据储存:通过新建一个TestMyAccount类来储存相关数据,并在主函数中以集合的方式储存使用,前类的具体操作在文末附上源代码
收支信息查询:通过对集合类Type关键字的判断来实现分类,其中要在点击查询键后实现界面只保留收入or支出的信息则需要一下操作实现界面的刷新
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
其本质为创建一个相关的新界面而删除原有界面
4.修改密码界面
//修改密码界面
class ModifyPwdFrame extends JFrame implements ActionListener{
private JLabel l_oldPWD,l_newPWD,l_newPWDAgain;
private JPasswordField t_oldPWD,t_newPWD,t_newPWDAgain;
private JButton b_ok,b_cancel;
private String username;
public ModifyPwdFrame(String username){
super("修改密码");
this.username=username;
l_oldPWD=new JLabel("旧密码");
l_newPWD=new JLabel("新密码:");
l_newPWDAgain=new JLabel("确认新密码:");
t_oldPWD=new JPasswordField(15);
t_newPWD=new JPasswordField(15);
t_newPWDAgain=new JPasswordField(15);
b_ok=new JButton("确定");
b_cancel=new JButton("取消");
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(l_oldPWD);
c.add(t_oldPWD);
c.add(l_newPWD);
c.add(t_newPWD);
c.add(l_newPWDAgain);
c.add(t_newPWDAgain);
c.add(b_ok);
c.add(b_cancel);
b_ok.addActionListener(this);
b_cancel.addActionListener(this);
this.setResizable(false);
this.setSize(280,160);
Dimension screen = this.getToolkit().getScreenSize();
this.setLocation((screen.width-this.getSize().width)/2,(screen.height-this.getSize().height)/2);
this.show();
}
public void actionPerformed(ActionEvent e) {
if(b_cancel==e.getSource()){
//添加代码
//取消
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
}else if(b_ok==e.getSource()){ //修改密码
//添加代码
//确定
try {
boolean judge=true;
BufferedReader br=new BufferedReader(new FileReader("d:/io/pwd.txt"));
String s= br.readLine();
if(t_newPWD.getText().trim().equals(s)){
JOptionPane.showMessageDialog(null,"新密码与原密码一样", "警告", JOptionPane.ERROR_MESSAGE);
judge=false;
}
if(t_newPWDAgain.getText().trim().contentEquals(t_newPWD.getText())&&judge==true)
{
PrintWriter p=new PrintWriter(new BufferedWriter(new FileWriter("d:/io/pwd.txt")));
p.println(t_newPWD.getPassword());
p.close();
JOptionPane.showMessageDialog(null, "修改密码成功","提醒",JOptionPane.INFORMATION_MESSAGE);
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
System.exit(0);
}else if(judge==true&&t_newPWDAgain.getText().trim().contentEquals(t_newPWD.getText())==false) {
JOptionPane.showMessageDialog(null,"两次新密码不一致!", "警告", JOptionPane.ERROR_MESSAGE);
}
}catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch(IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
修改密码操作类似于判断密码是否正确操作,无非是加上了对原储存密码文件修改的操作
if(t_newPWDAgain.getText().trim().contentEquals(t_newPWD.getText())&&judge==true)
{
PrintWriter p=new PrintWriter(new BufferedWriter(new FileWriter(“d:/io/pwd.txt”)));
p.println(t_newPWD.getPassword());
p.close();
JOptionPane.showMessageDialog(null, “修改密码成功”,“提醒”,JOptionPane.INFORMATION_MESSAGE);
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
System.exit(0);
}
tips:对文件的操作在打开文件后一定要要记得关!!
5.收支编辑界面
//收支编辑界面
class BalEditFrame extends JFrame implements ActionListener{
private JLabel l_id,l_date,l_bal,l_type,l_item;
private JTextField t_id,t_date,t_bal;
private JComboBox c_type,c_item;
private JButton b_update,b_delete,b_select,b_new,b_clear;
private JPanel p1,p2,p3;
private JScrollPane scrollpane;
private JTable table;
//收支界面构造函数
public BalEditFrame(){
super("收支编辑" );
l_id=new JLabel("编号:");
l_date=new JLabel("日期:");
l_bal=new JLabel("金额:");
l_type=new JLabel("类型:");
l_item=new JLabel("内容:");
t_id=new JTextField(8);
t_date=new JTextField(8);
t_bal=new JTextField(8);
String s1[]={"收入","支出"};
String s2[]={"购物","餐饮","居家","交通","娱乐","人情","工资","奖金","其他"};
c_type=new JComboBox(s1);
c_item=new JComboBox(s2);
b_select=new JButton("查询");
b_update=new JButton("修改");
b_delete=new JButton("删除");
b_new=new JButton("录入");
b_clear=new JButton("清空");
Container c=this.getContentPane();
c.setLayout(new BorderLayout());
p1=new JPanel();
p1.setLayout(new GridLayout(5,2,10,10));
p1.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("编辑收支信息"),
BorderFactory.createEmptyBorder(5,5,5,5)));
p1.add(l_id);
p1.add(t_id);
p1.add(l_date);
p1.add(t_date);
p1.add(l_type);
p1.add(c_type);
p1.add(l_item);
p1.add(c_item);
p1.add(l_bal);
p1.add(t_bal);
c.add(p1, BorderLayout.WEST);
p2=new JPanel();
p2.setLayout(new GridLayout(5,1,10,10));
p2.add(b_new);
p2.add(b_update);
p2.add(b_delete);
p2.add(b_select);
p2.add(b_clear);
c.add(p2,BorderLayout.CENTER);
p3=new JPanel();
p3.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("显示收支信息"),
BorderFactory.createEmptyBorder(5,5,5,5)));
String[] cloum = { "编号", "日期", "类型","内容", "金额"};
Object[][] row = new Object[50][5];
//创建方法类
method m=new method();
//显示信息
m.showDetail(row);
//
table = new JTable(row, cloum);
scrollpane = new JScrollPane(table);
scrollpane.setViewportView(table);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
p3.add(scrollpane);
c.add(p3,BorderLayout.EAST);
b_update.addActionListener(this);
b_delete.addActionListener(this);
b_select.addActionListener(this);
b_new.addActionListener(this);
b_clear.addActionListener(this);
//添加代码,为table添加鼠标点击事件监听addMouseListener
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
// 仅当鼠标单击时响应
// 得到选中的行列的索引值
// 得到选中的单元格的值,表格中都是字符串
int a= table.getSelectedRow();
t_id.setText(row[a][0].toString());
t_date.setText(row[a][1].toString());
c_type.setSelectedItem(row[a][2].toString());
c_item.setSelectedItem(row[a][3].toString());
t_bal.setText(row[a][4].toString());
}
}
);
this.setResizable(false);
this.setSize(800,300);
Dimension screen = this.getToolkit().getScreenSize();
this.setLocation((screen.width-this.getSize().width)/2,(screen.height-this.getSize().height)/2);
this.show();
}
//收支界面构造函数重载,用于刷新收支界面
public BalEditFrame(ArrayList<MyAccount> a1){
super("收支编辑" );
l_id=new JLabel("编号:");
l_date=new JLabel("日期:");
l_bal=new JLabel("金额:");
l_type=new JLabel("类型:");
l_item=new JLabel("内容:");
t_id=new JTextField(8);
t_date=new JTextField(8);
t_bal=new JTextField(8);
String s1[]={"收入","支出"};
String s2[]={"购物","餐饮","居家","交通","娱乐","人情","工资","奖金","其他"};
c_type=new JComboBox(s1);
c_item=new JComboBox(s2);
b_select=new JButton("查询");
b_update=new JButton("修改");
b_delete=new JButton("删除");
b_new=new JButton("录入");
b_clear=new JButton("清空");
Container c=this.getContentPane();
c.setLayout(new BorderLayout());
p1=new JPanel();
p1.setLayout(new GridLayout(5,2,10,10));
p1.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("编辑收支信息"),
BorderFactory.createEmptyBorder(5,5,5,5)));
p1.add(l_id);
p1.add(t_id);
p1.add(l_date);
p1.add(t_date);
p1.add(l_type);
p1.add(c_type);
p1.add(l_item);
p1.add(c_item);
p1.add(l_bal);
p1.add(t_bal);
c.add(p1, BorderLayout.WEST);
p2=new JPanel();
p2.setLayout(new GridLayout(5,1,10,10));
p2.add(b_new);
p2.add(b_update);
p2.add(b_delete);
p2.add(b_select);
p2.add(b_clear);
c.add(p2,BorderLayout.CENTER);
p3=new JPanel();
p3.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("显示收支信息"),
BorderFactory.createEmptyBorder(5,5,5,5)));
String[] cloum = { "编号", "日期", "类型","内容", "金额"};
Object[][] row = new Object[50][5];
//创建方法类
method m=new method();
//显示信息
m.showDetail(row,a1);
//
table = new JTable(row, cloum);
scrollpane = new JScrollPane(table);
scrollpane.setViewportView(table);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
p3.add(scrollpane);
c.add(p3,BorderLayout.EAST);
b_update.addActionListener(this);
b_delete.addActionListener(this);
b_select.addActionListener(this);
b_new.addActionListener(this);
b_clear.addActionListener(this);
//添加代码,为table添加鼠标点击事件监听addMouseListener
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
// 仅当鼠标单击时响应
// 得到选中的行列的索引值
// 得到选中的单元格的值,表格中都是字符串
int a= table.getSelectedRow();
t_id.setText(row[a][0].toString());
t_date.setText(row[a][1].toString());
c_type.setSelectedItem(row[a][2].toString());
c_item.setSelectedItem(row[a][3].toString());
t_bal.setText(row[a][4].toString());
t_id.setEditable(false);
}
}
);
this.setResizable(false);
this.setSize(800,300);
Dimension screen = this.getToolkit().getScreenSize();
this.setLocation((screen.width-this.getSize().width)/2,(screen.height-this.getSize().height)/2);
this.show();
}
//增删添改查
public void actionPerformed(ActionEvent e) {
if(b_select==e.getSource()){
//查询所有收支信息 ok
//添加代码
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
new BalEditFrame();
}else if(b_update==e.getSource()){
// 修改某条收支信息
//添加代码
//添加方法类
final int op=0;
if(op==JOptionPane.showConfirmDialog(null,"确定修改?", "提示",JOptionPane.OK_CANCEL_OPTION))
{
method m=new method();
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
a1=m.loadList(a1);
int b=Integer.parseInt(t_id.getText());
a1.remove(b-1);
a1.add(b-1, new MyAccount(t_id.getText(),t_date.getText(),c_type.getSelectedItem().toString(),c_item.getSelectedItem().toString(),t_bal.getText()));
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
m.delete();
m.update(a1);
a1=m.loadList(a1);
new BalEditFrame(a1);
}else {
}
}else if(b_delete==e.getSource()){ //删除某条收支信息
final int op=0;
if(op==JOptionPane.showConfirmDialog(null,"确定修改?", "提示",JOptionPane.OK_CANCEL_OPTION))
{
//写的方法类
method m=new method();
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
//方法读取文件对象,返回一个ArrayList<MyAccount>
a1=m.loadList(a1);
//读取编号项
int b=Integer.parseInt(t_id.getText());
//删除对应项
a1.remove(b-1);
//删除原界面
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
//删除原文件,构建新文件
m.delete();
m.update(a1);
//同步更新编辑界面
a1=m.loadList(a1);
new BalEditFrame(a1);
//添加代码
}else {
}
}else if(b_new==e.getSource()){ //新增某条收支信息
//添加代码
method m=new method();
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
a1=m.loadList(a1);
int flag=1;
for(int i=0;i<a1.size();i++) {
if(t_id.getText().equals(a1.get(i).getId())){
flag=0;
break;
}
}
if(flag==1) {
a1.add(new MyAccount(t_id.getText(),t_date.getText(),c_type.getSelectedItem().toString(),c_item.getSelectedItem().toString(),t_bal.getText()));
}else {
JOptionPane.showMessageDialog(null,"id重复,重新编辑", "警告", JOptionPane.ERROR_MESSAGE);
}
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
m.delete();
m.update(a1);
//同步更新编辑界面
a1=m.loadList(a1);
new BalEditFrame(a1);
}else if(b_clear==e.getSource()){ //清空输入框
//添加代码 ok
method m=new method();
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
a1=m.loadList(a1);
this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING) );
new BalEditFrame(a1);
}
}
}
其中另外实现的功能还有点击某一条信息会在左侧提示栏显示出其具体详细的信息,效果如下:
增删改查的操作在代码中有具体的介绍便不一一罗列。
6.method类
下面展示一些 内联代码片
。
class method{
//对主界面构造函数数组进行赋值
public Object showDetail(Object row[][]) {
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
try {
FileInputStream fis=new FileInputStream("d:/io/xu.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
a1=(ArrayList<MyAccount>)(ois.readObject());
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
for(int i=0;i<a1.size();i++) {
row[i][0]=a1.get(i).getId();
row[i][1]=a1.get(i).getDate();
row[i][2]=a1.get(i).getType();
row[i][3]=a1.get(i).getContent();
row[i][4]=a1.get(i).getAmount();
}
return row;
}
//对主界面构造函数数组赋值
public Object showDetail(Object row[][],ArrayList<MyAccount> al) {
for(int i=0;i<al.size();i++) {
row[i][0]=al.get(i).getId();
row[i][1]=al.get(i).getDate();
row[i][2]=al.get(i).getType();
row[i][3]=al.get(i).getContent();
row[i][4]=al.get(i).getAmount();
}
return row;
}
//加载目标文件信息,并以ArrayList<MyAccount>形式返回
public ArrayList<MyAccount> loadList(ArrayList<MyAccount> a1) {
try {
FileInputStream fis= new FileInputStream("d:/io/xu.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
a1=(ArrayList<MyAccount>)(ois.readObject());
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return a1;
}
//用于删除原文件
public void delete() {
File f=new File("d:/io/xu.txt");
f.delete();
}
//创建新文件并写入信息
public void update(ArrayList<MyAccount> a1){
try {
File f=new File("d:/io/xu.txt");
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(a1);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
补充:TestMyAccount类源代码
package BigZY;
import java.io.*;
import java.util.*;
public class TestMyAccount {
public static void main(String[] args) {
ArrayList<MyAccount> a1=new ArrayList<MyAccount>();
a1.add(new MyAccount("1","2019-12-12","收入","工资","8000"));
a1.add(new MyAccount("2","2019-12-13","支出","购物","1000"));
a1.add(new MyAccount("3","2019-12-14","收入","奖金","5000"));
a1.add(new MyAccount("4","2019-12-15","支出","餐饮","80"));
a1.add(new MyAccount("5","2019-12-15","支出","购物","100"));
try {
FileOutputStream fos=new FileOutputStream("d:/io/xu.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(a1);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyAccount implements Serializable{
private String id;
private String date;
private String type;
private String content;
private String amount;
public MyAccount(String id, String date, String type, String content, String amount) {
super();
this.id = id;
this.date = date;
this.type = type;
this.content = content;
this.amount = amount;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
其操作是之前的操作的基础:对创建的数组进行赋值,文件的删除与新建等操作都包含在其中,故其实应该理解本程序的第一顺序阅读块。
(本程序初始的用户名为“123”,储存文件的初始密码为“11111”),也希望大家能对错误或者可以改进的地方向我提出自己的高见!!
第一次发文,兄弟们不要吝啬你的一键三连十分感谢!!