+ Reply to Thread
Results 1 to 1 of 1

Thread: Java:Tutorial - Adding Buttons to your Interface

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

    Java:Tutorial - Adding Buttons to your Interface

    This is the second 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 also have read my previous tutorials:
    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
    Since you already know how to create a window we are going to start off where we left off last time with the window code.
    Code:
     package cctuts;
    
    import javax.swing.*;
    
    public class InterfaceTwo extends JFrame{
    	
    	public InterfaceTwo(){
    	   setSize(400,400);
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceTwo();
    	}
    }
    To add a button we add an “object” to the window. To do that we must define the object type, object name and instantiate the object (discussed in previous tutorials). In this case our button type is JButton, you can name the object what ever you want. I’ve decided to call it startButton. The JButton accepts a string paramenter and when you create the object in java the syntax of the line would look like this:
    Code:
     JButton startButton = new JButton("Start");
    Which is a simple button that says “Start.” Althought we created the object, we need to add the object to the window, and the JFrame class allows us to use the add() method to do just that. add() accepts an object parameter and would look like this:
    Code:
     add(startButton);
    The final code looks like this:
    Code:
     package cctuts;
    
    import javax.swing.*;
    
    public class InterfaceTwo extends JFrame{
    	
    	public InterfaceTwo(){
    	   setSize(400,400);
    	   JButton startButton = new JButton("Start");
    	   add(startButton);
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceTwo();
    	}
    }
    Visually this is that it looks like:


    Originally posted as Adding Buttons to your Interface
    Last edited by John; 08-01-2010 at 10:30 AM.

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

     
+ 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. Q: adding buttons during runtime?
    By encio in forum Visual Basic Programming
    Replies: 4
    Last Post: 06-22-2010, 06:32 PM
  2. Replies: 6
    Last Post: 07-20-2009, 03:27 PM
  3. Tutorial - OptionButtons + Command Buttons!
    By travy92 in forum Visual Basic Tutorials
    Replies: 4
    Last Post: 07-07-2009, 11:04 AM
  4. Interface, command buttons, menus.
    By ld_pvl in forum Java Help
    Replies: 3
    Last Post: 10-26-2008, 05:07 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