IT Community - Software Programming, Web Development and Technical Support

Java:Tutorial - Making multiple objects work differently

This is a discussion on Java:Tutorial - Making multiple objects work differently within the Java Programming forums, part of the Software Development category; Prerequisites You should know how to create a window in Java. LINK GOES HERE You should know how to add ...


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

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 02-23-2007, 09:03 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
Default Java:Tutorial - Making multiple objects work differently

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.EXI T_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.EXI T_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();
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 10-04-2008, 10:35 PM
danica danica is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 12
danica is on a distinguished road
Default Re: Java:Tutorial - Making multiple objects work differently

Java is a programming language originally developed by Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities.

_______________________

Keyword Research Georgia Health Insurance
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 11-25-2008, 01:57 AM
shikha shikha is offline
D-Web Trainee
 
Join Date: Nov 2008
Posts: 3
shikha is on a distinguished road
Default Re: Java:Tutorial - Making multiple objects work differently

Hello friends

Thanks for sharing such knowledgeable things. But please tell me what is the difference between java programming and core java..
Please reply me if anyone knows


Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 12-13-2008, 08:30 AM
SAMI 89 SAMI 89 is offline
D-Web Trainee
 
Join Date: Dec 2008
Posts: 2
SAMI 89 is on a distinguished road
Default Get free shipping when shopping for Ed Hardy products

Get a huge variety of Ed Hardy clothing, NIKE, Adidas, Christian Audigier and a lot more brands at a discounted price and with worldwide free shipping. So visit Ed Hardy Clothing|Ed Hardy Hats|Hip Hop|Christian Audigier|Timberland Boots - Raining Hollywood and start ordering today.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
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 Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Tutorial - A better looking GUI pranky Java Programming 2 03-08-2007 11:34 AM
Java:Tutorial - The Variable pranky Java Programming 0 02-23-2007 11:59 PM
Java:Tutorial - Arrays pranky Java Programming 0 02-23-2007 11:54 PM
Java:Tutorial - Make Your Button Work pranky Java Programming 0 02-23-2007 09:06 AM
Java:Tutorial - Tic-Tac-Toe pranky Java Programming 6 02-23-2007 08:48 AM


All times are GMT -7. The time now is 02:17 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0