Experiment No.
16
GUI application with Event Handling
Name:chaudhary irshad ali
Roll No:7 Year:SE
Div:A Batch:A1
Aim: - WAP to change background color as Red, Green, Blue using 3 toggle Buttons
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class ColorToggleButtons extends JFrame implements ActionListener {
private JPanel panel;
private JToggleButton redButton;
private JToggleButton greenButton;
private JToggleButton blueButton;
public ColorToggleButtons() {
// Set up the frame
setTitle("Toggle Color Background");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Create a panel for color change
panel = new JPanel();
[Link]([Link]); // Initial background color
add(panel, [Link]);
// Create toggle buttons
redButton = new JToggleButton("Red");
greenButton = new JToggleButton("Green");
blueButton = new JToggleButton("Blue");
// Add action listeners
[Link](this);
[Link](this);
[Link](this);
// Create a panel for buttons
JPanel buttonPanel = new JPanel();
[Link](redButton);
[Link](greenButton);
[Link](blueButton);
add(buttonPanel, [Link]);
@Override
public void actionPerformed(ActionEvent e) {
// Reset all buttons to unselected
if ([Link]() == redButton) {
if ([Link]()) {
[Link]([Link]);
[Link](false);
[Link](false);
} else {
[Link]([Link]);
} else if ([Link]() == greenButton) {
if ([Link]()) {
[Link]([Link]);
[Link](false);
[Link](false);
} else {
[Link]([Link]);
} else if ([Link]() == blueButton) {
if ([Link]()) {
[Link]([Link]);
[Link](false);
[Link](false);
} else {
[Link]([Link]);
public static void main(String[] args) {
// Run the application
[Link](() -> {
ColorToggleButtons frame = new ColorToggleButtons();
[Link](true);
});
OUTPUT: