import java.applet.Applet;
import java.awt.BorderLayout; import java.awt.Frame; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;import javax.swing.JButton;
import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField;public class ExchangeRate extends Applet implements ActionListener
{ private JFrame frame=new JFrame("转换"); private JTextField text1=new JTextField(); private JTextField text2=new JTextField(); private String s[]={"美元USD ","人民币CNY","日元 JPY", "欧元EUR","韩元KPW","CAD","澳元AUD","英镑GBP","台币TWD","NZD"}; private float rate[]={1,(float)6.8269,(float)89.1821992,(float)0.684134911,(float)1162.7907 , (float)1.06079974 ,(float)1.09793588 ,(float) 0.615763547 ,(float)32.1646832,(float)1.38159713}; private JComboBox cb=null; private JComboBox cb1=null; private double sum;//兑换后的金额JLabel num=new JLabel("兑换金额");
JLabel money=new JLabel ("由 "); JLabel bmoney=new JLabel("兑换至 "); JLabel bnum=new JLabel("兑换后的金额为"); Panel p1; Panel p2; Panel p3; Panel p4; Panel p5; JButton button;public void init()
{ cb=new JComboBox(s); cb1=new JComboBox(s); text1 = new JTextField(15); text2= new JTextField(10) ; p1=new Panel(); p2=new Panel(); p3=new Panel(); p4=new Panel(); p5=new Panel(); p1.add(num,BorderLayout.WEST); p1.add(text1,BorderLayout.EAST); p2.add(money,BorderLayout.WEST); p2.add(cb,BorderLayout.EAST); p3.add(bmoney,BorderLayout.WEST); p3.add(cb1,BorderLayout.EAST); add(p1,BorderLayout.NORTH); add(p2,BorderLayout.CENTER); add(p3,BorderLayout.SOUTH); button=new JButton("兑换"); p4.add(button,BorderLayout.WEST); add(p4); p5.add(bnum,BorderLayout.WEST); p5.add(text2,BorderLayout.EAST); add(p5); //cb.addActionListener(this); //cb1.addActionListener(this); button.addActionListener(this);}
public void actionPerformed(ActionEvent e) {
JButton button1=(JButton) e.getSource();
java.text.DecimalFormat df=new java.text.DecimalFormat("#.000000"); String text=text1.getText(); String sum1; int cbx = cb.getSelectedIndex();//记录下标 int cb1x=cb1.getSelectedIndex(); if(button1==button) { sum=Float.parseFloat(text);if(sum>0)
{sum1=df.format((double)rate[cb1x]/(double)rate[cbx]*sum);
text2.setText(sum1); } else text2.setText("你输入的金额错误!"); }}
public static void main(String[] args) { ExchangeRate er = new ExchangeRate(); Frame f = new Frame(); f.add(er); f.setSize(500, 500); er.init(); f.addWindowListener(new WindowListener(){ public void windowActivated(WindowEvent e) {}public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
e.getWindow().dispose(); }public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
}); f.show(); } }版权声明:本文为博主原创文章,未经博主允许不得转载。