Lost Password?

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

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-2007, 02:10 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,655
Last Blog:
Passwords
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 of
Send a message via AIM to John
Default Java:Tutorial - Making multiple objects work differently

This is the sixth tutorial that will show you how to create graphical user interfaces using java.

Prerequisites

You should know how to create a window in Java.
LINK GOES HERE

You should know how to add a button to the window in Java.
LINK GOES HERE

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. In pervious tutorials, our two buttons did the same thing, but real applications have different buttons that perform different functions.

Solution
Lets start with a shell we’ve formulated in previous tutorials:
Code:
 package cctuts;

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

public class InterfaceSix implements ActionListener{
	
JFrame interfaceFrame;
JButton startButton, stopButton;
	
	public InterfaceSix(){
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	interfaceFrame = new JFrame("First GUI");
    	interfaceFrame.setSize(200,70);
    	interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
    	
		startButton = new JButton("Start");
		startButton.addActionListener(this);
		interfaceFrame.add(startButton);
		
		stopButton = new JButton("Stop");
		stopButton.addActionListener(this);
		interfaceFrame.add(stopButton);
    
		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		interfaceFrame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {

	}
	
	public static void main(String[] args){ 
		new InterfaceSix();
	}
}
Now just to recap what we have here: a class that makes a visible window 200px by 70px. The window looks pretty because we set the default look and feel. The window contains two button objects which are positioned using a grid layout. Each button as an action listener, so when the button is pressed the actionPerformed method is invoked. If anything I just said doesn’t make sense, you should read my previous tutorials. In the actionPerformed method we need to determine which button is pressed so we can have our program act accordingly. We do this by using the a.getSource() method, which tells us which button is being pressed. We can then perform simple logical operations to tell our program what to do.

For example if the startButton is pressed, a.getSource() == startButton. We then can say something like:
Code:
if(a.getSource() == startButton){
System.out.println(“The start button was pushed…”);
Incorporated into a full class it would look something like this:
Code:
 package cctuts;

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

public class InterfaceSix implements ActionListener{
	

	JFrame interfaceFrame;
    JButton startButton, stopButton;
	
	public InterfaceSix(){
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	interfaceFrame = new JFrame("First GUI");
    	interfaceFrame.setSize(200,70);
    	interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
    	
		startButton = new JButton("Start");
		startButton.addActionListener(this);
		interfaceFrame.add(startButton);
		
		stopButton = new JButton("Stop");
		stopButton.addActionListener(this);
		interfaceFrame.add(stopButton);
    
		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		interfaceFrame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {
		if(a.getSource() == startButton){
			System.out.println("Start button was pressed...");
		}
		
		if(a.getSource() == stopButton){
			System.out.println("Stop button was pressed...");
		}
	}
	
	public static void main(String[] args){ 
		new InterfaceSix();
	}
}
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 - Adding more objects to your window John Java Tutorials 0 01-11-2007 02:09 PM
Java:Tutorial - Make Your Button Work John Java Tutorials 0 01-11-2007 02:04 PM
Java:Tutorial - Making A Window John Java Tutorials 0 01-11-2007 02:02 PM


All times are GMT -5. The time now is 08:37 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 66%

Ads