conversor_moeda

Boas pessoal! Tenho que fazer um conversor de moeda. Para isso, tenho 2 combobox com as currency. Preciso de fazer a selecção das moedas nesses combobox e depois digitar o valor. No fundo na textfield tfResultado tem que aparecer as moedas que escolhi e o valor k intruduzi com o valor da conversão. Já fiz o layout e implementei código no entanto ainda não está a funcionar :S Não consigo indicar ao programa quais as moedas que desejo converter nem ir buscar a taxa de conversão na matriz para fazer o calculo… Se alguem me puder ajudar agradecia

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class experiencia_moeda extends JFrame implements ActionListener {

    private JTextField tfValor, tfResultado;
    private JComboBox cbMoeda1, cbMoeda2;
    private JLabel lConverter, lPara, lValor;
    private JButton bConverter;
    String[] currency = {"EUR" ,"USD", "GBP", "JPY", "CHF"};

    public experiencia_moeda (String titulo) {

        super (titulo);
        cbMoeda1 = new JComboBox(currency);
        cbMoeda2 = new JComboBox(currency);
        tfValor = new JTextField(5);
        bConverter = new JButton ("Converter");
        lConverter = new JLabel ("Conversão de:");
        lPara = new JLabel ("para:");
        lValor = new JLabel ("Valor:");
        tfResultado = new JTextField (10);
        bConverter.addActionListener (this);
        init();    
    }

    public void init() {

        setSize (300, 200);

        setLayout (new BorderLayout ());
        JPanel pNorte = new JPanel (new GridLayout (1, 1));
        JPanel pCentro = new JPanel (new FlowLayout());
        JPanel pSul = new JPanel (new GridLayout (2, 1));

        pNorte.add(lConverter);
        pNorte.add(cbMoeda1);    
        pNorte.add(lPara);
        pNorte.add(cbMoeda2);

        pCentro.add(lValor);
        pCentro.add(tfValor);

        pSul.add(bConverter);
        pSul.add(tfResultado);

        add (pNorte, BorderLayout.NORTH);    
        add (pCentro, BorderLayout.CENTER);
        add (pSul, BorderLayout.SOUTH);
    }

    public void actionPerformed (ActionEvent ae) {

        if (ae.getSource () == bConverter) {

            double[][] matrix = {
                    {1.0, 1.25, 0.625, 125.0, 1.5626},    
                    {0.8, 1.0, 0.5, 100.0, 1.25},
                    {1.6, 2.0, 1.0, 200.0, 2.5},
                    {0.008, 0.01, 0.005, 1.0, 0.0125},
                    {0.64, 0.8, 0.4, 80, 1.0}
            };

            cbMoeda1.setSelectedIndex(4);
            cbMoeda1.addActionListener(this);
            cbMoeda2.setSelectedIndex(4);
            cbMoeda2.addActionListener(this);

            for (int i = 0; i < matrix.length; i++) {
                tfResultado.setText("\t" + cbMoeda1);
            }

            for (int i = 0; i < matrix.length; i++) {
                tfResultado.setText("\t" + cbMoeda2);

                for (int j = 0; j < matrix[i].length; j++) {

                    tfResultado.setText(cbMoeda1 + tfValor.getText() +  cbMoeda2 + matrix[i][j]);
                }

                System.out.println();
            }
        }
    }

    public static void main (String[] args) {
        System.out.println ("Falta muita coisa!");
        experiencia_moeda conversor = new experiencia_moeda ("Conversor de Moeda!");
        conversor.setSize (300, 200);
        conversor.setVisible(true);
    }
}

Você não foi muito específico ao dizer com o quê precisa de ajuda. Se precisar formatar moedas em campos jformatedtextfield use o código abaixo:

NumberFormat numberFormat= new java.text.DecimalFormat("##,#0.00");//seu formatador
String formatado = numberFormat.format(seuFloatAqui);

Espero ter ajudado em algo.

boa noite!!!

já fiz o meu programa e tá todo certo. Ele apenas não me faz a multiplicação para a conversão das moedas… alguem me pode ajudar? É este o código (tfResultado.setText("" + Double.parseDouble(tfValor.getText()) * matrix[valorX][valorY]);), mas não tá a fazer:S:S:S:S:s

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ConversorMoeda extends JApplet implements ActionListener {

	private JTextField tfValor, tfResultado;

	private JComboBox cbMoeda1, cbMoeda2;

	private JLabel lConverter, lPara, lValor;

	private JButton bConverter;

	int valorX = 0;

	int valorY = 0;

	String[] currency = {"EUR" ,"USD", "GBP", "JPY", "CHF"};

	double[][] matrix = {
			{1.0, 1.25, 0.625, 125.0, 1.5626},	
			{0.8, 1.0, 0.5, 100.0, 1.25},
			{1.6, 2.0, 1.0, 200.0, 2.5},
			{0.008, 0.01, 0.005, 1.0, 0.0125},
			{0.64, 0.8, 0.4, 80, 1.0}
	};

	public void init() {

		setSize (300, 200);

		cbMoeda1 = new JComboBox(currency);

		cbMoeda2 = new JComboBox(currency);

		tfValor = new JTextField(5);

		bConverter = new JButton ("Converter");

		lConverter = new JLabel ("Conversão de:");

		lPara = new JLabel ("para:");

		lValor = new JLabel ("Valor:");

		tfResultado = new JTextField (10);

		bConverter.addActionListener (this);

		setLayout (new BorderLayout ());
		JPanel pNorte = new JPanel (new GridLayout (1, 1));
		JPanel pCentro = new JPanel (new FlowLayout());
		JPanel pSul = new JPanel (new GridLayout (2, 1));

		pNorte.add(lConverter);

		pNorte.add(cbMoeda1);

		pNorte.add(lPara);

		pNorte.add(cbMoeda2);

		pCentro.add(lValor);

		pCentro.add(tfValor);

		pSul.add(bConverter);

		pSul.add(tfResultado);

		add (pNorte, BorderLayout.NORTH);

		add (pCentro, BorderLayout.CENTER);

		add (pSul, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent ae) {
		tfResultado.setText("");
		if (ae.getSource() == bConverter) {		

			if( Double.parseDouble(tfValor.getText()) > 0) {

				if (cbMoeda1.getSelectedItem() == "EUR"); {
					valorX = 0;
				}

				if (cbMoeda1.getSelectedItem() == "USD"); {
					valorX = 1;
				}

				if (cbMoeda1.getSelectedItem() == "GBP"); {
					valorX = 2;
				}

				if (cbMoeda1.getSelectedItem() == "JPY"); {
					valorX = 3;
				}

				if (cbMoeda1.getSelectedItem() == "CHF"); {
					valorX = 4;
				}

				if (cbMoeda2.getSelectedItem() == "EUR"); {
					valorY = 0;
				}

				if (cbMoeda2.getSelectedItem() == "USD"); {
					valorY = 1;
				}

				if (cbMoeda2.getSelectedItem() == "GBP"); {
					valorY = 2;
				}

				if (cbMoeda2.getSelectedItem() == "JPY"); {
					valorY = 3;
				}

				if (cbMoeda2.getSelectedItem() == "CHF"); {
					valorY = 4;
				}

				tfResultado.setText("" + Double.parseDouble(tfValor.getText()) * matrix[valorX][valorY]);
			}
		}
	}
}