Bom dia gente, então, ontem eu estava fazendo um exercício sobre isso, justamente converter Fahrenheit para Celsius, alguma coisa deu errado. Fiz alguns testes comentando algumas partes, parece que a parte de captura dos dados do textField1 tah ok, e a recuperação para mostrar os dados no textField2 também não encontrei problemas, o que deu erro mesmo foi na hora da fórmula, pelo menos pelo que pareceu depois de eu ter feito todos esses testes comentando algumas partes e descomentando (na parte do listener). Depois que passa pela fórmula o double só é mostrado como 0.0 ou -0.0, alguém sabe pq isso, pra mim tem sido um grande mistério :?: :?: :?:
package Exercícios;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class converter extends JFrame{
private JLabel label1;
private JLabel label2;
private JTextField textField1;
private JTextField textField2;
private JButton button;
private JPanel fPanel;
private JPanel cPanel;
public converter(){
super("Converter");
fPanel = new JPanel();
cPanel = new JPanel();
label1 = new JLabel("Fahrenheit Temperature:");
label2 = new JLabel("Celsius Temperature:");
textField1 = new JTextField(5);
textField2 = new JTextField(5);
textField2.setEditable(false);
button = new JButton("Convert");
button.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
double temp = 0;
double tempC = 0;
String tempTxt;
tempTxt = textField1.getText();
temp = Double.parseDouble(tempTxt);
tempC = (5/9) * (temp - 32);//problema
tempTxt = String.valueOf(tempC);
textField2.setText(tempTxt);
}
});
fPanel.add(label1);
fPanel.add(textField1);
cPanel.add(label2);
cPanel.add(textField2);
add(fPanel,BorderLayout.WEST);
add(button,BorderLayout.CENTER);
add(cPanel,BorderLayout.EAST);
}
}
e aqui a main:
package Exercícios;
import javax.swing.JFrame;
public class converterTest {
public static void main(String[] args){
converter conversor = new converter();
conversor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
conversor.setSize(500,60);
conversor.setVisible(true);
}
}
OBS.: fiz isso no macho-programmer, uheuheuhe manualmente, sem usar as ferramentas pra montar GUI e tudo mais, sei lá, mas acho que não tem nada a ver com o erro isso…