Why does nothing happen when you click the button?

A good answer might be:

There is no listener for the Button's events. If there is no listener for a particular type of event, then the program will simply ignore events of that type.

How to add Buttons

There is a listener for the frame's "close button," but not for the new Button. You can click the Button, and generate an event, but no listener receives the event.

public class buttonDemo extends Frame
{
  // construct a Button
  Button bChange = new Button("Click Me!"); 

  // constructor for buttonDemo
  buttonDemo()          
  {
    // add the button to the container
    add( bChange );     
  }

  public static void main ( String[] args )
  {
    buttonDemo frm = new buttonDemo();
      . . . . .
  }
}
  • The program starts running with main().
  • main() asks the system to construct a buttonDemo object.
  • The buttonDemo class is a container class, so a buttonDemo object can contain a Button.
  • A Button is constructed using new with a constructor, as with any class.
    • The words on the button will be "Click Me!"
    • The variable bChange refers to the Button.
  • The buttonDemo constructor uses add( bChange ) to add the Button to the Frame.
  • The default layout is used in adding the button to the frame.
    • The default is one big button in the center.
  • The buttonDemo class does not have its own paint() method because everything to be painted is a component.
  • The system will automatically paint all components in a container when it needs to.
  • If special processing is needed, such as drawString() calls, then you will need to override paint().

The program in the previous chapter does not define a constructor because it inherits the constructor from its parent class (Frame), which is enough to do the job. When you add components to a container, you will have to define a constructor for the container class.

QUESTION 3:

First let us look at the problem of the button that fills the frame. What size to you think would be better for the button? Where do you think the button should go?

Click Here after you have answered the question