/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaexchange;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author chun
*/
public class RateExchange extends JFrame implements ItemListener, ActionListener{
public static void main(String[] args) {
RateExchange e=new RateExchange("汇率转换");
}
Choice choice1, choice2;
float sum;
float rate[] = { 1, (float) 6.5431, (float) 84.2800,
(float) 0.6935, (float) 0.9563,
(float) 0.9533, (float) 0.6132, (float) 29.112,
(float) 7.7727 };
String s[] = { "USD ", "CNY", "JPY", "EUR", "CAD",
"AUD", "GBP", "TWD", "HKD" };
JLabel lab1=new JLabel("兑换金额"),
lab2=new JLabel("要兑换的币种"),
lab3=new JLabel("兑换成的币种"),
lab4=new JLabel("兑换后金额");
JTextArea ta1=new JTextArea(""),
ta2=new JTextArea("");
JButton bt;
double db1, db2;
String str1, str2;
private RateExchange(String string) {
super(string);
setBounds(400, 150, 300, 400);
Container ctp = getContentPane();
ctp.setBackground(Color.lightGray);
ctp.setLayout(null);
setSize(400,300);
setLocation(400,300);
setVisible(true);
lab1.setBounds(10, 10, 150, 20);
ta1.setBounds(80, 10, 130, 20);
lab2.setBounds(10, 60, 80, 20);
choice1 = new Choice();
for (int i=0; i< s.length; i++) {
choice1.addItem(s[i]);
}
choice1.setBounds(100, 60, 90, 80);
choice1.addItemListener(this);
lab3.setBounds(10, 110, 90, 20);
choice2 = new Choice();
for (int i = 0; i < s.length; i++) {
choice2.addItem(s[i]);
}
choice2.setBounds(100, 110, 90, 80);
choice2.addItemListener(this);
lab4.setBounds(10, 210, 150, 20);
bt = new JButton("兑换");
bt.setBounds(120, 160, 60, 20);
bt.addActionListener(this);
ta2.setBounds(80, 210, 130, 20);
add(lab1);
add(ta1);
add(lab2);
add(choice1);
add(lab3);
add(choice2);
add(bt);
add(lab4);
add(ta2);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == choice1) { //判断动作事件是否由choice1发生
int cbx1 = choice1.getSelectedIndex();//获取当前选择的下标
}
if (e.getSource()==choice2) {//判断动作事件是否由choice1发生
int cbx2=choice2.getSelectedIndex();
}
}
public void actionPerformed(ActionEvent e) {
String sum1;
String text = ta1.getText();
sum=Float.parseFloat(text);//将取得的字符串转换为浮点数
int cbx1=choice1.getSelectedIndex();
int cbx2=choice2.getSelectedIndex();
java.text.DecimalFormat df = new java.text.DecimalFormat("0.000000");//格式化十进制数字输出
sum1=df.format((double) rate[cbx2]/(double) rate[cbx1] * sum);
ta2.setText(sum1);//把转换后的值给ta2
}
}