Patti
Posts: 46
Nickname: patti
Registered: Feb, 2002
|
|
Re: Currency Converter Help
|
Posted: Apr 18, 2002 1:00 PM
|
|
Well, I've doctored it up some but still can't get the result Label to show any result. Please Help.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//======================================================
//Converts US Dollars to other currency.
//======================================================
public class Currency extends Applet {
private CurrencyGUI gui;
public void init () {
gui = new CurrencyGUI (this);
} //method init
} //class Currency
//======================================================
// Creates the graphical interface for the applet.
//======================================================
class CurrencyGUI {
private static Label titleLabel, conversionLabel,resultLabel,enterLabel;
private static TextField amountText;
private static Choice countryChoice;
private static String USDollars;
public static int enterDollars;
public CurrencyGUI (Applet app) {
FlowLayout tempLayout = (FlowLayout) app.getLayout();
tempLayout.setHgap (45);
app.setBackground (Color.yellow);
titleLabel = new Label (" US Dollars Converter");
conversionLabel = new Label ("Result:", SwingConstants.LEFT);
conversionLabel.setAlignment (SwingConstants.LEFT);
resultLabel = new Label ("N/A", SwingConstants.LEFT);
resultLabel.setAlignment (SwingConstants.LEFT);
enterLabel = new Label ("Enter US Dollars",SwingConstants.LEFT);
enterLabel.setAlignment (SwingConstants.LEFT);
amountText = new TextField (10);
amountText.addActionListener(new ConversionActionListener());
countryChoice = new Choice();
countryChoice.add ("English pound");
countryChoice.add ("Japanese yen");
countryChoice.add ("French franc");
// add the components to the applet
app.add (titleLabel);
app.add (countryChoice);
app.add (conversionLabel);
app.add (resultLabel);
app.add (enterLabel);
app.add (amountText);
app.setSize (250,250);
} //constructor CurrencyGUI
public static int getCurrencyChoice() {
return countryChoice.getSelectedIndex();
} //method getCurrencyChoice
public static String getUSDollars () {
String USDollars = enterLabel.getText();
enterDollars = Integer.parseInt (USDollars);
return USDollars;
}
public static String setResultLabel(String resultText) {
resultLabel.setText(resultText);
String result;
return result;
}
class ConversionActionListener implements ActionListener {
private TextField amountText;
private Button convertButton;
public ConversionActionListener (TextField textListen, Button buttonListen){
this.amountText = textListen;
this.convertButton = buttonListen;
} //ConversionActionListener
public void actionPerformed (ActionEvent event){
Double Dollars;
double dollars;
double result;
String resultText;
Dollars = Double.valueOf(CurrencyGUI.getUSDollars());
dollars = Dollars.doubleValue();
result = CurrencyConverter.convert(dollars);
resultText = Double.toString(result);
CurrencyGUI.setResultLabel (resultText);
} //actionPerformed
} //class conversionActionListener
} //class CurrencyGUI
//==============================================================
// Contains the code to perform the conversion.
//==============================================================
class CurrencyConverter {
final static double POUNDSTOUSD = 0.61203;
final static double YENTOUSD = 138.509;
final static double FRANCSTOUSD = 5.97415;
final static int Pound = 0;
final static int Yen = 0;
final static int Franc = 0;
public static double money;
public static double convert (double dollars) {
double conversionFactor = 0.0;
int convertTo = CurrencyGUI.getCurrencyChoice();
switch (convertTo){
case 1: conversionFactor = POUNDSTOUSD;
break;
case 2: conversionFactor = YENTOUSD;
break;
case 3: conversionFactor = FRANCSTOUSD;
break;
default: conversionFactor = 0.0;
}
money = dollars * conversionFactor;
return money;
} //method convert
} //class CurrencyConverter
|
|