iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 9, 2008
Form Field Hints
EarthWeb.com
 
May 8, 2008
Meet The Hardy Heron: What's New in Ubuntu 8.04
OReilly Network
 
May 8, 2008
Does Enterprise Development Have to Be Painful? (Part Two)
OReilly Network
 
May 8, 2008
Perl Pragma Primer
WebReference.com
 
May 8, 2008
1 comment
.net
 
May 7, 2008
Poll: Do you check the download speed of the pages you build?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /Java

Java Graphical User Interfaces 101 

  Views:    6026
  Votes:    2
by Kevin Green 10/08/06 Rating: 

Synopsis:

Learn to create windows in the most basic of Java tutorials. It's simple and easy to use! Only basic Java knowledge is needed.
Pages: 
The Article

First off, all you need is a Java compiler and a good editor.

I'd recommend: pcGrasp or jCreator, as they are used in class rooms across America and stay out of your way.

Note that when you copy and paste source coding into pcGrasp, don't just use CTRL+V to paste it. Go to Edit - Paste from TrueType. Otherwise the text will have "oD" after each line, signifying a line break.

Anyways, I assume you know this about Java: what importing things does, basic data types (strings, ints), how to write methods, and how to write basic classes.

To create a GUI application you need to create 2 classes (just a good habit).


One is called a Resource Class. This class has most of the coding (in the case of GUI's) and then what's known as a Client class. The Client classes manipulates the resource class.

Alright. If you don't understand, it's no big deal for now. Just get a soda and continue...


Now, you may be wondering, how do I get a window frame on my desktop. Well you basically just answered your own question!

First, make a client class:

//Created by ___________

import java.awt.*; //make sure you import the AWT package!

public class Resource
{//begin Class

public Frame theWindow = new Frame();
//the Frame class creates windows


Resource() //what's known as a constructer method
{//begin Constructor

//Now we bend the window to our will!
theWindow.setSize(300,300);

theWindow.setTitle("My first Java window!!");

theWindow.show();

}//end Constructor



}//end Class

Alright, assuming you're past the "Hello World" phase in Java, heres what you should know:

We start off by importing java.awt.* - As it contains all we need to create a window.
(note that you can also use SWING to accomplish this task by instead importing javax.swing.* and adding a "J" infront of every class, but this is beyond the scope of this tutorial.)
import java.awt.*; 


Next we declare out class, and our class variable theWindow
public Frame theWindow = new Frame();


We have created an instance of the Frame class, which is used to draw windows and contains GUI components.

After that, the code in the constructor is self explanatory. The class methods for Frame are intuitively obvious as to what they do.


Now that we've created the Resource, compile it into Resource.class

Next is the Client class which will use the Resource class to draw the window.

The difference between a Client and Resource class is that a Client class can be run on its own. It does not need to be instantiated as an object, and it contains the program entry point known as the main method.

public class Client //Class declaration
{//begin class

public static void main(String [] arg) // the Main Method!
{//begin main

Resource me = new Resource();

//create an instance of our resource class!
//Note that the name of the class you use here
//(Resource) must be the same as the name you saved
//your Resource class as!

}//end main


}//end Class - You're done!

The only part that's of any significance is the line
Resource me = new Resource();


Where we create an instance of our Resource class. Remember that in its constructor, we told it to create a window. Now, we could've done all that in this one file as well, but as a general rule, you use the Resource class to design your GUI, add all the necessary components (a later tutorial) and then you use the Client class to run it.

All that's left is for you to compile and run Client.class
Pages: 

Similar/related articles:


 
  Sponsors