Swing App Currency Converter 1.0 11-28-2013, 06:04 AM
#1
This is a swing application coded from scratch by myself. I recently started to learn swing and decided to do a project for more experience. Just thought I would share my source code with you guys in case you want to learn swing. Any feedback for improvement would be much appreciated. I would like to re-position my widgets to make it more presentably but I'm new to swing and haven't learned how to take full control of my positioning. Any suggestions?
This app converts US currency to the currency value of 5 different countries.
This app converts US currency to the currency value of 5 different countries.
Code:
import javax.swing.*;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
/**
* Currency Converter 1.0
* Written by: Josh
* Date: 11/27/13
*/
public class GUIMain implements ActionListener {
//Declare variables for widgets
private JLabel convert;
private JLabel usDollars;
private JLabel result;
private JTextField usAmount;
private JComboBox currency;
private JButton submit;
//Set up content for combo box
private String[] money = {"Euro", "Japanese Yen", "Canadian Dollars", "Korean Won", "Mexican Pesos"};
//Declare constants for currency conversion
public static final double EURO = 0.74;
public static final double YEN = 100.41;
public static final double CANADIAN = 1.05;
public static final double WON = 1062.10;
public static final double PESOS = 13.11;
//Main function
public static void main(String[] args){
GUIMain gui = new GUIMain();
gui.go();
}
//Set up GUI
public void go() {
//Configure widgets
convert = new JLabel("Convert:");
usAmount = new JTextField(10);
usDollars = new JLabel("U.S. dollars to:");
result = new JLabel("");
submit = new JButton("OK");
submit.addActionListener(this);
currency = new JComboBox(money);
currency.setSelectedIndex(4);
//Create a new Layout
FlowLayout layout = new FlowLayout();
//Create panel and add widgets
JPanel container = new JPanel();
container.add(convert);
container.add(usAmount);
container.add(usDollars);
container.add(currency);
container.add(submit);
container.add(result);
container.setLayout(layout);
container.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
container.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
//Create frame, set properties, and add panel
JFrame frame = new JFrame();
frame.setTitle("Currency Converter 1.0");
frame.setSize(440, 300);
frame.getContentPane().add(container);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
//Convert selected currency and display
@Override
public void actionPerformed(ActionEvent e) {
DecimalFormat df = new DecimalFormat("#.##");
try {
double oldAmount = Double.parseDouble(usAmount.getText());
int index = currency.getSelectedIndex();
Double newAmount = 0.0;
switch (index){
case 0: newAmount = oldAmount * EURO;
break;
case 1: newAmount = oldAmount * YEN;
break;
case 2: newAmount = oldAmount * CANADIAN;
break;
case 3: newAmount = oldAmount * WON;
break;
case 4: newAmount = oldAmount * PESOS;
break;
}
result.setText(" " + df.format(newAmount));
} catch (Exception ex){
result.setText("Error: Please enter a valid currency amount");
}
}
}

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)



![[Image: rytwG00.png]](http://i.imgur.com/rytwG00.png)