In this tutorial, we will create a program that converts a Celsius measurement to Fahrenheit, and vice versa. To keep things simple, the layout and architecture of the program will not be perfect, but I am sure after reading this, you could change it around and optimize it. For now, we will just work on getting used to using EVENT and ACTIONLISTENER concepts.
First off, we import all of the java utils and packages we need for this program to work. The new import in this case will be java.awt.event.* .The import code should look like this:
import java.awt.*;
import java.awt.event.*
;import javax.swing.*;
Next, we can declare our class and start defining some variables. It is very important to not only extend JFrame this time, but also implement ActionListener. If you never programmed using actions before, you may not be familiar with how they work. Programs that do not include an action listener are compiled from top to bottom. They are not event based driven meaning that the end user only has a single action to possibly perform at one time. In event based driven programs, the end user performs an action (such as clicking a button), and the program “listens” for this action. Once this “listener” is told what to do, the program does something based on the action. This should become clear after the following program is created. The next line should look like this:
public class Thermo extends JFrame implements ActionListener{
JLabel title; //title variable declared
JTextField cel; //Celsius variable declared as a text field where the end user can input something
JTextField far; //similar to above variable but for Far.
JTextArea answer; //a text area variable where the answer will be displayed.
To review, the program will contain a frame in which a label, 2 text fields for input, and a text area for output is used to convert Celsius to Fahrenheit and vice versa. Now that we have declared our variables, we can make our constructor. The constructor will look like this:
super("Conversion Program"); //heading of the frame
setLayout(new FlowLayout()); //set the layout to our previously used flow layout.
title = new JLabel(" ");
cel = new JTextField(10); //new object for JTextField, 10 chars long
far = new JTextField(10); //same as above
answer = new JTextArea(10,30); //object for the text area, 10x30
cel.addActionListener(this); //both variables are added as action listeners
far.addActionListener(this);
add(title); //the variables are added to the layout of the frame
add(cel);
add(far);
add(answer);
}
We’re actually almost done. What comes next is forming the logic of the program. We create a code that will convert the number entered and display it in the text area. This is done with the function below:
public void actionPerformed (ActionEvent event){
if (event.getSource() == cel){
int celnum = 0; //declared new variable
celnum = Integer.parseInt(cel.getText());
answer.append("Your answer in Fahrenheit = " + (celnum*9/5+32) + "\n");
}
else if (event.getSource() == far){
int farnum = 0;
farnum = Integer.parseInt(far.getText());
answer.append("Your answer in Celsius = " + ((farnum-32)*5/9) + "\n");
}
}
The logic behind this is pretty simple. We are telling the program that if the source is the cel textfield, then append the answer in Fahrenheit by multiplying the celnum variable by 9/5 then adding 32. The else is statement is very similar.
The final step is of course creating our main function, which will contain the visibility, and size of the frame. The final function should look like this:
public static void main(String[] args) {
Thermo framea = new Thermo();
framea.setVisible(true);
framea.setSize(500, 400); }
}
The program in NetBeans looks like this:

When compiled, it looks like this:

I hope this tutorial was useful. There will be more complex tutorials on frames soon.





It's hard not to laugh when you see this. It is kind of ironic, wouldn't you say? I mean after such a great season with…
St. Patrick's Day is a Christian holiday celebrating the life of Saint Patrick; he is associated with bringing Christianity to Ireland in the 5th century.…
Make St. Patrick's Day explosive with a few Irish Car Bombs!
Food Network Canada has failed at its attempt to replicate the American Food Network's "Diners, Drive-Ins and Dives". While there's no denying that shows revolving…
Alright, so we’ve got the essential basics down. We’ve created out first frame and it has some text in it using JLables. Now it’s time…
The summer is in full swing and I am often asked to convert between Celsius and Fahrenheit. The exact calculation is: [(Degrees Celsius)*(9/5)] + 32…
If you are anything like me, you love tabbed browsing. I love it so much infact, that I typically have between 6 and 15 tabs…
So, many Gounce users have been inquiring about how to create a list of files or folders inside of a directory. So many in fact,…
If you're reading this, you’ve undoubtedly ran into a PDF conversion problem. Don’t worry; it happens to all of us. There are many times where…
It is no secret that computer technology moves at near-lightspeed. Keeping up with the current gear can be a difficult task for anyone. This guide…
Make St. Patrick's Day explosive with a few Irish Car Bombs!