Lost Password?

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

Vote on your favorite hash algorithm here!

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-2007, 02:04 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,241
Last Blog:
Passwords
Credits: 891
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 - Make Your Button Work

This is the third of six 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. Although buttons look good, if they don’t do anything they have no meaning.

Solution
Since this tutorial assumes you know how to create a window and add a button to the window we will start off with that code.
Code:
 package cctuts;

import javax.swing.*;

public class InterfaceThree extends JFrame{
	
	public InterfaceThree(){
	   setSize(400,400);
	   JButton startButton = new JButton("Start");
	   setDefaultCloseOperation(EXIT_ON_CLOSE);
	   setVisible(true);
	}
	
	public static void main(String[] args){ 
		new InterfaceThree();
	}
}
The first thing we need to do is import another package and implement the ActionListerner class within that package to handle events. The package is java.awt.event (awt meaning abstract windowing toolkit). This looks like:
Code:
package cctuts;

import java.awt.event.*;
import javax.swing.*;

public class InterfaceThree extends JFrame implements ActionListener{
	
	public InterfaceThree(){
	   setSize(400,400);
	   JButton startButton = new JButton("Start");
	   add(startButton);
	   setDefaultCloseOperation(EXIT_ON_CLOSE);
	   setVisible(true);
	}
	
	public static void main(String[] args){ 
		new InterfaceThree();
	}
}
By implementing the ActionListener interface, our class is required to use the actionPerformed method.
Code:
 package cctuts;

import java.awt.event.*;
import javax.swing.*;

public class InterfaceThree extends JFrame implements ActionListener{
	
	public InterfaceThree(){
	   setSize(400,400);
	   JButton startButton = new JButton("Start");
	   add(startButton);
	   setDefaultCloseOperation(EXIT_ON_CLOSE);
	   setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {

	}
	
	public static void main(String[] args){ 
		new InterfaceThree();
	}
}
Now you are wondering, How does the program know if the button is pushed? Well at the moment it doesn’t. To do so, we must add an action listener to the startButton object. Like this:
Code:
 package cctuts;

import java.awt.event.*;
import javax.swing.*;

public class InterfaceThree extends JFrame implements ActionListener{
	
	public InterfaceThree(){
	   setSize(400,400);
	   JButton startButton = new JButton("Start");
	   startButton.addActionListener(this);
	   add(startButton);
	   setDefaultCloseOperation(EXIT_ON_CLOSE);
	   setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {

	}
	
	public static void main(String[] args){ 
		new InterfaceThree();
	}
}
Now anything in the actionPerformed method will be executed every time that button is pressed. So add something in there and your done! Here is my final code to demonstrate the idea:
Code:
 package cctuts;

import java.awt.event.*;
import javax.swing.*;

public class InterfaceThree extends JFrame implements ActionListener{
	
	public InterfaceThree(){
	   setSize(400,400);
	   JButton startButton = new JButton("Start");
	   startButton.addActionListener(this);
	   add(startButton);
	   setDefaultCloseOperation(EXIT_ON_CLOSE);
	   setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {
		for(int i = 0; true; i++){
			System.out.println(i);
		}
	}
	
	public static void main(String[] args){ 
		new InterfaceThree();
	}
}
Which will out put this:

Last edited by John; 01-11-2007 at 03:10 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
VB 6.0: Tutorial, How to Make Glass2K!! TcM VB Tutorials 12 09-28-2008 11:53 AM
VB 6.0: Tutorial, How to make a GIF in your application TcM VB Tutorials 17 07-01-2008 03:26 PM
Design/Php Partnership to work on a project to make income!!! powermike92 Request Services (Paid) 3 11-03-2007 04:01 PM
How to make Dumplings ahsan16 The Lounge 2 01-11-2007 10:55 PM
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM


All times are GMT -5. The time now is 08:46 PM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1075.89
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 900.18
John ........ 890.77
Brandon W ........ 770.65
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 232.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads