Ajuda com Erro

2 respostas
T
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;

public class Estacionamento extends JApplet implements ActionListener {
	JLabel tempoLabel, valorLabel, totalLabel;
	JTextField tempoField, valorField, totalField;
	double total = 0;
	
	public void init() {
		Container container = getContentPane ();
		container.setLayout (new FlowLayout());
		
		JOptionPane.showMessageDialog (null,"*¨* Tabela de Preços *¨*\n\nAté 3 horas: R$2,00\nHoras adicionais: R$ 5,00\nAcima de 16hs: R$ 10,00");
		
		tempoLabel = new JLabel ("Tempo de estacionamento");
		container.add (tempoLabel);
		tempoField = new JTextField (10);
		container.add(tempoField);		
		
		tempoField.addActionListener(this);
		
		valorLabel = new JLabel ("Valor a ser cobrado");
		container.add (valorLabel);
		valorField = new JTextField (10);
		valorField.setEditable (false);
		container.add(valorField);
		
		totalLabel = new JLabel ("Total de Lucro");
		container.add (totalLabel);
		totalField = new JTextField (15);
		totalField.setEditable (false);
		container.add (totalField);
	}
	
	public void actionPerformed (ActionEvent e) {
		int tempo;
		double taxa;
				
		tempo = Integer.parseInt (tempoField.getText());
		taxa = calculateCharges(tempo);
		total = total + taxa;
		showStatus ("Valor a Pagar");
		valorField.setText(Double.toString (taxa));
		
		
		total = Double.parseDouble (totalField.getText());
		
	}
	
	public double calculateCharges (double t) {
		if (t <= 3)
		return 2;
		else if (t >= 19)
		return 10;
		else
		return 2 + (t - 3) * 0.5; 
	}
}

O código acima compila normalmente, mas não está acumulando os valores na variável totalField. Alguém pode me ajudar???? Obrigado!!

2 Respostas

T

Achei o erro galera… O correto é totalField.setText(Double.toString(total)); e não total = Double.parseDouble (totalField.getText());

Mas valeu mesmo assim!! Abraços

C

Voce pode colocar por convençao o metodo trim()

total = Double.parseDouble (totalField.getText().trim());

Criado 28 de maio de 2007
Ultima resposta 29 de mai. de 2007
Respostas 2
Participantes 2