Java实现窗口框架,转换金额的大小写

首先一个MoneyFrame类
package L5frame;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class MoneyFrame extends Frame implements ActionListener {

	private TextField text_money,text_str;
	private Button button;
	public MoneyFrame() {
		super("金额的中文大写形式");
		this.setBounds(300, 240, 360, 90);
		this.setBackground(Color.lightGray);
		this.setLayout(new GridLayout(2,3,2,2));
		
		this.add(new Label("金额",Label.RIGHT));
		text_money = new TextField("12345678.90",22);
		this.add(text_money);
		//直接enter触发事件监听,输出结果
		text_money.addActionListener(this);
/*		//设置按钮组件
		button = new Button("转换");
		this.add(button);
		button.addActionListener(this);
*/	
		this.add(new Label("中文大写形式",Label.RIGHT));
		text_str = new TextField(22);
		this.add(text_str);
		
		this.addWindowListener(new WinClose());
		this.setVisible(true);
	}
	public void actionPerformed(ActionEvent ev){
		try {
			double x = Double.parseDouble(text_money.getText());
			text_str.setText(RMBtoString(x));
		}
		catch(NumberFormatException nfex) {
			System.out.print("不能转换成浮点数,请重新输入!");
			//jdialog.show(" \" "+text_money.getText()+" \" 不能转换成浮点数,请重新输入!");
		}
		finally{}
	}
	//将x代表的金额转换成大写形式 
	public static String RMBtoString(double x){
		String yuan = "亿千百拾万千百拾元角分";
		String digital = "零壹贰叁肆伍陆柒捌玖";
		String result = "";
		int y = (int)(x*100);
		for(int i=yuan.length()-1;y>0 && i>0;i--,y/=10){
			result = ""+digital.charAt(y%10)+yuan.charAt(i)+result;
		}
		return result;
	}
	
	public static void main(String[] args) {
		new MoneyFrame();
	}
}

然后是一个WinClose类,该类实现窗口的“×”功能,叉掉窗口
package L5frame;

import java.awt.event.*;

public class WinClose implements WindowListener {

	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值