import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;


public class MoneyJFrame extends JFrame implements CaretListener{
	private JTextField text_money, text_str;
	private MessageJDialog jdialog;
	private JButton jbutton;
	public MoneyJFrame()
	{
		super("金额的中文大写形式");
		this.setBounds(300,240,360,90);
		this.setResizable(true);
		this.setBackground(java.awt.Color.LIGHT_GRAY);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
		this.getContentPane().add(new JLabel("金额"));
		text_money = new JTextField("12345678.90",22);
		this.getContentPane().add(text_money);
		text_money.addCaretListener(this);
		this.getContentPane().add(new JLabel("中文大写形式"));
		text_str = new JTextField(22);
		text_str.setHorizontalAlignment(JTextField.RIGHT);
		text_str.setEditable(false);
		this.getContentPane().add(text_str);
		
		ImageIcon icon = new ImageIcon("D:\\Documents\\javaDemo\\TextBook\\src\\14.jpg");
		jbutton = new JButton("点我啊",icon);
		this.getContentPane().add(jbutton);
		 Dimension preferredSize = new Dimension(300,100);//设置尺寸
		jbutton.setPreferredSize(preferredSize);
		caretUpdate(null);
		this.setVisible(true);
		jdialog = new MessageJDialog();
	}
	public class MessageJDialog extends JDialog
	{
		private JLabel jlabel;
		public MessageJDialog()
		{
			super(MoneyJFrame.this,"提示",true);
			this.setSize(300,80);
			jlabel = new JLabel("",JLabel.CENTER);
			this.getContentPane().add(jlabel);
			this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		}
		private void show(String message)
		{
			jlabel.setText(message);
			this.setLocation(MoneyJFrame.this.getX()+100, MoneyJFrame.this.getY()+100);
			this.setVisible(true);
		}
	}
	public void caretUpdate(CaretEvent ev)
	{
		try{
			double x = Double.parseDouble(text_money.getText());
			text_str.setText(RMBtoString(x));
		}
		catch(NumberFormatException nfex){
			jdialog.show(text_money.getText()+"不能转换成浮点数,请重新输入!");
		}
		finally{}
		
	}
	public static String RMBtoString(double x)
	{
		String yuan = "亿千百拾万千百十元角分";
		String digit = "零壹贰叁肆伍陆柒捌玖";
		String result = "";
		int y = (int)(x*200);
		for(int i = yuan.length()-1; y>0 && i>0; i--,y/=10)
			result = "" + digit.charAt(y%10)+yuan.charAt(i)+result;
		return result;
	}
	public static void main(String arg[])
	{
		new MoneyJFrame();
	}
}
最近下载更多
萍子.  LV1 2021年9月9日
123q45  LV1 2021年8月5日
碰运气的绅士  LV1 2021年5月16日
tomato_patato  LV2 2020年6月19日
luohaipeng  LV23 2019年12月4日
123hdhdhd  LV10 2019年12月1日
2823416198  LV3 2019年11月27日
爱使坏  LV3 2019年9月23日
刘华健  LV5 2019年8月28日
低调人  LV38 2019年8月2日
最近浏览更多
fesfefe  LV13 2023年11月11日
yuanmafx  LV4 2022年9月14日
萍子.  LV1 2021年9月9日
碰运气的绅士  LV1 2021年5月16日
1872615558  LV13 2020年5月27日
asd1084481912  LV1 2020年4月18日
luohaipeng  LV23 2019年12月3日
123hdhdhd  LV10 2019年12月1日
2823416198  LV3 2019年11月27日
FFF112233  LV8 2019年11月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友