I'm just wondering what is wrong with this? All it needs to do is calculate farenheight from celcius upon pressing the enter key. It's displaying the entered temp but it's not displaying the calculated ceclius temp. I added the test label to verify that setText() was indeed working upon pressing enter(which it does)...mysterious.
I tried doing something like output.setText(String.format("%.2f", math)); and output.setText("" + celcius); ...What the heck...?
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TempetureConvert extends JFrame { /** * */ private static final long serialVersionUID = 5966229927379459783L; private JTextField f; private JLabel output; private JLabel test; private String user; private JPanel panel; private double celcius, math; String text; public TempetureConvert(){ f = new JTextField(""); f.setPreferredSize(new Dimension(35, 25)); panel = new JPanel(); this.setTitle("Temperature Converter"); this.setSize(300, 80); output = new JLabel(""); test = new JLabel(""); panel.add(test); panel.add(f); panel.add(output); this.add(panel); f.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { //JTextField f = (JTextField) e.getSource(); if(e.getKeyCode() == KeyEvent.VK_ENTER){ math = new Double(f.getText()); test.setText("" + math); celcius = (5/9)*(math-32); text = Double.toString(celcius); output.setText(text); } } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } }); this.setVisible(true); } public String getUser() { return user; } public void setUser(String user) { this.user = user; } }