CurrencyConversion source code:
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
javax.swing.event.*;
class CurrencyConversion extends JFrame implements
ActionListener{
JTabbedPane tbpane;
JPanel panelt1;
JLabel lblus;
JLabel lblkh;
JLabel lblcbkh;
JTextField txtus;
JTextField txtkh;
JComboBox cbratekh;
JButton btuskh;
JPanel panelt2;
JLabel lbleuro;
JLabel lbleurous;
JLabel lblcbus;
JTextField txteurous;
JTextField txteuro;
JComboBox cbrateus;
JButton bteurous;
CurrencyConversion(){
1 setTitle("Currency
Conversion Program");
2 setDefaultCloseOperation(EXIT_ON_CLOSE);
3 setSize(300,250);
4 setResizable(false);
5 Container
cont=getContentPane();
//USD to Riel conversion
interface
6 lblus=new
JLabel("USD:");
7 lblkh=new
JLabel("Riel:");
8 lblcbkh=new
JLabel("Select exchange rate:");
9 btuskh=new
JButton("OK");
10 btuskh.addActionListener(this);
11 txtus=new JTextField(10);
12 txtus.addKeyListener(new
KeyList());
13 txtkh=new JTextField(10);
14 txtkh.addKeyListener(new
KeyList());
15 Object[]
ratekh={3800,3900,4000,4100,4200};
16 cbratekh=new
JComboBox(ratekh);
17 cbratekh.setEditable(true);
18 cbratekh.getEditor().getEditorComponent().addKeyListener(new
KeyList());
19 panelt1=new JPanel();
20 panelt1.add(lblcbkh);
21 panelt1.add(cbratekh);
22 panelt1.add(lblus);
23 panelt1.add(txtus);
24 panelt1.add(lblkh);
25 panelt1.add(txtkh);
26 panelt1.add(btuskh);
27 panelt1.setLayout(new
GridLayout(4,1));
//EURO and USD conversion interface
28 lbleuro=new
JLabel("Euro:");
29 lbleurous=new
JLabel("USD:");
30 lblcbus=new
JLabel("Select exchange rate:");
31 bteurous=new
JButton("OK");
32 bteurous.addActionListener(this);
33 txteuro=new JTextField(10);
34 txteuro.addKeyListener(new
KeyList());
35 txteurous=new
JTextField(10);
36 txteurous.addKeyListener(new
KeyList());
37 Object[]
rateus={1.25,1.28,1.30,1.32,1.35,1.40};
38 cbrateus=new
JComboBox(rateus);
39 cbrateus.setEditable(true);
40 cbrateus.getEditor().getEditorComponent().addKeyListener(new
KeyList());
41 panelt2=new JPanel();
42 panelt2.setLayout(new
GridLayout(4,1));
43 panelt2.add(lblcbus);
44 panelt2.add(cbrateus);
45 panelt2.add(lbleuro);
46 panelt2.add(txteuro);
47 panelt2.add(lbleurous);
48 panelt2.add(txteurous);
49 panelt2.add(bteurous);
//Create JTabbedPane
object and add panelt1 and panelt2
50 JTabbedPane tbpane=new
JTabbedPane();
51 tbpane.add(panelt1, null,
0);
52 tbpane.add(panelt2, null,
1);
//Set title to each tab
53 tbpane.setTitleAt(0,"USD
and Riel");
54 tbpane.setTitleAt(1,"EURO
and USD");
//Add tbpane JTabbedPane
object to the container
55 cont.add(tbpane);
56 setVisible(true);
}
57 public
void actionPerformed(ActionEvent e){
double rate;
double usdamount;
double khamount;
double euroamount;
if(e.getSource()==btuskh){
if(!checkBlank(txtus,txtkh,
cbratekh))
if(txtkh.getText().equals("")){
//USD to Riel conversion
rate=Double.parseDouble(cbratekh.getSelectedItem().toString());
usdamount=Double.parseDouble(txtus.getText());
khamount=usdToKH(rate,usdamount);
txtkh.setText(""+khamount);
}
else
if(txtus.getText().equals("")) { //Riel to USD conversion
rate=Double.parseDouble(cbratekh.getSelectedItem().toString());
khamount=Double.parseDouble(txtkh.getText());
usdamount=khToUSD(rate,khamount);
txtus.setText(""+usdamount);
}
else{
showMessage("Please
leave the result text box blank");
}
}
else
if(e.getSource()==bteurous){
if(!checkBlank(txteurous,txteuro,
cbrateus))
if(txteurous.getText().equals("")){
//Euro to USD conversion
rate=Double.parseDouble(cbrateus.getSelectedItem().toString());
euroamount=Double.parseDouble(txteuro.getText());
usdamount=euroToUSD(rate,euroamount);
txteurous.setText(""+usdamount);
}
else
if(txteuro.getText().equals("")) { // USD to EURO conversion
rate=Double.parseDouble(cbrateus.getSelectedItem().toString());
usdamount=Double.parseDouble(txteurous.getText());
euroamount=usdToEuro(rate,usdamount);
txteuro.setText(""+euroamount);
}
else{
showMessage("Please
leave the result text box blank");
}
}
}
Code Explanation:
1 Set the title of the program window (Currency Conversion Program).
2 Let the program window close when the user clicks the close button.
3 Specify the width and height of the program window when it opens.
4 Disable program window resizing.
5 Create a container object to act as the controls placeholder.
6-27 Construct the interface for USD and Riel conversion. We need three labels, one combobox, two text boxes, and one button. These controls are grouped together in the panelt1 object. The combobox allows the user to select conversion rate. The text boxes are for inputting USD or Riel amount. Inputting in both text boxes in one operation is not valid. If you input USD, the conversion will be made from USD currency to Riel currency and vise versa. The two text boxed are registered to the key event so that restriction can be placed on its key typed. The button object is registered to the action event to enable button-click action.
28-49 The construction of the interface for EURO and USD conversion is done in the same way as the interface for USD and Riel conversion except different names of controls.
50 Create a JTabbedPane object tbpane.
51 Place the panelt1 to the first tab.
52 Place the panelt2 tot the second tab.
53 Set title of the first tab to "USD and Riel " text.
54 Set title of the second tab to "EURO and USD" text.
55 Add the tbpane oject to the container.
56 Make user the program window is visible.
57 The actionPerformed method is rewritten to enable the button-click action. When the user clicks btuskh from the first tab, the conversion is made from USD amount to Riel amount or vise versa. If you want to convert from USD to Riel , input the USA amount and leave the Riel text box blank and vise versa.
If the user clicks the bteurous button from the second tab, the conversion is for EURO to USA currency. It works the same way as the previous button click except for different currencies conversion.
58 The KeyList class extends the KeyAdapter class to rewrite the keyTyped method. This method defines code to restrict key typed in all text boxes. Only number, dot, backspace, and delete keys are accepted.
59 The checkBlank method is implemented to check whether the combobox conversion rate or the two text boxes are not blank before conversion can be made.
60 The usdToKH method is invoked to convert from USD amount to Riel amount.
61 The khToUSD method is invoked to convert from Riel amount to USD amount.
62 The euroToUSD method is invoked to convert from EURO amount to USD amount.
63 The usdToEURO method is invoked to convert from USD amount to EURO amount.
65 The showMessage method is invoked to show a message dialog to inform the user when an invalid action is being made.
66 The CurrencyConversion Program has the main method to start the program.
