窗口化金额转换工具

  话说Java自带的图形设备接口真的好丑啊。

 

  

package com.slatop;

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;

class ContWin {
	private Frame frame = null;
	private TextField textField =null;
	private Button button = null;
	private ErrMessage errMag = null;
	
	public Frame getFrame() {
		return frame;
	}
	public TextField getTextField() {
		return textField;
	}
	public Button getButton() {
		return button;
	}
	public ErrMessage getErrMag() {
		return errMag;
	}
	ContWin() {
		//创建图形组件
		createFrame("金额转换");
		createEdit(60);
		createButton("转换");
		createDlg();
		//添加组件到主窗口
		frame.add(textField);
		frame.add(button);
		//显示主窗口
		frame.setVisible(true);
	}
	//创建主窗体
	void createFrame(String title) {
		//窗体对象
		frame = new Frame(title);	
		//窗体大小
		frame.setSize(500, 100);
		//窗体起始坐标,相对于屏幕
		frame.setLocation(500, 500);
		//窗体内布局模式
		frame.setLayout(new FlowLayout());
	}
	//创建文件框
	void createEdit(int wide) {
		textField = new TextField(wide);
	}
	//创建按钮
	void createButton(String text) {
		button = new Button(text);
	}
	//创建对话框
	void createDlg() {
		errMag = new ErrMessage("输入的必须是数字且数只能在1到10000000000000000数!", frame);
	}
}


 

package com.slatop;

class Convert {
	//大写汉字
	private static char[] chinNum = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
	//位汉字
	private static char[] bit = {' ','拾','佰','仟'};
	//段汉字
	private static char[] parg = {' ','万','亿','兆'};
	//缓冲
	private StringBuilder buf = null;
	//记录每一段尾数是否为0
	private boolean zeroFlag = false;
	//当尾数为0时记录连续0的次数
	private int zeroCount = 0;
	//取每次循环的位数
	private int ds = 0;
	//取每次循环的段数
	private int ws = 0;
	//获取字符对应的数字
	private int num = 0;

	String numToChinese(String srcBui) {		
		buf = new StringBuilder(srcBui);
		buf.reverse();
		srcBui = buf.toString();
		buf.delete(0, buf.length());
		//循环
		for (int i = 0; i < srcBui.length(); i++) {
			//获取字符对应的数字
			num = srcBui.charAt(i) - '0';
			//取每次循环的位数
			ds = i/4;
			//取每次循环的段数
			ws = i%4;
			//当本段尾数为0且连续为0时继续判断下一个是否为0
			if(zeroFlag) { 
				if(num == 0) {
					zeroCount ++ ;
					//如果0的次数连续出现4次,清空0记录
					if(zeroCount == 4) {
						//如果此时字符串为空,那么代表着之前所有段也全部为0所以零也不写入
						buf.append(chinNum[0]);
						zeroFlag = false;
						zeroCount = 0;
					}
					continue;
				}
				else {
					//当0不是从段尾连续出现时写入一个段汉字继续向下执行
					buf.append(chinNum[0]);
					buf.append(parg[ds]);				
				}
			}
			//进入一个新段
			if(ws == 0) {
				//从段尾查如果是0记录下来
				if(num == 0) {
					zeroCount ++;
					zeroFlag = true;
					continue;
				}
				buf.append(parg[ds]);
			}
			//加入位汉字
			if(num != 0) {
				buf.append(bit[ws]);
			}
			//加入大写数字
			buf.append(chinNum[num]);
			//清空0记录
			zeroFlag = false;
			zeroCount = 0;
			
		}
		
		String str = buf.reverse().toString();

		//清除掉用于补位的空格
		str = str.replaceAll(" ", "");
		//将多个零替换成一个零
		str = str.replaceAll("零+", "零");
		//去掉最后的零
		str = str.replaceAll("(.+)[零]","$1");
		return str  + "圆整";
	}
	
	boolean chekcNum (String srcBui) {
		//检测数字格式
		if (srcBui == null || !srcBui.matches("\\d+")) {
			return false;
		}

		if (srcBui.compareTo("0") == 0) return false;
		//检测数字范围
		if (srcBui.length() > 16) {
			return false;
		}
		
		return true;
	}
}


 

package com.slatop;

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.Buffer;
class ErrMessage {
	private String errInfo = null;
	private Dialog dlg = null;
	private Button but = null;

	public ErrMessage(String errInfo, Frame frame) {
		this.errInfo = errInfo;
		
		dlg = new Dialog(frame, "错误!", true);
		dlg.setSize(400,100);
		dlg.setLocation(550, 500);
		dlg.setLayout(new FlowLayout());
		
		createLable();
		createBut();
		closeDlg();
		
		dlg.setVisible(true);
		

	}
	public Dialog getDlg() {
		return dlg;
	}
	private void createBut() {
		but = new Button("确定");
		dlg.add(but);
	}
	private void createLable() {
		dlg.add(new Label(errInfo));
	}
	
	private void closeDlg() {
		but.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dlg.setVisible(false);
				
			}
		});
	}
}


 

 package com.slatop;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class WinEvent {
	private ContWin cw = null;

	public WinEvent(ContWin cw) {
		this.cw = cw;
		
		closeWin();
		butCtion();
	}
	
	void closeWin() {
		cw.getFrame().addWindowListener(new WindowAdapter() {
			@Override
			//复写关闭事件(事件也就是windows中的消息机制)
			public void windowClosing(WindowEvent e) {
				//终止当前jvm运行
				System.exit(0);
			}
		});
	}
	
	void butCtion () {
		cw.getButton().addActionListener(new ActionListener() {		
			@Override
			public void actionPerformed(ActionEvent e) {
				String num = cw.getTextField().getText();
				
				//去除前端的零
				num  = num .replaceAll("0*(\\d+)", "$1");				
				Convert con = new Convert();
				if(con.chekcNum(num))
					num = con.numToChinese(num);
				else {
					cw.getErrMag().getDlg().setVisible(true);
					return;
				}			
				cw.getTextField().setText(num);
			}
		});
	}
}


 

package com.slatop;

public class MainClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ContWin cw = new ContWin();
		WinEvent we = new WinEvent(cw);
	}

}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值