Create a Java Swing class called CurrencyConverter Create a north panel, center
ID: 3843857 • Letter: C
Question
Create a Java Swing class called CurrencyConverter
Create a north panel, center panel, and south panel, and add them to your GUI using BorderLayout
In the north panel, it will have a green background, a label that says "Currency Converter", and a button that says "Reset"
Click on "Reset" will clear out the text field, clear out the label in the south panel, and reset the background in the south panel to white
In the center panel, it will have a text field, where you enter the amount in USD to convert, and two buttons"Peso" and "Yen"
In the south panel, make the default background white
If "Peso" is clicked, make the south panel background orange, and display the converted amount pesos
If "Yen" is clicked, make the south panel background red, and display the converted amount in yen
Display the converted amount with 2 decimal places, and a $ sign
conversion ratio is:
1 US dollar = 20.37 Peso
1 US dollar = 114.37 Yen
Explanation / Answer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author SuNiL
*/
public class CurrencyConverter extends JFrame implements ActionListener{
JButton bt1,bt2,bt3;
JLabel lb1,lb2;
JPanel pl1,pl2,pl3;
JTextField tf1;
public CurrencyConverter(){
bt1=new JButton("Reset");
bt2=new JButton("Peso");
bt3=new JButton("Yen");
lb1=new JLabel("Currency Converter");
lb2=new JLabel();
tf1=new JTextField(20);
pl1=new JPanel();
pl2=new JPanel();
pl3=new JPanel();
pl1.setBackground(Color.green);
pl3.setBackground(Color.white);
setLayout(new BorderLayout());
pl1.add(lb1);
pl1.add(bt1);
add(pl1,BorderLayout.NORTH);
pl2.add(tf1);
pl2.add(bt2);
pl2.add(bt3);
add(pl2,BorderLayout.CENTER);
pl3.add(lb2);
add(pl3,BorderLayout.SOUTH);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
}
public static void main(String args[]){
CurrencyConverter ob=new CurrencyConverter();
ob.setSize(700,500);
ob.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
float num=0,val=0;//variables for storing the input and calulating output
try{
num=Float.parseFloat(tf1.getText());//taking input in float format
}catch(NumberFormatException nf){
JOptionPane.showMessageDialog(this, "Enter the appropriate value");//if the input is wrong
}
if(str.equals("Reset")){//reseting the values
tf1.setText("");
lb2.setText("");
pl3.setBackground(Color.white);
} else if(str.equals("Peso")){
pl3.setBackground(Color.orange);
val=(float) (Math.round(num*20.37*100)/100.0);//calculating the value and converting to 2 decimal place
lb2.setText("$ "+num+"= "+(val)+" Peso");//printing the calculated value
} else{
pl3.setBackground(Color.red);
val=(float) (Math.round(num*114.37*100)/100.0);//calculating the value and converting to 2 decimal place
lb2.setText("$ "+num+"= "+(val)+" Yen");//printing the calculated value
}
}
}