+ Reply to Thread
Results 1 to 7 of 7

Thread: Java:Tutorial - A better looking GUI

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

    Java:Tutorial - A better looking GUI

    This is the fourth of a few tutorials that will show you how to create graphical user interfaces using java.

    Prerequisites
    This tutorial is moderately difficult. If you know the Java basics please refer to my previous tutorials. If you do know the Java basics you should be familiar with my previous tutorials on GUI's. To read my tutorials please refer to my 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. Adding buttons to your interface allow for user interaction which is the reason for a GUI.

    Solution
    Although the idea introduced in the tutorial isn’t very big and doesn’t deserve to be its own tutorial, I changed the basic code we’ve been using so I felt it was worth while to point out a different method to create a window. It uses basic ideas I’ve explained in previous tutorials, but since this is how I will be creating most of my windows from now on, you should observe the changes and try to understand them.

    This code does the exact same thing on the outside as a pervious tutorial I made, but there are significant changes. Lets take a look at the code:
    Code:
     package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceFour implements ActionListener {
        JFrame interfaceFrame;
        JButton startButton;
        
        public InterfaceFour() {
        	interfaceFrame = new JFrame("First GUI");
        	interfaceFrame.setSize(200,70);
        	interfaceFrame.setVisible(true);
        	
    		startButton = new JButton("Start");
    		startButton.addActionListener(this);
    		interfaceFrame.add(startButton);
        
    		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		interfaceFrame.setVisible(true);
        }
    
    	public void actionPerformed(ActionEvent a) {
    		for(int i = 0; true; i++){
    			System.out.println(i);
    		}
    	}
    	
        public static void main(String[] args) {
            new InterfaceFour();
        }
    }
    As you see, rather than extending JFrame I’ve made an object called interfaceFrame. Essentially the code is exactly the same, but all the JFrame methods you want to use have to be called on the specific object, on our case interfaceFrame, and you do that by using the period. If you noticed, JFrame() accepts a string parameter where you can add the title of the application; I named it “First GUI.” Moreover I defined the object types as instance variables so they can be referenced in other parts of the code.

    The other tidbit I wanted to point out in this tutorial is you can make your program “look” better by setting the default look and feel by adding this code
    Code:
     JFrame.setDefaultLookAndFeelDecorated(true);
    This final code is important to understand because my future tutorials will be more complex and feed off this idea.

    Code:
     package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceFour implements ActionListener {
        JFrame interfaceFrame;
        JButton startButton;
        
        public InterfaceFour() {
        	JFrame.setDefaultLookAndFeelDecorated(true);
        	interfaceFrame = new JFrame("First GUI");
        	interfaceFrame.setSize(200,70);
        	interfaceFrame.setVisible(true);
        	
    		startButton = new JButton("Start");
    		startButton.addActionListener(this);
    		interfaceFrame.add(startButton);
        
    		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		interfaceFrame.setVisible(true);
        }
    
    	public void actionPerformed(ActionEvent a) {
    		for(int i = 0; true; i++){
    			System.out.println(i);
    		}
    	}
    	
        public static void main(String[] args) {
            new InterfaceFour();
        }
    }
    Image:


    Originally posted as A better looking GUI
    Last edited by John; 08-01-2010 at 10:18 AM.

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

     
  3. #2
    lakshman is offline Newbie
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Re: Java:Tutorial - A better looking GUI

    hai i am lakshman.,
    i am from india and i am a java developer.ok i observed all most all examples of swing with button.
    but i want to apply classical buttons and gui's in JFrames.(like HTML GUI's)
    pls forward sample code...
    thanking you.,

  4. #3
    Jordan Guest

    Re: Java:Tutorial - A better looking GUI

    I don't remember ever seeing this tutorial. Excellent tutorial! +rep.

  5. #4
    Stu_328 is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    92
    Rep Power
    12

    Re: Java:Tutorial - A better looking GUI

    Quote Originally Posted by lakshman View Post
    hai i am lakshman.,
    i am from india and i am a java developer.ok i observed all most all examples of swing with button.
    but i want to apply classical buttons and gui's in JFrames.(like HTML GUI's)
    pls forward sample code...
    thanking you.,
    Code:
    UIManager.setLookAndFeel(
    	    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

  6. #5
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: Java:Tutorial - A better looking GUI

    cool one .. John +rep

  7. #6
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,391
    Rep Power
    30

    Re: Java:Tutorial - A better looking GUI

    That's Great John +rep .

  8. #7
    Join Date
    Oct 2008
    Location
    Istog, Kosova
    Posts
    4,001
    Blog Entries
    1
    Rep Power
    40

    Re: Java:Tutorial - A better looking GUI

    + rock (rep)
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

+ 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. Advanced Java:Tutorial - Tic-Tac-Toe
    By John in forum Java Tutorials
    Replies: 47
    Last Post: 11-28-2011, 11:05 PM
  2. Tutorial: Java Inner Classes
    By Jordan in forum Java Tutorials
    Replies: 1
    Last Post: 02-13-2009, 10:54 PM
  3. Tutorial: Java Serialization
    By Jordan in forum Java Tutorials
    Replies: 0
    Last Post: 05-07-2008, 02:34 PM
  4. Tutorial: Java Collections
    By Jordan in forum Java Tutorials
    Replies: 0
    Last Post: 05-05-2008, 04:38 PM
  5. Java UML Tutorial
    By gszauer in forum Java Help
    Replies: 4
    Last Post: 01-30-2008, 08:55 AM

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