Jump to content

NEED HELPP!


Recommended Posts

well im trying to make a currency calculator using java. Anyone know any java programming sites? or can anyone help start one? I have the GUI done, now I want a list box that would have the different conversions...ex.. dollars -> yen, pounds -> pesos, etc. etc...well once you make a selection you just type in the amount and click on convert to convert the amount...help anyone??

Link to comment
Share on other sites

Woah woah, slow down.. Is this for a class or something?

 

Are you doing an applet or an actual program?

 

How is it you got the GUI done without knowing how make it work?

 

Are you programming from scratch or are you using existing code (or plug-in code)?

 

 

It should be pretty easy, you'll just need a frame with a textfield, a label, the listbox and a button. Then have the listbox set the value for a variable, and use that to reference a switch in your actionperformed for the button.

 

If you have specific questions or whatever I can probably help. You can PM or IM or whatever.

Link to comment
Share on other sites

Is this for a class or something?

Yeah its for skool

 

Are you doing an applet or an actual program?

The actual program

 

How is it you got the GUI done without knowing how make it work?

Its from an existing code, so I change the program title and window size

 

Are you programming from scratch or are you using existing code (or plug-in code)?

Im using an existing code from a basic mutliplying program

Link to comment
Share on other sites

Alright, well see my little blurb above.. that would be how I would go about it anyway.

 

If you're not already using it, this is a godsend for java programming: http://java.sun.com/j2se/1.5.0/docs/api/

 

Click on the class you're interested in on the left (combobox, for example) and you'll get all the functions and constructors and things for it. Handy stuff.

 

Is this your first program for the class? It's kind of complicated for a first program... Or are you just looking for how lists work?

Edited by Daeval
Link to comment
Share on other sites

Alright, I don't know how complicated your teacher wants it, but here's a real basic way to do it I whipped up just now. I'd never messed with basic listboxes before (just combo/pull down), so I used this as a chance to practice.

 

I think I probably underestimated your coding ability when I first read your question, so I'm going to assume you know about creating an instance of a frame, etc. Conversion numbers are obviously made up. You'd also want to play with a NumberFormat to get the output looking nice.

 

 

// Imports
import java.awt.*;
import java.awt.event.*;


public class ConversionFrame extends java.awt.Frame implements ActionListener
{
// Class Variables
TextField txtInput;
Label lblResults;
Button btnCalculate;
List lstConversions;

// Constructor
   public ConversionFrame(String title, int width,int height)
   {

 // I'm going to assume you know how to set the title, 
 // dimensions, and give it a windowlistener to close the frame
 // I also didn't bother making it pretty.

 // Declare UI Stuff
 txtInput = new TextField(15);
 lblResults = new Label("               ");
    btnCalculate = new Button("Convert");
 lstConversions = new List(4, false);

 // Add list components
 lstConversions.add("USD to Euros", 0);
 lstConversions.add("USD to Yen",1);
 lstConversions.add("USD to Pesos",2);
 lstConversions.add("USD to Canadian",3);

 // Add objects to the frame
    add(txtInput);
    add(lstConversions);
    add(btnCalculate);
    add(lblResults);

 // Give the button the ActionListener
    btnCalculate.addActionListener(this);

    // Display the frame
 setVisible(true);
   }


   // This is what happens when you hit the button...
public void actionPerformed(ActionEvent e)
{
 double input = Float.parseFloat(txtInput.getText());

 // Test the selected index number
 switch(lstConversions.getSelectedIndex())
 {
	 case 0:  // USD to Euros
   lblResults.setText(Double.toString(input * 1.25));
   break;

	 case 1:  // USD to Yen
   lblResults.setText(Double.toString(input * 0.25));
   break;

	 case 2:  // USD to Pesos
   lblResults.setText(Double.toString(input * 12.5));
   break;

	 case 3:  // USD to Canadian
   lblResults.setText(Double.toString(input * 0.125));
   break;

	 default:
   lblResults.setText("Error");
 }
}

}

Edited by Daeval
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...