Home : Course Map : Chapter 7 : Java : Supplements :
A Button with AWT
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The applet below demonstrates the basics of using the Button component, and also a Label, from the basic AWT framework.

The Applet instance implements the ActionListener interface so it must provide an actionPerformed() method. The Button and Label instances are added to the Applet, which inherits the Panel class and its capability to hold other components. The default FlowLayout will arrange the components sequentially left to right horizontally and top to bottom vertically within the available space.

The button adds the Applet instance to its list of ActionListeners and ActionEvent objects are sent to it via calls to the actionPerformed() method.

ButtonApplet.java

 

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/** This program demonstrates the Button and Label
  * components in the basic AWT framework.
 **/
public class ButtonApplet extends Applet
                       implements ActionListener
{
  Label fLabel;
  Button fButton;
  int num_pushes;

  /** Create the interface with a button. **/
  public void init () {
    num_pushes = 0;

    add (fLabel = new Label ("Num Pushes = 0"));
    fLabel.setBackground (Color.blue);
    fLabel.setForeground (Color.red);

    // Create a Button and add it to the Applet's panel.
    add (fButton = new Button ("Push") );

    // Add this object to the buttons ActionListener list.
    fButton.addActionListener ( this );

    setBackground (Color.blue);
  } // init

  public void actionPerformed  (ActionEvent e) {
     num_pushes++;
     fLabel.setText ("Num Pushes = " + num_pushes);
  } // actionPerformed

} // class ButtonApplet

 

Latest update: Nov. 4, 2004

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.