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

COMPUTER SOFTWARE APPLICATION - CITS


           EXERCISE 117 : Create a container with a few controls


            Objectives

           At the end of this exercise you shall be able to
           •  develop Java programs to Create a container with a few controls

           Requirements

           Tools/Materials

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


           Procedure


           TASK 1: Create a JCheckBox for Multiple Selection
           Code:
           import javax.swing.*;

           import java.awt.event.ActionEvent;
           import java.awt.event.ActionListener;
           public class CheckBoxControlExample {
               public static void main(String[] args) {
                   // Create a JFrame (window) with the title “CheckBox Example”

                   JFrame frame = new JFrame(“CheckBox Example”);


                   // Create two JCheckBox components with labels “Option 1” and “Option 2”
                   JCheckBox checkBox1 = new JCheckBox(“Option 1”);

                   JCheckBox checkBox2 = new JCheckBox(“Option 2”);


                   // Create a JButton with the label “Submit”
                   JButton submitButton = new JButton(“Submit”);


                   // Add an ActionListener to the submitButton

                   submitButton.addActionListener(new ActionListener() {
                       @Override
                       public void actionPerformed(ActionEvent e) {
                           // Create a StringBuilder to build the message

                           StringBuilder selectedOptions = new StringBuilder(“Selected Options: “);


                           // Check if checkBox1 is selected and append the corresponding text
                           if (checkBox1.isSelected()) {







                                                           198

                                                                                                                                                     CITS : IT & ITES - Computer Software Application - Exercise 117
   208   209   210   211   212   213   214   215   216   217   218