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

[7632 byte] By [kdwoella] at [2007-11-27 3:37:47]
# 1

HI kdwoell!

I have just joined this forum, so this is my first post here:) but I think I can give you tips about your code.

Separate ActionListener and FocusListener implementations from JFrame implementation. This is about style (if you have a possiblity, check how IDEs handle GUI code generation), better way would be to put these interfaces implementations in separate classes (although you should leave it in the same file)

Also try to initialise variables in constructor or in function called from constructor - leave only declarations where you have initialisations now and move initialisations to constructor.

When it comes to application logic:

I have started your code, typed 10 into F, clicked on Celsius textbox, then focused Celsius textbox again and this is the result:

java.lang.NumberFormatException: For input string: "-12,22"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)...

You are parsing string with "," into double

You should read smthing about handling exceptions - try/catch clausule;)

I hope, this information would help somehow

Kind Regards

Marecki6000

marecki6000a at 2007-7-12 8:41:04 > top of Java-index,Java Essentials,New To Java...
# 2
One thing I think you could do is improve your documentation. It's good to comment your code - but} // end if/else/method/constructor/. . .comments that state the obvious are unnecessary and distracting!
ErikSilkensena at 2007-7-12 8:41:04 > top of Java-index,Java Essentials,New To Java...