用JAVA做个人民币大小写转换器
编写一个简易人民币大小写转换器的GUI界面程序,要求用户输入一个人民币的数据,要求转换器可以获得数据,并且可能以大写的方式显示出来...
编写一个简易人民币大小写转换器的GUI界面程序,要求用户输入一个人民币的数据,要求转换器可以获得数据,并且可能以大写的方式显示出来
展开
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JPanel{
private JTextField inTextField=new JTextField(20);
private JTextField outTextField=new JTextField(50);
private Timer timer=new Timer(100,new TimerListener());
private Changer changer=new Changer();//转换器//
public GUI(){
JPanel p1=new JPanel();
p1.add(new JLabel("小写"));
p1.add(inTextField);
p1.add(new JLabel("大写"));
p1.add(outTextField);
setLayout(new BorderLayout());
add(p1,BorderLayout.CENTER);
outTextField.setEditable(false);
inTextField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
timer.stop();
changer.setIn(inTextField.getText());
outTextField.setText(changer.change());
timer.start();
}
});
timer.start();
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(!inTextField.getText().equals("")){
changer.setIn(inTextField.getText());
outTextField.setText(changer.change());
}
}
}
public static void main(String[] args){
JFrame frame=new JFrame();
GUI gui=new GUI();
frame.getContentPane().add(gui);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
static class Changer{
String in;
String out="";
public Changer(){
}
public Changer(String in){
this.in=in;
}
public void setIn(String in){
this.in=in;
}
public String change(){
int lengthOfP1=(in.indexOf(".")!=-1) ? in.indexOf(".") : in.length();
//整数部分//;
int lengthOfP2=(in.indexOf(".")!=-1) ? in.length()-lengthOfP1-1 : in.length()-lengthOfP1 ;
//小数部分//;
if(lengthOfP2>2){
in=in.substring(0,in.indexOf(".")+3);
}
//取两位小数//
int p1=0;
int p2=0;
if(in.indexOf(".")!=-1&&in.indexOf(".")!=in.length()-1){
p1=Integer.parseInt(in.substring(0,in.indexOf(".")));
p2=Integer.parseInt(in.substring(in.indexOf(".")+1,in.length()));
//取两位小数//
}
else if(in.indexOf(".")==in.length()-1){
p1=Integer.parseInt(in.substring(0,in.indexOf(".")));
}
else{
p1=Integer.parseInt(in);
}
if(p2<10&&lengthOfP2==1){
p2=p2*10;
}
//取两位数//
if(lengthOfP1>=10){
return "Too much money.";
}
for(int i=lengthOfP1;i>0;i--){
int temp=(p1/((int)Math.pow(10,i-1)));
out+=value3(temp)+value1(i);
p1=p1%(int)Math.pow(10,i-1);
}
for(int i=2;i>0;i--){
int temp=(p2/((int)Math.pow(10,i-1)));
out+=value3(temp)+value2(i);
p2=p2%(int)Math.pow(10,i-1);
}
String output=out;
out="";
return output;
}
public static String value1(int num){
switch(num){
case 1:
return "元";
case 2:
return "十";
case 3:
return "百";
case 4:
return "千";
case 5:
return "万";
case 6:
return "十万";
case 7:
return "百万";
case 8:
return "千万";
case 9:
return "亿";
default:
return null;
}
}//转为中文符号//
public static String value2(int num){
switch(num){
case 2:
return "角";
case 1:
return "分";
default:
return null;
}
}//转为中文符号//
public static String value3(int num){
String s="零壹贰叁肆伍陆柒捌玖";
return s.charAt(num)+"";
}//转大写//
}
}
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JPanel{
private JTextField inTextField=new JTextField(20);
private JTextField outTextField=new JTextField(50);
private Timer timer=new Timer(100,new TimerListener());
private Changer changer=new Changer();//转换器//
public GUI(){
JPanel p1=new JPanel();
p1.add(new JLabel("小写"));
p1.add(inTextField);
p1.add(new JLabel("大写"));
p1.add(outTextField);
setLayout(new BorderLayout());
add(p1,BorderLayout.CENTER);
outTextField.setEditable(false);
inTextField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
timer.stop();
changer.setIn(inTextField.getText());
outTextField.setText(changer.change());
timer.start();
}
});
timer.start();
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(!inTextField.getText().equals("")){
changer.setIn(inTextField.getText());
outTextField.setText(changer.change());
}
}
}
public static void main(String[] args){
JFrame frame=new JFrame();
GUI gui=new GUI();
frame.getContentPane().add(gui);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
static class Changer{
String in;
String out="";
public Changer(){
}
public Changer(String in){
this.in=in;
}
public void setIn(String in){
this.in=in;
}
public String change(){
int lengthOfP1=(in.indexOf(".")!=-1) ? in.indexOf(".") : in.length();
//整数部分//;
int lengthOfP2=(in.indexOf(".")!=-1) ? in.length()-lengthOfP1-1 : in.length()-lengthOfP1 ;
//小数部分//;
if(lengthOfP2>2){
in=in.substring(0,in.indexOf(".")+3);
}
//取两位小数//
int p1=0;
int p2=0;
if(in.indexOf(".")!=-1&&in.indexOf(".")!=in.length()-1){
p1=Integer.parseInt(in.substring(0,in.indexOf(".")));
p2=Integer.parseInt(in.substring(in.indexOf(".")+1,in.length()));
//取两位小数//
}
else if(in.indexOf(".")==in.length()-1){
p1=Integer.parseInt(in.substring(0,in.indexOf(".")));
}
else{
p1=Integer.parseInt(in);
}
if(p2<10&&lengthOfP2==1){
p2=p2*10;
}
//取两位数//
if(lengthOfP1>=10){
return "Too much money.";
}
for(int i=lengthOfP1;i>0;i--){
int temp=(p1/((int)Math.pow(10,i-1)));
out+=value3(temp)+value1(i);
p1=p1%(int)Math.pow(10,i-1);
}
for(int i=2;i>0;i--){
int temp=(p2/((int)Math.pow(10,i-1)));
out+=value3(temp)+value2(i);
p2=p2%(int)Math.pow(10,i-1);
}
String output=out;
out="";
return output;
}
public static String value1(int num){
switch(num){
case 1:
return "元";
case 2:
return "十";
case 3:
return "百";
case 4:
return "千";
case 5:
return "万";
case 6:
return "十万";
case 7:
return "百万";
case 8:
return "千万";
case 9:
return "亿";
default:
return null;
}
}//转为中文符号//
public static String value2(int num){
switch(num){
case 2:
return "角";
case 1:
return "分";
default:
return null;
}
}//转为中文符号//
public static String value3(int num){
String s="零壹贰叁肆伍陆柒捌玖";
return s.charAt(num)+"";
}//转大写//
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询