Swing App Currency Converter 1.0 - darthjames226 - 11-28-2013
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.
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");
}
}
}
Swing App Currency Converter 1.0 - darthjames226 - 11-28-2013
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.
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");
}
}
}
RE: Swing App eCurrency Converter - Deque - 11-28-2013
I would have used an Enum for the currency (instead of the "constants") as well as the strings related to that. It will spare you the switch-case statement.
I also suggest you use Java docs instead of just comments.
Other than that I see nothing wrong with it.
RE: Swing App eCurrency Converter - bananamango - 11-28-2013
Idea: Use online stats to make your converter time accurate without updating.
RE: Swing App eCurrency Converter - Daedalus - 11-28-2013
Although I only have very little experience with Java, your code doesn't look too bad at all. As much as I dislike Java, I do give it props for how easy it is to write GUIs using Swing.
RE: Swing App eCurrency Converter - Coder-san - 11-28-2013
Why eCurrency? It's Real Currency you are dealing with.
eCurrency are Paypal, and gang.
RE: Swing App eCurrency Converter - darthjames226 - 11-28-2013
(11-28-2013, 08:56 PM)Deque Wrote: I would have used an Enum for the currency (instead of the "constants") as well as the strings related to that. It will spare you the switch-case statement.
I also suggest you use Java docs instead of just comments.
Other than that I see nothing wrong with it.
You made some very valid points. I will work on the suggestions you made. Thanks for the advice.
(11-28-2013, 09:00 PM)bananamango Wrote: Idea: Use online stats to make your converter time accurate without updating.
This is a very interesting idea. Any suggestions on how I can go about implementing this in my code?
(11-28-2013, 09:09 PM)Knee-O Wrote: Although I only have very little experience with Java, your code doesn't look too bad at all. As much as I dislike Java, I do give it props for how easy it is to write GUIs using Swing.
Thanx. I have some re-factoring to do but I'm satisfied with the results thus far. I'm going to implement a FlowLayout instead of BorderLayout to improve on the GUI. Yes I have found Java to be very convenient when designing a GUI from scratch without the using a point and click GUI designer. I prefer to code from scratch then a GUI builder. You learn a lot more that way.
(11-28-2013, 09:21 PM)Coder-san Wrote: Why eCurrency? It's Real Currency you are dealing with.
eCurrency are Paypal, and gang.
You're absolutely correct and I will implement that change. I've made numerous apps in different languages which I always prefixed the title with an 'e'. However in this case it does make sense to remove it. Thanx for pointing this out.
RE: Swing App eCurrency Converter - bananamango - 11-28-2013
(11-28-2013, 10:13 PM)darthjames226 Wrote: (11-28-2013, 08:56 PM)Deque Wrote: I would have used an Enum for the currency (instead of the "constants") as well as the strings related to that. It will spare you the switch-case statement.
I also suggest you use Java docs instead of just comments.
Other than that I see nothing wrong with it.
You made some very valid points. I will work on the suggestions you made. Thanks for the advice.
(11-28-2013, 09:00 PM)bananamango Wrote: Idea: Use online stats to make your converter time accurate without updating.
This is a very interesting idea. Any suggestions on how I can go about implementing this in my code?
(11-28-2013, 09:09 PM)Knee-O Wrote: Although I only have very little experience with Java, your code doesn't look too bad at all. As much as I dislike Java, I do give it props for how easy it is to write GUIs using Swing.
Thanx. I have some re-factoring to do but I'm satisfied with the results thus far. I'm going to implement a FlowLayout instead of BorderLayout to improve on the GUI. Yes I have found Java to be very convenient when designing a GUI from scratch without the using a point and click GUI designer. I prefer to code from scratch then a GUI builder. You learn a lot more that way.
Well brief overview would be to:
Open a socket
Read some websites rss feed on currency exchanges
Use regex to sort out the numbers.
RE: Swing App Currency Converter 1.0 - darthjames226 - 11-28-2013
(11-28-2013, 10:24 PM)bananamango Wrote: Well brief overview would be to:
Open a socket
Read some websites rss feed on currency exchanges
Use regex to sort out the numbers.
I will most definitely look into this. Thanx for the advice. :Thumbs-Up:
RE: Swing App Currency Converter 1.0 - bananamango - 11-28-2013
(11-28-2013, 10:29 PM)darthjames226 Wrote: (11-28-2013, 10:24 PM)bananamango Wrote: Well brief overview would be to:
Open a socket
Read some websites rss feed on currency exchanges
Use regex to sort out the numbers.
I will most definitely look into this. Thanx for the advice. :Thumbs-Up:
No problem man, goodluck with it. Keep me informed on how you do.
|