Write a Java Application using Swing GUI API to convert US dollars to Euro and E
ID: 3760763 • Letter: W
Question
Write a Java Application using Swing GUI API to convert US dollars to Euro and Euro to US dollars. In the GUI you have to show:
1) Your name in different color and font 2) you ID in different color and font 3) A photo of your high school or undergraduate college with appropriate size 4) The name of your high school or undergraduate college beside the photo 5) taking input as US dollars and produce output as Euro 6) taking input as Euro and produce output as US dollars. 7) The output show up in the same GUI as seen in the attached sample screenshot. 8) Your designed GUI should look like as the attached screenshot.
.
You can use GridBagLayou as Layout manager.
You can use setFont, setForeground, setIcon methods of JLabel class
kilo_miles_advanced.jpg
Explanation / Answer
Answer :
import java.awt.event.*;
import javax.swing.*;
public class ConverterCurrencyEuro_Dollar extends JFrame {
private JLabel euroLabel;
private JTextField euroAmountField;
private JLabel dollarLabel;
private JTextField dollarAmountField;
private JButton convertEuroButton;
private JButton convertDollarsButton;
private JLabel converseRateLabel;
private JTextField converseRateField;
private static final double INITIAL_CONVERSE_RATE = 1.07;
private static final int FIELD_SIZE = 5;
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 50;
public ConverterCurrencyEuro_Dollar() {
this.createComponents();
this.createPanel();
this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void createComponents() {
this.converseRateLabel = new JLabel("Converse rate: ");
this.converseRateField = new JTextField(FIELD_SIZE);
this.converseRateField.setText(String.valueOf(INITIAL_CONVERSE_RATE));
this.euroLabel = new JLabel("Euro: ");
this.euroAmountField = new JTextField(FIELD_SIZE);
this.euroAmountField.setText("0");
this.dollarLabel = new JLabel("Dollars: ");
this.dollarAmountField = new JTextField(FIELD_SIZE);
this.dollarAmountField.setText("0");
this.createButtons();
}
private void createButtons() {
class ConvertListener implements ActionListener {
private String toCurrency;
public ConvertListener(String currency) {
this.toCurrency = currency.toLowerCase();
}
public void actionPerformed(ActionEvent e) {
double conversionRate = INITIAL_CONVERSE_RATE;
try {
conversionRate = Double.parseDouble(converseRateField.getText());
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Conversion rate must be a positive
number",
"Error: invalid conversion rate", JOptionPane.ERROR_MESSAGE);
}
if (this.toCurrency.equals("euro")) {
try {
double amount = Double.parseDouble(dollarAmountField.getText());
euroAmountField.setText(String.format("%.4f", amount / conversionRate));
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Dollar amount must a positive number",
"Error: invalid dollar amount", JOptionPane.ERROR_MESSAGE);
}
} else if (this.toCurrency.equals("dollars")) {
try {
double amount = Double.parseDouble(euroAmountField.getText());
dollarAmountField.setText(String.format("%.4f", amount / conversionRate));
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Euro amount must be a positive
number",
"Error: invalid euro amount", JOptionPane.ERROR_MESSAGE);
}
}
}
}
this.convertEuroButton = new JButton("<");
ActionListener convertEuroListener = new ConvertListener("euro");
this.convertEuroButton.addActionListener(convertEuroListener);
ActionListener converDollarsListener = new ConvertListener("dollars");
this.convertDollarsButton = new JButton(">");
this.convertDollarsButton.addActionListener(converDollarsListener);
}
private void createPanel() {
JPanel panel = new JPanel();
panel.add(euroLabel);
panel.add(euroAmountField);
panel.add(convertEuroButton);
panel.add(converseRateLabel);
panel.add(converseRateField);
panel.add(convertDollarsButton);
panel.add(dollarLabel);
panel.add(dollarAmountField);
this.add(panel);
}
public static void main(String[] args) {
JFrame testFrame = new ConverterCurrencyEuro_Dollar();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.