Page 214 - CTS - CSA TP - Volume 2
P. 214
COMPUTER SOFTWARE APPLICATION - CITS
selectedOptions.append(“Option 1 “);
}
// Check if checkBox2 is selected and append the corresponding text
if (checkBox2.isSelected()) {
selectedOptions.append(“Option 2 “);
}
// Display a JOptionPane with the selected options
JOptionPane.showMessageDialog(frame, selectedOptions.toString());
}
});
// Set the layout manager for the frame to BoxLayout along the Y-axis
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
// Add the checkboxes and submit button to the frame
frame.getContentPane().add(checkBox1);
frame.getContentPane().add(checkBox2);
frame.getContentPane().add(submitButton);
// Set the size, default close operation, and make the frame visible
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation:
1 Imports: Import necessary classes from the javax.swing and java.awt.event packages for GUI components
and event handling.
2 Class Declaration: Declare a class named CheckBoxControlExample.
3 Main Method: The main method is the entry point for the application.
4 JFrame Initialization: Create a JFrame object named frame with the title “CheckBox Example.”
5 Checkboxes and Button Initialization: Create two JCheckBox components (checkBox1 and checkBox2) and a
JButton (submitButton).
6 ActionListener: Add an ActionListener to the submitButton. Inside the actionPerformed method, check the
selected state of each checkbox and build a message accordingly.
7 Layout Manager: Set the layout manager for the frame to BoxLayout along the Y-axis.
8 Component Addition: Add the checkboxes and the submit button to the frame’s content pane.
9 Frame Configuration: Set the frame’s size, default close operation, and make it visible.
199
CITS : IT & ITES - Computer Software Application - Exercise 117