import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class MoneyJFrame extends JFrame implements CaretListener {
private static final long serialVersionUID = 1L;
private JTextField text_Money,text_Str;
private MessageJDialog jdialog;
public MoneyJFrame(){
super("金额的中文大写形式");
this.setBounds(300, 240, 450, 100);
this.setResizable(false);
this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
this.setBackground(Color.LIGHT_GRAY);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(new JLabel("金额"));
text_Money = new JTextField("12345.67",30);
this.getContentPane().add(text_Money);
this.getContentPane().add(new JLabel("中文大学形式"));
text_Str = new JTextField(30);
this.getContentPane().add(text_Str);
text_Str.setEditable(false);
text_Money.addCaretListener(this);
this.caretUpdate(null);
jdialog = new MessageJDialog(this);
setVisible(true);
}
public static void main(String[] args) {
new MoneyJFrame();
}
@Override
public void caretUpdate(CaretEvent e) {
try {
double x = Double.parseDouble(text_Money.getText());
text_Str.setText(RNBtoStr(x));
} catch (Exception e2) {
//JOptionPane.showConfirmDialog(this, text_Money.getText()+"无法转换成浮点数!");
//JOptionPane.showMessageDialog(this, text_Money.getText()+"无法转换成浮点数!");
jdialog.show( text_Money.getText()+"无法转换成浮点数!" );
}
}
private String RNBtoStr(double x) {
String yuan="亿仟佰拾万仟佰拾圆角分";
String digit="零壹贰叁肆伍陆柒捌玖";
String result="";
int y = (int) (x*100);
int i = yuan.length()-1;
while(y>0 && i>0){
result = ""+digit.charAt(y%10)+yuan.charAt(i)+result;
i--;
y = y/10;
}
return result;
}
}
</pre><p></p><pre name="code" class="java">import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MessageJDialog extends JDialog{
private static final long serialVersionUID = 1L;
private JLabel jlable;
private JFrame jframe;
public MessageJDialog(JFrame jframe){
super(jframe,"提示",true);
this.jframe = jframe;
this.setSize(300, 80);
jlable = new JLabel("",JLabel.CENTER);
this.getContentPane().add(jlable);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
}
public void show(String msg){
jlable.setText(msg);
setLocation(jframe.getX()+100, jframe.getY()+100);
setVisible(true);
}
}