Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java:Tutorial - Making A Window


  • Please log in to reply
7 replies to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 11 January 2007 - 12:02 PM

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.
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
  • 1

#2 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 17 November 2010 - 06:17 PM

You seem to have a good handle on GUI programming in java. Do you have any good reference books that i can learn from. I have a heap of programs for which i want to make gui's for but i want something that would teach the basics and build from there. The tutorials on the oracle/java website starts of very easy then jumps into complex code without proper introductions. Probably a book for those who a very new to gui programming.
  • 0

#3 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 30 November 2010 - 05:44 PM

Good tutorial mate,

You have a great understanding of API's
  • 0

#4 midopowers

midopowers

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 10 March 2011 - 10:29 PM

One of the best reference books is Introduction to Java programming: comprehensive version/Y. Daniel Liang.--6 th ed
I haven't read it but it was recommended for me (as a newbie ) more than once :)
  • 0

#5 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 11 March 2011 - 11:10 AM

thanks.. ill see if i can grab an e-copy
  • 0

#6 dzhushev

dzhushev

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 04 April 2011 - 01:06 PM

Hey, nice tutorial. Easy and clear. I just started working with GUIs so it was very helpful.
Thanks for sharing
  • 0

#7 Xtru1990

Xtru1990

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 10 April 2011 - 06:53 PM

thank you for sharing this idea, it was really helpful!!
  • 0

#8 Panthera

Panthera

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 31 August 2011 - 03:26 PM

Thanks, looks a bit too basic i think but its great for beginners. :thumbup1:
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download