Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-2007, 02:02 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,238
Last Blog:
Passwords
Credits: 887
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default 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 my 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.
Code:
 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.

Code:
 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.

Code:
 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()

Code:
 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);

Code:
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.

Code:
 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:

Last edited by John; 01-11-2007 at 03:07 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Tutorial - Tic-Tac-Toe John Java Tutorials 25 02-27-2008 06:41 PM
New window with custom content - JS domestic JavaScript and CSS 10 06-04-2007 02:08 AM
! Need urgent help ! Drawing program using cygwin siren C and C++ 0 05-26-2007 10:51 PM
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM
Java:Tutorial - Adding more objects to your window John Java Tutorials 0 01-11-2007 02:09 PM


All times are GMT -5. The time now is 12:40 AM.

Contest Stats

Xav ........ 1333.07
MeTh0Dz|Reb0rn ........ 1059.52
John ........ 887.37
morefood2001 ........ 879.43
marwex89 ........ 869.98
WingedPanther ........ 851.68
Brandon W ........ 764.23
chili5 ........ 312.39
Steve.L ........ 254.16
dcs ........ 223.87

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads