Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Button Pressing Example 
 

In the program code given below, the Frame contains three buttons named - "Bonjour", "Good Day", "Aurevoir".

 

Button Pressing Example

                         

In the program code given below, the Frame contains three buttons named - "Bonjour", "Good Day", "Aurevoir". The purpose of these three buttons need to be preserved within a command associated with the button. That is a different action takes place on the click of each button.

You need to run this program outside the browser because this is an application. Moreover, if you don't to check that which button was selected then in that case you need to associate a different ActionListener to each button rather than associating one to all. 

The following program demonstrates the event generated by each button.

import java.awt.*; 
import java.awt.event.*; 
 
public class ButtonPressDemo 
  public static void main(String[] args){ 
    Button b; 
    ActionListener a = new MyActionListener()
    Frame f = new Frame("Java Applet")
    f.add(b = new Button("Bonjour"), BorderLayout.NORTH)
    b.setActionCommand("Good Morning")
    b.addActionListener(a)
    f.add(b = new Button("Good Day"), BorderLayout.CENTER)
    b.addActionListener(a)
    f.add(b = new Button("Aurevoir"), BorderLayout.SOUTH)
    b.setActionCommand("Exit")
    b.addActionListener(a)
    f.pack()
    f.show()
  

 
class MyActionListener implements ActionListener 
  public void actionPerformed(ActionEvent ae) {
    String s = ae.getActionCommand()
    if (s.equals("Exit")) { 
      System.exit(0)
      
      else if (s.equals("Bonjour")) { 
        System.out.println("Good Morning")
        
        else 
          System.out.println(s + " clicked")
          
    

Output of the program:

C:\newprgrm>javac ButtonPressDemo.java

C:\newprgrm>java ButtonPressDemo
Good Morning clicked
Good Day clicked

C:\newprgrm>

Download this example.

 

 

                         

» View all related tutorials
Related Tags: c gui com ide browser class stl ui applet io methods classes method sed apple nested override vi component panel

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

This is really a fantastic site. It helps me a lot in solving my programming errors.....

Posted by Naba Deka on Monday, 12.1.08 @ 20:10pm | #82255

i would like to become java eeport
can ableto help me plz

Posted by inayath on Wednesday, 05.21.08 @ 15:22pm | #60589

please help me to make button an image icon
and when you press more than one time the icon change to another picture

Posted by asma on Monday, 02.4.08 @ 14:24pm | #47185

How to set the setBounds for the buttons.
And how to put the images on the frame .

Please give some sample programs on that.


Posted by srinivas on Monday, 11.26.07 @ 23:29pm | #40569

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.