Discuss AWT components CheckBox, and CheckBoxGroup briefly.
CheckBox Class
Checkbox control is used to turn an option on(true) or off(false). There is a label for the checkbox representing what the checkbox does. The state of a checkbox can be changed by clicking on it. each
Commonly used Constructors
Checkbox();//Creates a check box with an empty string for its label. Checkbox(String label); //Creates a check box with the specified label.
Checkbox(String label, boolean state); Creates a check box with the specified label and sets the specified state.
Checkbox(String label, boolean state, CheckboxGroup group); //Constructs a Checkbox with the specified label, set to the specified state, and in the specified check box group.
Public Methods
String getLabel(); //Gets the label of this check box.
boolean getState(); //Determines whether this check box is in the on or off state.
Object[] getSelectedObjects(); //Returns an array (length 1) containing the checkbox label or null if the checkbox is not selected.
void setLabel(String label); //Sets this check box's label to be the string argument.
void setState(boolean state); //Sets the state of this check box to the specified state.
void setCheckboxGroup (CheckboxGroup g); //Sets this check box's group to the specified check box group.
OR,
Java AWT Checkbox
The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to "on".
AWT Checkbox Class Declaration
public class Checkbox extends Component implements ItemSelectable, Accessible
Java AWT Checkbox Example
In the following example, we are creating two checkboxes using the Checkbox(String label) construct and adding them into the Frame using add() method.
CheckboxExample1.java
// importing AWT class
import java.awt.*;
public class CheckboxExample1
{
// constructor to initialize
CheckboxExample1() {
// creating the frame with the title
Frame f = new Frame("Checkbox Example");
// creating the checkboxes
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java", true);
// setting location of checkbox in frame
checkbox2.setBounds(100, 150, 50, 50);
// adding checkboxes to frame
f.add(checkbox1);
f.add(checkbox2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
// main method
public static void main (String args[])
{
new CheckboxExample1();
}
}
Comments
Post a Comment