Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

creating Frames [ Java Tutorial ]

 
webmaster forum
Activity
0%
 
Regular Coder
Gender: Female
Posts: 85
Topics: 15
April 26, 2010, 12:31:55 PM



Technical talk .net

Hello, Technicals !
hope you all doing well ^^

well, I'm still a very beginner and not sure whether i shall post this tutorial, and also not sure if this is going to go straight to your minds, but i'll try my best and i'll tell all what i know.
so please, read. i'll be appreciated if someone got the idea  shiny . leave a comment when feel like ^^

let's just start ..

So,

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 could 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. let's see how the code would look like :

Code:
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.
i'm sure that if you are learning GUIs , you would know what that means as we all learned in previous levels .


public static void main (String [] args) {

this line also is understood perfectly. no need to talk a lot here.


JFrame f = new JFrame();

Here comes creating the frame and give it the name of f .

(the first) 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 .
(the last) JFrame : the constructor , which has to be named the same as the class name .


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

[copy and paste the code in your IDE to see the result]
nothing showed up, right? you would think that there might be an error in the code, but NO. when running the program , it will stop automatically after few seconds, why ?
actually, the frame was definitely created , but HIDDEN . so the program stopped.
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 :

Code:
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 try adding some other features to our frame .
Here goes the code :

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);
    }
    
}

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

First one was giving the frame a title : "Testing the frame"
we wrote it in between the brackets in the same line of creating the frame. this looks simple, doesn't it ? 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 , but the operation will still ON.
so we should include that code to stop the program when wanted.

it goes like this :
(frame name) f [dot] (method name, which is.. ) setDefaultCloseOperation (in between the brackets) we call the class name which is JFrame [dot] (the method, which is.. ) CLOSE_ON_EXIT (note: java is case sensitive so write it exactly like i wrote)
anyhow , you don't need the explanation of it right now .


WELL, that's all i guess. don't know how i was doing. just hoping i covered everything well.
i need your comments.
Experts, how was the tutorial ? is there anything missing ? do you have any suggestions ? any corrections ? i'll be appreciated.

Beginners, hope my words make you get the basic Idea .

Many Thanks to everyone .

Smiley



You can find more information here :-



*** Please do not forget to leave your comments about this article ***

« Last Edit: May 29, 2010, 04:29:08 AM by ice_spring »

I'm trying to express myself Smiley

my blog: http://www.libertydash.blogspot.com/

smashwords: http://www.smashwords.com/books/view/91236
 
webmaster forum
jesila  Offline
Activity
0%
 
New Coder
Posts: 29
Topics: 0
May 12, 2010, 11:43:44 AM

Hi,
I am new in this forum and I have no idea about it.
« Last Edit: December 03, 2011, 10:16:12 AM by Admin »
 
webmaster forum
Activity
0%
 
Regular Coder
Gender: Female
Posts: 85
Topics: 15
May 12, 2010, 01:47:56 PM

start from the beginning then Smiley

I'm trying to express myself Smiley

my blog: http://www.libertydash.blogspot.com/

smashwords: http://www.smashwords.com/books/view/91236
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
May 12, 2010, 02:48:55 PM

Good introduction! I have always thought that Swing programming is very tedious - the tutorials on Sun's website http://java.sun.com/docs/books/tutorial/ui/index.html are very good if anyone is interested.

I actually have a number of "base" projects (somewhere) that I wrote ages ago that I can rip stuff out of and put stuff into to get started with a new UI. It has been a while since I used Swing though....

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Corrinla  Offline
Activity
0%
 
New Coder
Posts: 28
Topics: 0
December 07, 2010, 06:25:35 PM

So difficult for me to understand, but i will try.  thanks all the same!

pandora jewelry | wholesale
 
webmaster forum
Activity
0%
 
Regular Coder
Gender: Female
Posts: 85
Topics: 15
December 16, 2010, 08:54:28 AM

you're welcome corrinla Smiley
just keep practicing

I'm trying to express myself Smiley

my blog: http://www.libertydash.blogspot.com/

smashwords: http://www.smashwords.com/books/view/91236
 
webmaster forum
Corrinla  Offline
Activity
0%
 
New Coder
Posts: 28
Topics: 0
December 20, 2010, 12:30:52 AM

WELL, that's all i guess. don't know how i was doing. just hoping i covered everything well.

pandora jewelry | wholesale
 
webmaster forum
Activity
0%
 
Regular Coder
Gender: Female
Posts: 85
Topics: 15
December 20, 2010, 07:08:33 AM

yea exactly

I'm trying to express myself Smiley

my blog: http://www.libertydash.blogspot.com/

smashwords: http://www.smashwords.com/books/view/91236
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 02, 2012, 07:02:38 PM