小程序---金额转换

这是课本里面的一个例题,我进行了优化写出了这个较为满意的程序,
有不好的地方希望多多指教。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class MoneyJFrame extends JFrame implements CaretListener {
    private JTextField tfdNum,tfdChinese;
    private MesDialog mdg ;
    public MoneyJFrame() {
        super("金额转换");
        setBounds(200, 100, 450, 130);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container c = getContentPane();//获得内容窗格
        c.setLayout(new FlowLayout(FlowLayout.RIGHT));

        c.add(new JLabel("金额:"));
        tfdNum = new JTextField("123.45",30);
        c.add(tfdNum);

        c.add(new JLabel("中文大写:"));
        tfdChinese = new JTextField("",30);
        tfdChinese.setEditable(false);
        c.add(tfdChinese);

        //添加插入符号监听器
        tfdNum.addCaretListener(this);
        //创建一个错误提示窗口,但不显示
        mdg = new MesDialog(this);
        //无事件源的触发一下监听器,目的是为了把默认的金额转换成大写
        caretUpdate(null);

        setVisible(true);   
    }

    public static void main(String[] args) {
        new MoneyJFrame();
    }

    @Override
    public void caretUpdate(CaretEvent e) {
        String str = tfdNum.getText();
        str.trim();
        if (str == null|| str.length()==0) {
            tfdChinese.setText("");
            return ;
        }
        try {
            double num = Double.parseDouble(str);
            if (Math.log10(num)>12) {
                String mes = str+" 已超过检索范围";
                mdg.show(mes);
                return;
            }
            String Chinese = toChinese(num);
            tfdChinese.setText(Chinese);
        } catch (NumberFormatException e1) {
            String mes = "错误的输入:"+str;
            mdg.show(mes);
        }
    }

    private String toChinese(double num) {
        String unit = "佰拾亿仟佰拾万仟佰拾圆角分";
        String money = "零壹贰叁肆伍陆柒捌玖";
        String res = "";
        long x = (long)((num+0.005)*100);
        int index = unit.length()-1;
        while (x>0 && index>-1) {
            res = ""+money.charAt((int)(x%10))+unit.charAt(index--)+res;
            x /= 10;
        }
        return res;
    }
}
//输入错误的提示窗口
class MesDialog extends JDialog{
    private JFrame jFrame;
    private JLabel lb;
    public MesDialog(JFrame jFrame) {
        this.jFrame = jFrame;
        setTitle("错误提示");
        setSize(300, 120);
        Container c = getContentPane();
        c.setBackground(Color.WHITE);
        lb = new JLabel();
        c.add(lb);

        JButton btnOk = new JButton("确定");
        btnOk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        JPanel p = new JPanel();
        p.setBackground(Color.WHITE);
        p.add(btnOk);
        c.add(p,BorderLayout.SOUTH);
    }
    public void show(String mes) {
        lb.setText(mes);
        setLocation(jFrame.getX()+90, jFrame.getY()+70);
        setVisible(true);
    }
}

你可能感兴趣的:(2.Java,------②小程序,Java)