Checkboxes in Java Applet

A Check box is a AWT control that is used to turn an option on(true) or off(false). It consists of a small box that is either selected or unselected based on whether the Boolean argument is true or false.

 

 

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class ActionExample1 extends Applet implements ActionListener

{

     Button okButton;

     Button wrongButton;

     TextField nameField;

     CheckboxGroup radioGroup;

     Checkbox radioo1;

     Checkbox radioo2;

     Checkbox radioo3;

     public void init()

     {

         setLayout(new FlowLayout());

         okButton = new Button("Action!");

         wrongButton = new Button("Don't click!");

         nameField = new TextField("Type here Something",35);

         radioGroup = new CheckboxGroup();

         radioo1 = new Checkbox("Redd", radioGroup,false);

         radioo2 = new Checkbox("Bluee", radioGroup,true);

         radioo3 = new Checkbox("Greeen", radioGroup,false);

         add(okButton);

         add(wrongButton);

         add(nameField);

         add(radioo1);

         add(radioo2);

         add(radioo3);

         okButton.addActionListener(this); //actions to the components

         wrongButton.addActionListener(this);

         }

         public void paint(Graphics graphh)

         {

         if (radioo1.getState()) graphh.setColor(Color.red);

        else if (radioo2.getState()) graphh.setColor(Color.blue);

        else graphh.setColor(Color.green);

        graphh.drawString(nameField.getText(),20,100);

     }

       public void actionPerformed(ActionEvent aevt)

         {

             if (aevt.getSource() == okButton)

            repaint(); /*perform his action(paint again) ,this will cause the applet to get the text out of  the textField again*/

         else if (aevt.getSource() == wrongButton) //if we click on this button then

         {

               wrongButton.setLabel("Not here!"); //this change the text in the textfield

               nameField.setText("That was the wrong button!");

               repaint(); //applet show the message

         }

     }

}

//<applet code="ActionExample1.class" height=500 width=500></applet>

Applet with CheckBox

Explanation….

In above example a button is clicked to get and place text in or out a textfield and to get the state of checkboxes.

In above example we use the FlowLayout.

If the radioo1 box is selected then radioo1.getState() will

return true and this will execute

         if (radioo1.getState()) graph.setColor(Color.red);

The color is set that we can get the text out the TextField

 

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @