java:Tutorial - Making A Window

This is the first of six tutorials that will show you how to create graphical user interfaces using java.

Prerequisites
You should have JDK installed and an editing environment you are comfortable with.
http://forum.codecall.net/java-tutorials/1703-java-tutorial-getting-started.html



You should know how to create classes within your IDE
http://forum.codecall.net/java-tutorials/1706-java-tutorial-hello-world.html

Also for any questions please refer to  tutorial index:
INDEX

The Idea
In order for your program to be attractive, the user must be able to easily navigate through your program. By creating a GUI the user is presented with all the features of the program in a clear and coherent manner.

Solution
The first thing we are going to do is create our class with a constructor and a main method to start our application.
 package cctuts;
public class InterfaceOne {
       
       
public InterfaceOne(){

       
}
       
       
public static void main(String[] args){
               
new InterfaceOne();
       
}
}

Next we are going to import the java.swing package so we are able to use the JFrame class.

 package cctuts;
import javax.swing.*;
public class InterfaceOne {
       
       
public InterfaceOne(){

       
}
       
       
public static void main(String[] args){
               
new InterfaceOne();
       
}
}

Next we are going to extend JFrame so we inherit the capabilities of the parent class.

 package cctuts;
import javax.swing.*;
public class InterfaceOne extends JFrame{
       
       
public InterfaceOne(){

       
}
       
       
public static void main(String[] args){
               
new InterfaceOne();
       
}
}

Now in the constructor we are going to define the size of the window by using setSize()

 package cctuts;
import javax.swing.*;
public class InterfaceOne extends JFrame{
       
       
public InterfaceOne(){
           setSize
(400,400);
       
}
       
       
public static void main(String[] args){
               
new InterfaceOne();
       
}
}

It is also important to set the default close operation, meaning what the program does when you exit. If you don’t set the default close operation, when you exit your application, although it will close from your task bar, it will still be running in the background. To do that we add setDefaultCloseOperation(EXIT_ON_CLOSE);

package cctuts;
import javax.swing.*;
public class InterfaceOne extends JFrame{
       
       
public InterfaceOne(){
           setSize
(400,400);
           setDefaultCloseOperation
(EXIT_ON_CLOSE);
       
}
       
       
public static void main(String[] args){
               
new InterfaceOne();
       
}
}

Finally if you run the following code, although there are no errors you wont see a window. Although a windows has been created in the memory it is not visible, and to do that you add setVisible(true); and your final code looks like this.

 package cctuts;
import javax.swing.*;
public class InterfaceOne extends JFrame{
       
       
public InterfaceOne(){
           setSize
(400,400);
           setDefaultCloseOperation
(EXIT_ON_CLOSE);
           setVisible
(true);
       
}
       
       
public static void main(String[] args){
               
new InterfaceOne();
       
}
}

Although it’s not much here is what your window looks like:
Posted Image 

No comments:

Post a Comment