Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-2007, 02:03 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,237
Last Blog:
Passwords
Credits: 877
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default 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 cl*** 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 cl*** 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 cl*** 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:

Last edited by John; 01-11-2007 at 03:08 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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 - Tic-Tac-Toe John Java Tutorials 25 02-27-2008 06:41 PM
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM
Java:Tutorial - Adding more objects to your window John Java Tutorials 0 01-11-2007 02:09 PM
Java:Tutorial - A better looking GUI John Java Tutorials 0 01-11-2007 02:07 PM
Java:Tutorial - Make Your Button Work John Java Tutorials 0 01-11-2007 02:04 PM


All times are GMT -5. The time now is 02:49 AM.

Contest Stats

Xav ........ 1322.18
MeTh0Dz|Reb0rn ........ 1053.7
morefood2001 ........ 879.43
John ........ 877.37
marwex89 ........ 869.98
WingedPanther ........ 830.24
Brandon W ........ 735.07
chili5 ........ 309.39
Steve.L ........ 239.84
dcs ........ 216.02

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads