简单的家庭收支管理程序 主要练习按钮响应 文件输出 读取
import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.omg.CORBA.PRIVATE_MEMBER;
import java.util.*;
class Money{
public String name=null;
public String source=null;
public String date=null;
public String amount=null;
};
public class MoneyManage extends JFrame{
private ArrayList<Money> list=new ArrayList<Money>();
private int position=-1;//位置为-1表示尚不存在元素
private JButton addB=new JButton("增加");
private JButton modifyB=new JButton("修改");
private JButton delB=new JButton("删除");
private JButton preB=new JButton("上一条");
private JButton nextB=new JButton("下一条");
private JButton saveB=new JButton("保存");
private JButton openB=new JButton("打开");
private JLabel nameL=new JLabel("姓名");
private JTextField nameT=new JTextField("",4);//内容+长度
private JLabel sourceL=new JLabel("来源");
private JTextField sourceT=new JTextField("",4);
private JLabel dateL=new JLabel("日期-yyyymmdd");
private JTextField dateT=new JTextField("",4);
private JLabel amountL=new JLabel("金额-收入用正数表示 支出用负数表示");
private JTextField amountT=new JTextField("",4);
private JLabel staticL=new JLabel("以下时间段为收支统计: ");
private JLabel fromL=new JLabel("从yyyymmdd");
private JTextField fromT=new JTextField("",4);
private JLabel toL=new JLabel("到yyyymmdd");
private JTextField toT=new JTextField("",4);
private JButton resultB=new JButton("结果是: ");
private JTextField resultT=new JTextField("",3);
public MoneyManage(){
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(addB);
c.add(modifyB);
c.add(delB);
c.add(preB);
c.add(nextB);
c.add(saveB);
c.add(openB);
c.add(nameL);
c.add(nameT);
c.add(sourceL);
c.add(sourceT);
c.add(dateL);
c.add(dateT);
c.add(amountL);
c.add(amountT);
c.add(staticL);
c.add(fromL);
c.add(fromT);
c.add(toL);
c.add(toT);
c.add(resultB);
c.add(resultT);
addB.addActionListener(new addHandler());
modifyB.addActionListener(new modifyHandler());
delB.addActionListener(new delHandler());
preB.addActionListener(new preHandler());
nextB.addActionListener(new nextHandler());
saveB.addActionListener(new saveHandler());
openB.addActionListener(new openHandler());
resultB.addActionListener(new resultHandler());
}
/**
* 通过更新文本框里面的内容来更新界面
* @param i list中的位置
*/
public void refresh(int i){
nameT.setText(list.get(i).name);
sourceT.setText(list.get(i).source);
dateT.setText(list.get(i).date);
amountT.setText(list.get(i).amount);
}
/**
* 将文本框中的内容设置为空
*/
public void setEmptyInfo(){
nameT.setText("");
sourceT.setText("");
dateT.setText("");
amountT.setText("");
}
/**
* @author Lenovo
* 实现添加按钮的功能 将信息添加到list
*
*/
class addHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(nameT.getText().equals("")||sourceT.getText().equals("")||
dateT.getText().equals("")||amountT.getText().equals("")){
JOptionPane.showMessageDialog(null, "任意一项都不能为空");
}else {
System.out.println("add");;
Money money=new Money();
money.name=nameT.getText();
money.source=sourceT.getText();
money.date=dateT.getText();
money.amount=amountT.getText();
list.add(money);
position=list.size()-1;//pos指向队尾
refresh(position);
JOptionPane.showMessageDialog(null, "已添加");
}
}
}
/**
* @author Lenovo
*实现修改按钮的功能 修改当前记录的信息
*/
class modifyHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(list.size()==0){
JOptionPane.showMessageDialog(null, "记录为空");
setEmptyInfo();
}else{
list.get(position).name=nameT.getText();
list.get(position).source=sourceT.getText();
list.get(position).date=dateT.getText();
list.get(position).amount=amountT.getText();
refresh(position);
JOptionPane.showMessageDialog(null, "已添加");
}
}
}
/**
* @author Lenovo
*删除当前元素
*/
class delHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(list.size()==0){
JOptionPane.showMessageDialog(null, "记录为空");
setEmptyInfo();
}else{
if(list.size()==1){
list.remove(position);
setEmptyInfo();
}else{
if(position==list.size()-1){
list.remove(position);
position=list.size()-1;
}else{
list.remove(position);
}
refresh(position);
JOptionPane.showMessageDialog(null, "已删除");
}
}
}
}
/**
* @author Lenovo
* 实现上一条按钮的功能
*/
class preHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(position==-1){
JOptionPane.showMessageDialog(null, "还没有记录");
}else if(position==0){
JOptionPane.showMessageDialog(null, "这已经是第一条记录");
}else{
position--;
refresh(position);
}
}
}
/**
* @author Lenovo
* 实现下一条按钮的功能
*/
class nextHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(position==-1){
JOptionPane.showMessageDialog(null, "还没有记录");
}else if(position==list.size()-1){
JOptionPane.showMessageDialog(null, "这已经是最后一条记录");
}else{
position++;
refresh(position);
}
}
}
/**
* @author Lenovo
* 实现保存按钮的功能
*
*/
class saveHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
File file=new File("File.txt");
BufferedWriter bw=new BufferedWriter(new FileWriter(file));
for(int i=0;i<list.size();i++){
bw.write(list.get(i).name+","+list.get(i).source+","+
list.get(i).date+","+list.get(i).amount+"\r\n");
}
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "收支记录保存成功!");
}catch (Exception ee) {
// TODO: handle exception
ee.printStackTrace();
}
}
}
/**
* @author Lenovo
* 实现打开按钮的操作
*/
class openHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
File file=new File("File.txt");
if(!file.exists()){
JOptionPane.showMessageDialog(null, "文件不存在");
setEmptyInfo();
}else{
BufferedReader br=new BufferedReader(new FileReader(file));
String line=br.readLine();
if(line==null){
JOptionPane.showMessageDialog(null, "记录为空");
setEmptyInfo();
}else{
list.clear();
while(line!=null){
String[] temp=line.split(",");
Money money=new Money();
money.name=temp[0];
money.source=temp[1];
money.date=temp[2];
money.amount=temp[3];
list.add(money);
line=br.readLine();
}
position=0;
refresh(position);
}
}
}catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
/**
* @author Lenovo
* 实现结果是按钮的操作
*/
class resultHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Float result=0.0f;
for(int i=0;i<list.size();i++){
if(Float.parseFloat(list.get(i).date)>=Integer.parseInt(fromT.getText())
&&Float.parseFloat(list.get(i).date)<=Integer.parseInt(toT.getText())){
result+=Float.parseFloat(list.get(i).amount);
}
resultT.setText(String.valueOf(result));
}
}
}
public static void main(String[] args){
MoneyManage mm=new MoneyManage();
mm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mm.setSize(570,180);
mm.setVisible(true);
}
}
效果图: