IT Community - Software Programming, Web Development and Technical Support

Java:Tutorial - Adding Buttons to your Interface

This is a discussion on Java:Tutorial - Adding Buttons to your Interface within the Java Programming forums, part of the Software Development category; Prerequisites You should have JDK installed and an editing environment you are comfortable with. Java:Tutorial - Getting Started - CodeCall Programming ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-23-2007, 10:11 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
Default Java:Tutorial - Adding Buttons to your Interface

Prerequisites
You should have JDK installed and an editing environment you are comfortable with.
Java:Tutorial - Getting Started - CodeCall Programming Forum

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();
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

vB 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 - A better looking GUI pranky Java Programming 2 03-08-2007 12:34 PM
Java:Tutorial - The Variable pranky Java Programming 0 02-24-2007 12:59 AM
Java:Tutorial - Arrays pranky Java Programming 0 02-24-2007 12:54 AM
Java:Tutorial - Getting Started pranky Java Programming 0 02-24-2007 12:48 AM
Java:Tutorial - Tic-Tac-Toe pranky Java Programming 6 02-23-2007 09:48 AM


All times are GMT -7. The time now is 11:17 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0