Guide to Buttons in Java Applets: Types and Examples

Classified in Computers

Written on in English with a size of 4.39 KB

Buttons in Java Applets

Introduction

Buttons are essential UI elements in Java applets, allowing user interaction and triggering specific actions. This guide explores various button types and their implementation.

Basic Buttons

Buttons can be created using the Button class. They are displayed on the screen and trigger events when clicked. Each button has an identifier used for event handling.

Example:

Button button;button = new Button("Button");

Button Events

When a user clicks a button, an event is generated. These events can be captured using the action() method:

public boolean action(Event evt, Object obj) {    if (evt.target instanceof Button) {        System.out.println((String) obj);    } else {        System.out.println("Event No-Button");    }}

To distinguish between multiple buttons, compare the target object of the Event with the button objects in your interface:

import java.net.*;import java.applet.Applet;public class Button extends Applet {    Button b1, b2, b3;    public void init() {        b1 = new Button("Button B1");        b2 = new Button("Button B2");        b3 = new Button("Button B3");        this.add(b1);        this.add(b2);        this.add(b3);    }    public boolean action(Event evt, Object obj) {        if (evt.target.equals(b1))            System.out.println("You pressed the button B1");        if (evt.target.equals(b2))            System.out.println("button was pressed B2");        if (evt.target.equals(b3))            System.out.println("button was pressed B3");        return true;    }}

Choice Buttons (Dropdown Lists)

Choice buttons provide a dropdown list of options, allowing users to select one. This is useful for scenarios like color selection:

import java.net.*;import java.applet.Applet;public class BotonSeleccion extends Applet {    Choice Selector;    public void init() {        Selector = new Choice();        Selector.addItem("Red");        Selector.addItem("Green");        Selector.addItem("Blue");        add(Selector);    }    public boolean action(Event evt, Object obj) {        if (evt.target instanceof Choice) {            String color = (String) obj;            System.out.println("The selected color is" + color);        }        return true;    }}

Checkbox Buttons

Checkbox buttons represent a true/false or yes/no state. The Checkbox class is used to create them. The state is reflected in the obj argument of the action() method:

import java.net.*;import java.applet.Applet;public class BotonComprobacion extends Applet {    Checkbox Fill;    public void init() {        Fill = new Checkbox("Fill");        add(Fill);    }    public boolean action(Event evt, Object obj) {        if (evt.target instanceof Checkbox)            System.out.println("CheckBox" + evt.arg.toString());        return true;    }}

Radio Buttons

Radio buttons allow selecting only one option from a group. The CheckboxGroup class is used to group them:

import java.net.*;import java.applet.Applet;public class BotonRadio extends Applet {    CheckboxGroup Radio;    public void init() {        Radio = new CheckboxGroup();        add(new Checkbox("First", Radio, true));        add(new Checkbox("Second", Radio, false));        add(new Checkbox("Third Party", Radio, false));    }}

Self-Contained Buttons

Java's object-oriented nature allows creating self-contained buttons with built-in event handlers. This simplifies adding custom buttons to applications:

import java.net.*;import java.applet.Applet;class OKButton extends Button {    public OKButton() {        setLabel("OK");    }    public boolean action(Event evt, Object obj) {        System.out.println("OK Button");        return true;    }}public class BotonAuto extends Applet {    OKButton button;    public void init() {        button = new OKButton();        add(button);    }}

Conclusion

This guide covered various button types in Java applets, including basic buttons, choice buttons, checkboxes, radio buttons, and self-contained buttons. Understanding these types and their event handling mechanisms is crucial for building interactive and user-friendly Java applets.

Related entries: