Page 219 - CTS - CSA TP - Volume 2
P. 219

COMPUTER SOFTWARE APPLICATION - CITS


           EXERCISE 118 : Create a container with controls with action

                                       listeners and event handlers


            Objectives

           At the end of this exercise you shall be able to
           •  Develop Java programs to Create a container with controls with action listeners and event handlers.

           Requirements

           Tools/Materials
           •  PC/Laptop  with Windows OS
           •  JDK Software
           •  Text Editor (Visual Studio/Sublime/Notepad)

           Procedure

           TASK 1:  Creating a container with controls (CheckBox) using AWT in Java, along with action listeners
                    and event handlers

           import java.awt.*;
           import java.awt.event.ActionEvent;
           import java.awt.event.ActionListener;


           public class CheckboxExample {



               public static void main(String[] args) {
                   Frame frame = new Frame(“Checkbox Example”);
                   Checkbox checkbox = new Checkbox(“Check me”);



                   checkbox.addItemListener(e -> {
                       System.out.println(“Checkbox state: “ + (checkbox.getState() ? “Checked” : “Unchecked”));
                   });


                   frame.add(checkbox);

                   frame.setSize(300, 150);
                   frame.setLayout(new FlowLayout());
                   frame.setVisible(true);
           frame.addWindowListener(new java.awt.event.WindowAdapter() {

                   public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                           System.exit(0);
                       }
                   });
               }
           }



                                                           204

                                                                                                                                                     CITS : IT & ITES - Computer Software Application - Exercise 118
   214   215   216   217   218   219   220   221   222   223   224