⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addaccount.java

📁 J2ME编的手机助手 文件下载解压缩以后,是一个NetBeans的工程文件,如果有NB的朋友,可以直接打开编辑 源文件在src目录下面,可执行文件在dist目录下 功能如下 1
💻 JAVA
字号:
/*
 * AddAccount.java
 *
 * Created on 2007年3月11日, 下午2:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.bccn.account.ui;

import java.util.Date;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import net.bccn.account.model.Account;
import net.bccn.account.util.Util;

/**
 *
 * @author hadeslee
 */
public class AddAccount extends Form implements ItemStateListener,CommandListener{
    private static AddAccount af;
    private ChoiceGroup type,detail;//类型和细节
    private TextField money,remark;//金额,备注
    private DateField date;//日期
    private Command ok,back,reset;//OK,返回,重置的按钮
    String[] output=new String[]{"日常用品","化妆品","交通","服装",
    "娱乐","书籍报纸","其它"};
    String[] input=new String[]{"工资","奖金","外快","其它"};
    private boolean isModify;//是否是在修改
    private Account temp;//临时的变量
    private static Image[] outputImage=new Image[7],inputImage=new Image[4];
    /**
     * Creates a new instance of AddAccount
     */
    private AddAccount() {
        super("添加账目");
        initWindow();
    }
    private void initWindow(){
        type=new ChoiceGroup("账目类型",Choice.EXCLUSIVE,
                new String[]{"支出","收入"},null);
        type.setSelectedIndex(0,true);
        Image[] outs=ViewAccountForm.getInstance().getOutput();
        Image[] ins=ViewAccountForm.getInstance().getInput();
        for(int i=0;i<outputImage.length;i++){
            outputImage[i]=outs[i+1];
        }
        for(int i=0;i<inputImage.length;i++){
            inputImage[i]=ins[i+1];
        }
        detail=new ChoiceGroup("详细",Choice.POPUP,
                new String[]{"日常用品","化妆品","交通","服装",
                "娱乐","书籍报纸","其它"},outputImage);
        money=new TextField("金额",null,8,TextField.DECIMAL);
        remark=new TextField("备注",null,20,TextField.ANY);
        date=new DateField("日期",DateField.DATE);
        date.setDate(new Date());
        ok=new Command("确定",Command.OK,1);
        back=new Command("返回",Command.BACK,0);
        reset=new Command("重置",Command.CANCEL,2);
        this.append(type);
        this.append(detail);
        this.append(money);
        this.append(date);
        this.append(remark);
        this.addCommand(ok);
        this.addCommand(back);
        this.addCommand(reset);
        this.setCommandListener(this);
        this.setItemStateListener(this);
    }
    public static AddAccount getInstance(){
        if(af==null){
            af=new AddAccount();
        }
        af.setTitle("添加记录");
        af.isModify=false;
        af.addCommand(af.ok);
        af.addCommand(af.reset);
        af.reset();
        return af;
    }
    //重载一个方法,第一个参数是想传进来的账目,第二个参数表示修改还是查看
    public static  AddAccount getInstance(Account modi,boolean b){
        if(af==null){
            af=new AddAccount();
        }
        af.isModify=b;
        af.temp=modi;
        if(b){
            af.setTitle("修改账目");
        }else{
            af.setTitle("查看账目");
        }
        af.type.setSelectedIndex((modi.getType().equals("支出")?0:1),true);
        if(af.type.isSelected(0)){//此时表示支出
            af.detail.deleteAll();
            for(int i=0;i<af.output.length;i++){
                af.detail.insert(i,af.output[i],outputImage[i]);
                if(af.output[i].equals(modi.getDetail())){
                    af.detail.setSelectedIndex(i,true);
                }
            }
        }else{
            af.detail.deleteAll();
            for(int i=0;i<af.input.length;i++){
                af.detail.insert(i,af.input[i],inputImage[i]);
                if(af.input[i].equals(modi.getDetail())){
                    af.detail.setSelectedIndex(i,true);
                }
            }
        }
        af.money.setString(Float.toString(modi.getMoney()));
        af.date.setDate(modi.getDate());
        af.remark.setString(modi.getRemark());
        if(b){
            af.addCommand(af.ok);
            af.addCommand(af.reset);
        }else{
            af.removeCommand(af.ok);
            af.removeCommand(af.reset);
        }
        return af;
    }
    //根据当前的输入得出一个Account对象
    private Account getAccount(){
        Account ac=new Account();
        ac.setType(type.getString(type.getSelectedIndex()));
        ac.setDetail(detail.getString(detail.getSelectedIndex()));
        ac.setMoney(Float.parseFloat(money.getString()));
        ac.setDate(date.getDate());
        if(remark.getString()!=null){
            ac.setRemark(remark.getString());
        }
        if(isModify){
            ac.setID(temp.getID());
        }
        return ac;
    }
    public void commandAction(Command command, Displayable displayable) {
        if(command==back){
            if(this.getTitle().equals("添加账目"))
                Util.changeTo(Util.MAIN_FORM);
            else
                Util.changeTo(Util.VIEW_ACCOUNT);
        }else if(command==ok){
            //如果金额里面没有输入金额,则不往下执行
            if(money.getString()==null||money.getString().equals("")||
                    Float.parseFloat(money.getString())<=0){
                Util.showINFO(AlertType.ERROR,"请输入金额!!\n金额数值不能小于等于0!!");
                return;
            }
            if(isModify){
                Account aw=getAccount();
                boolean b=Util.updateRecord(aw);
                if(b){
                    Util.changeTo(Util.VIEW_ACCOUNT_FORM);
                    Util.showINFO(AlertType.INFO,"记录修改成功!!请返回!!",ViewAccount.getInstance());
                    ViewAccount.updateList(aw);
                }else{
                    Util.showINFO(AlertType.ERROR,"记录修改失败!!");
                }
            }else{
                boolean b=Util.saveRecord(this.getAccount());
                if(b){
                    reset();
                    Util.showINFO(AlertType.INFO,"记录添加成功!!");
                }else{
                    Util.showINFO(AlertType.ERROR,"记录添加失败!!");
                }
            }
        }else if(command==reset){
            reset();
        }
    }
    //重置
    private void reset(){
        this.setTitle("添加账目");
        type.setSelectedIndex(0,true);
        detail.deleteAll();
        for(int i=0;i<output.length;i++){
            detail.insert(i,output[i],outputImage[i]);
        }
        detail.setSelectedIndex(0,true);
        money.setString(null);
        date.setDate(new Date());
        remark.setString(null);
    }
    public void itemStateChanged(Item item){
        if(item==type){
            int index=type.getSelectedIndex();
            if(index==0){
                detail.deleteAll();
                for(int i=0;i<output.length;i++){
                    detail.insert(i,output[i],outputImage[i]);
                }
            }else{
                detail.deleteAll();
                for(int i=0;i<input.length;i++){
                    detail.insert(i,input[i],inputImage[i]);
                }
            }
            
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -