+ Reply to Thread
Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By John

Thread: Java:Tutorial - Making A Window

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    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 01:07 PM.
    Panthera likes this.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: Java:Tutorial - Making A Window

    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.
    Perfection of means and confusion of ends seem to characterize our age. Albert Einstein

  4. #3
    Jarryd's Avatar
    Jarryd is offline Learning Programmer
    Join Date
    Sep 2010
    Location
    Queensland, Australia
    Posts
    63
    Rep Power
    6

    Re: Java:Tutorial - Making A Window

    Good tutorial mate,

    You have a great understanding of API's

  5. #4
    midopowers is offline Newbie
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Re: Java:Tutorial - Making A Window

    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

  6. #5
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: Java:Tutorial - Making A Window

    thanks.. ill see if i can grab an e-copy
    Perfection of means and confusion of ends seem to characterize our age. Albert Einstein

  7. #6
    dzhushev is offline Newbie
    Join Date
    Apr 2011
    Location
    Aarhus, Denmark
    Posts
    1
    Rep Power
    0

    Re: Java:Tutorial - Making A Window

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

  8. #7
    Xtru1990 is offline Newbie
    Join Date
    Apr 2011
    Location
    kathmandu
    Posts
    1
    Rep Power
    0

    Re: Java:Tutorial - Making A Window

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

  9. #8
    Panthera is offline Newbie
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Re: Java:Tutorial - Making A Window

    Thanks, looks a bit too basic i think but its great for beginners.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VB 6.0: Tutorial, Making a Port Scanner
    By TcM in forum Visual Basic Tutorials
    Replies: 122
    Last Post: 01-21-2012, 02:19 PM
  2. Replies: 3
    Last Post: 06-19-2010, 06:00 AM
  3. Help me how to making a page load itself into a new popup window ???
    By love4all1080 in forum JavaScript and CSS
    Replies: 3
    Last Post: 03-18-2009, 03:56 AM
  4. java applet window size??
    By stack in forum Java Help
    Replies: 5
    Last Post: 07-24-2007, 07:00 AM
  5. Java:Tutorial - Adding more objects to your window
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts