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);
}
}