Learning Java: First GUI
Just started learning Java and I wrote a simple Celsius-Fahrenheit conversion program. I'm looking for feedback.. There are some issues which I was tenative about e.g. "thread safe", layout management, wether this is OOP programming, etc. You pros out there have any comments about implementation I would welcome them.
I'm kinda proud of it. I managed to sepearate the GUI from the temperature conversion logic. Also, whenever either input field loses focus, the temperature conversion class is called and the opposite field is updatged. Here is the code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.*;//need for BigDecimal even though I cant make BigDecimal work
import java.text.*;//need for DecimalFormat
class MyFrameextends JFrameimplements ActionListener, FocusListener{
JLabel promptLbl =new JLabel("Enter a temperature then press Convert");
JTextField farInputTxt =new JTextField(5);
JLabel farInputLbl =new JLabel("癋");
JTextField celInputTxt =new JTextField(5);
JLabel celInputLbl =new JLabel("癈");
JButton resetBtn =new JButton("Reset");
// instantiate an instance of Focus1
DefaultKeyboardFocusManager myFocusmgr =new DefaultKeyboardFocusManager();
DecimalFormat df =new DecimalFormat("#.##");//overkill but just experimenting with different
//BigDecimal amount = new BigDecimal("#.##"); // ways to format long doubles to fit in text boxes
// hmmm, exception in main, if I leave in BigDecimal.
Temperature temp =new Temperature();
double farInput;
double celInput;
// constructor MyFrame
public MyFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,100);
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
pane.add(promptLbl);
pane.add(farInputLbl);
pane.add(farInputTxt);
pane.add(celInputLbl);
pane.add(celInputTxt);
pane.add(resetBtn);
//add FocusListener
farInputTxt.addFocusListener(this);
celInputTxt.addFocusListener(this);
// add eventListener
resetBtn.addActionListener(this);
setVisible(true);
farInputTxt.setText("0.00");//these stmts make errors if I place them anywhere else
celInputTxt.setText("0.00");
}//end constructor MyFrame
// focus listeners
publicvoid focusLost(FocusEvent e){
if (e.getSource()== farInputTxt){
System.out.println("Fahrenheit field lost focus: " + e.getComponent());
double farInput = Double.parseDouble(farInputTxt.getText().trim());
//celInputTxt.setText(String.valueOf(temp.toCel(farInput)));
celInputTxt.setText(String.valueOf(df.format(temp.toCel(farInput))));
}else{
System.out.println("Celsius field lost focus: " + e.getComponent());
double celInput = Double.parseDouble(celInputTxt.getText().trim());
//farInputTxt.setText(String.valueOf(temp.toFar(celInput)));
farInputTxt.setText(String.valueOf(df.format(temp.toFar(celInput))));
}// end if else
}//end method focusLost
publicvoid focusGained(FocusEvent e){
// thought I needed this; decided to leave it in
}// end method focusGained
publicvoid actionPerformed(ActionEvent e){
if (e.getSource()==resetBtn){
System.out.println("reset called");
farInputTxt.setText("0.00");
celInputTxt.setText("0.00");
}// end if
}// end actionPerformed
publicstaticvoid main(String[] args){
new MyFrame();
}// end main method
}// end class MyFrame
and here is the Temperature class:
publicclass Temperature{
publicdouble toFar(double cel){
double far;
far = ((9.0/5.0)*cel)+32;
return far;
//formula for fahrenheit Tf = (9/5)*Tc+32
}//end method toFar
publicdouble toCel(double far){
double cel;
cel = (far-32)*(5.0/9.0);
return cel;
// formula for celsius Tc = (5/9)*(Tf-32)
}//end method toCel
}// end class Temperature

