Sunday, December 19, 2010

Creating Frames [Java Tutorial]

This tutorial was completely written by ME


||..~


The tutorial will be talking about GUI (Graphical User Interface) in java programming language. we all know that java is a OOP language. it means that we can design interfaces for the software we develop, so that the user could interact much easily with it. That's what graphical interfaces aimed to.

let's write a code to show a window, or a FRAME as we call it in Java, and see how the code will look like:

import javax.swing.*;

public class JFrameTest {

    public static void main (String [] args) {

     JFrame f = new JFrame();

    }
       
}

now we've already created our frame.
let's take it line by line:


import javax.swing.*; 
this line is concerned for calling the package (javax.swing) which contains the JFrame component. simply, the frame cannot be created without including this package.

public class JFrameTest {
creating the class and name it as JFrameTest

public static void main (String [] args) {
this line is understood perfectly. If you are already taking tuts on GUI then it's probably not needed to explain.

JFrame f = new JFrame();
Here comes creating the frame and give it the name of f .

JFrame : the class name

f : creating the object (frame)

new : key word used for creating the frame which we gave f as the name of it .

JFrame : the constructor -followed by ' () ' and a ' ; ' , which has to be named the same as the class name .

and the last two lines are the closing brackets for JFrameTest and main classes.


||..~


if you run the code, nothing will show up. Because the frame is hidden and need to be visible. in this case, we need to set some attributes to the frame, at lease to make it visible. 
so what we do?
look at the following:

import javax.swing.*;

public class JFrameTest {

    public static void main (String [] args) {
     JFrame f = new JFrame();

     f.setVisible(true);

     }
    
} 

One line was added.


f.setVisible(true);
so this is the code responsible for making the frame VISIBLE

 (the object name) f [dot] (the method) setVisible ([the boolean value] true ) in between the brackets () 
of course we don't forget the semi colon ; 



||..~


Now let's add some other features to our frame .
Here goes the code :

import javax.swing.*;

public class JFrameTest {

    public static void main (String [] args) {

     JFrame f = new JFrame("Testing the Frame");

     f.setVisible(true);

     f.setSize(300,200);

     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      }
    
}

3 more things were added. let's pick them up one by one .


First one was giving the frame a title : "Testing the frame"
we mentioned it in between the brackets in the same line of creating the frame. this looks simple. note that title has to be in between Double Quotes " "


Next thing


f.setSize(300,200);
setting the size of the frame by adding the arguments in between the brackets (WIDTH , HEIGHT);
we set 300 as the width, 200 as the height.


Last one


f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this is a very useful line. well, it is a must in every frame we create. (may be some exceptions in higher levels)

this line is used for terminating the program. if we click on the (x) button, the program will still running, even though the frame disappears , the operation will still ON.
so we should include that code to stop the program when wanted to.

 (frame name) f [dot] (method name, which is.. ) setDefaultCloseOperation (in between the brackets) we call the class name which is JFrame [dot] (the method) CLOSE_ON_EXIT  
and remember all the time, the semi colon ;

(note: java is case sensitive. keep that in mind) 


||..~


and that's all i guess. I don't know how I was doing. just hoping I covered everything well and hope my words make you get the basic idea .


*comments will be very welcome*

wish you peace, and all good stuff =)

liberty~

3 comments:

  1. just to make the tutorial easier for anyone to follow, may i suggest to include a screenshots specially the final output...

    ReplyDelete
  2. thanx
    i appreciate your suggestion.

    sorry for that but it's because that this is old tutorial i wrote months ago.

    i'll think about including screenshots in the coming and newer ones

    =)

    ReplyDelete