Created
May 28, 2013 14:19
-
-
Save betaveros/5663084 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
public class CurrencyExchange { // I'm not an actionListener! | |
String[] differentCurrency = {"US Dollars","Japanese YEN","Chinese YUAN", "Korean WON"}; | |
JFrame frame; | |
JPanel contentPane; | |
JLabel label, result; | |
JButton button; | |
// lesson 1. proper naming (start w/ lowercase for typical variable names) | |
// sorry, I can't read code easily without such conventions | |
JTextField initial, result1; | |
// moved JComboBox! | |
JComboBox list; | |
public CurrencyExchange(){ | |
/* Create and set up the frame */ | |
frame = new JFrame("Currency Converter"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
/* Create a content pane */ | |
contentPane = new JPanel(); | |
/* Create and add label */ | |
label = new JLabel("Enter Amount in New Taiwan Dollars (NTD):"); | |
contentPane.add(label); | |
initial = new JTextField(6); | |
contentPane.add(initial); | |
list = new JComboBox(differentCurrency); | |
contentPane.add(list); | |
JButton run= new JButton("Convert"); | |
// behold an anonymous ActionListener | |
// you don't have to test for the source anymore, only this button can call it | |
run.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
String firstNumber = initial.getText(); | |
double finalresult; | |
String course = (String) list.getSelectedItem(); | |
if(course.equalsIgnoreCase("US Dollars")) { | |
finalresult = Double.parseDouble(firstNumber) * 0.03; | |
result1.setText(finalresult + ""); | |
} | |
} | |
}); | |
contentPane.add(run); | |
result = new JLabel("Converted Result"); | |
contentPane.add(result); | |
result1 = new JTextField(6); | |
// result1.addActionListener(this); | |
contentPane.add(result1); | |
contentPane.setLayout(new GridLayout(10, 0, 0, 0)); | |
/* Add content pane to frame */ | |
frame.setContentPane(contentPane); | |
/* Size and then display the frame. */ | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
private static void runGUI(){ | |
JFrame.setDefaultLookAndFeelDecorated(false); | |
CurrencyExchange greeting = new CurrencyExchange(); | |
} | |
public static void main(String[] args){ | |
javax.swing.SwingUtilities.invokeLater(new Runnable(){ | |
public void run (){ | |
runGUI(); | |
} | |
});} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment