首先一个MoneyFrame类
然后是一个WinClose类,该类实现窗口的“×”功能,叉掉窗口
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) {}
}