Untitled-4

iphone apps

Essential iPhone Apps

We run down the best apps in each category. Get the most out of your iPhone today!

Read More

New-Chrome-Icon

Google Chrome - The Official Browser of Gounce.com

Do yourself a favor and check out this fantastic browser from our friends at Google.

Read More

smartdjvsgenius

Itunes' Genius Vs. Zune's Smart DJ

Too lazy to make playlists? Try these automated tools then kick back and enjoy!

Read More

running

Best Songs To Run To 2011

These songs, sorted by genre, will keep you headed in the right direction.

Read More

prezivsppt

Prezi vs. Microsoft PowerPoint

Deliver astounding presentations from anywhere, by harnessing Prezi's state of the art cloud technology.

Read More

Thursday, 16 June 2011 21:56

Introduction to Frames Using Java (Part 2)

Java

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 to take the next step and create a somewhat decent/applicable program using frames. A few new concepts will be introduced in this tutorial, and it should give a better glimpse at how frames work and how powerful they could be. Keep in mind though, that this is still only the basics. 

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.


Like our style? Subscribe and we'll keep you in the loop!

* indicates required

Last modified on Sunday, 26 February 2012 00:07
Rev

Rev

Interests: Computers, Programming, Hockey, Football, Music

Hobbies: Guitar, Drums, Programming, Hockey

Favorite Movie: Memento

Favorite Animated Movie: Toy Story

Favorite Album: Fear of a Blank Planet - Porcupine Tree

Gear: Macbook 6.1

Instruments: PRS Angelus Acoustic, PRS Custom 24 Electric, Pearl Export Drums.

Website: www.gounce.com Contact Rev
blog comments powered by Disqus

Follow Us

GoogleBuzz     delicious youtube

Recent Articles

Step By Step Links