Button Component
The Button component of Java's AWT is a workhorse of any GUI. The only decision you need to make when you create one is whether or not you want it to have a label. The syntax is as follows
Button buttonname = new Button(button label);
Where 'buttonname' is the name you want to give the object and 'button label' is the text you want to appear on the button. Once you create the button you add it to the applet (or other container) by entering the command
add(buttonname);
We can change the button's label or get the label's text using the Button.setLabel or Button.getLabel methods. Now that we can create the buttons and manipulate their labels we can investigate the code that makes the button perform the operations we need.
The Applet below changes the background color by simply clicking on one of the labeled buttons.
| The update method will fill the
background by drawing a filled rectangle with the color named in the button. Notice the buttons themselves remain gray. There is no way to change that color at this point. |
The code for the Applet is listed below
import java.applet.*;
import java.awt.*;
public class button extends Applet
{
public void init()
{
add(new Button("Red"));
add(new Button("White"));
add(new Button("Blue"));
add(new Label("Hit a Button to
change the screen color");
}
|
We import all the facilities of the AWT and applet that Java has to offer by using the * wildcard. We create an applet. And initialize it by adding three new buttons labeled Red, White and Blue. and place a label to prompt the user to hit a button. |
public boolean action(Event evt, Object whatAction)
{
if (!(evt.target instanceof Button))
{
return false;
}
String buttonLabel = (String) whatAction;
if (buttonLabel=="Red")
setBackground(Color.red);
if (buttonLabel=="White")
setBackground(Color.white);
if (buttonLabel=="Blue")
setBackground(Color.blue);
repaint();
return true;
}
}
|
When an action takes place the default handleEvent method calls the action method of our component. If its not a button event we simply return Otherwise check the button's label to determine which button was pressed. Then set the background to the appropriate color and repaint and update the screen. We return true to indicate the event we wanted was handled. |
The easiest way to obtain the code for this Applet is to click here and copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "button.java".
After compiling this program we need to create an HTML page with a reference to the class file. It will require a graphic window that is large enough to contain the buttons and the label. The update method will determine the screen size you set up for the applet and change the background for whatever size you like.
Save the HTML code in the same directory as the class file.
When you load the HTML file in your browser the applet will execute as below.
View of the applet's output
The buttons themselves and the label do not change color when you press them. There is no way to change the color or to place any image on a button. If you want a picture button you will have to implement the animations and events yourself with this version of Java. Try hitting the 'BACK' button on your browser and then return to this screen. Notice the label background will change but the buttons will not. Labels that are placed on the screen after the background color is set will take on that color as their background whereas buttons will always be gray.
Back to the Java AWT Components Page