mirror of
https://github.com/Manoj-HV30/OOPS-lab-codes.git
synced 2026-05-16 19:35:25 +00:00
e16a668d26
This class provides a GUI for selecting colors using checkboxes and displays the selected color.
93 lines
2.7 KiB
Java
93 lines
2.7 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.*;
|
|
|
|
public class ColorSelector extends JFrame implements ActionListener {
|
|
|
|
JCheckBox redCheckBox, greenCheckBox, blueCheckBox;
|
|
JButton applyButton;
|
|
JLabel colorLabel;
|
|
|
|
public ColorSelector() {
|
|
|
|
setTitle("Color Selector");
|
|
setSize(400, 250);
|
|
setLayout(null);
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
// Checkboxes
|
|
redCheckBox = new JCheckBox("Red");
|
|
greenCheckBox = new JCheckBox("Green");
|
|
blueCheckBox = new JCheckBox("Blue");
|
|
|
|
redCheckBox.setBounds(50, 30, 100, 30);
|
|
greenCheckBox.setBounds(150, 30, 100, 30);
|
|
blueCheckBox.setBounds(250, 30, 100, 30);
|
|
|
|
// Button
|
|
applyButton = new JButton("Apply Color");
|
|
applyButton.setBounds(130, 80, 140, 30);
|
|
applyButton.addActionListener(this);
|
|
|
|
// Label
|
|
colorLabel = new JLabel("Selected Color", JLabel.CENTER);
|
|
colorLabel.setBounds(80, 130, 240, 60);
|
|
colorLabel.setOpaque(true); // REQUIRED
|
|
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // REQUIRED
|
|
|
|
// Add components
|
|
add(redCheckBox);
|
|
add(greenCheckBox);
|
|
add(blueCheckBox);
|
|
add(applyButton);
|
|
add(colorLabel);
|
|
|
|
setVisible(true);
|
|
}
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
boolean red = redCheckBox.isSelected();
|
|
boolean green = greenCheckBox.isSelected();
|
|
boolean blue = blueCheckBox.isSelected();
|
|
|
|
if (red && !green && !blue) {
|
|
colorLabel.setBackground(Color.RED);
|
|
colorLabel.setText("RED");
|
|
}
|
|
else if (!red && green && !blue) {
|
|
colorLabel.setBackground(Color.GREEN);
|
|
colorLabel.setText("GREEN");
|
|
}
|
|
else if (!red && !green && blue) {
|
|
colorLabel.setBackground(Color.BLUE);
|
|
colorLabel.setText("BLUE");
|
|
}
|
|
else if (red && green && !blue) {
|
|
colorLabel.setBackground(Color.YELLOW);
|
|
colorLabel.setText("YELLOW");
|
|
}
|
|
else if (red && !green && blue) {
|
|
colorLabel.setBackground(Color.MAGENTA);
|
|
colorLabel.setText("MAGENTA");
|
|
}
|
|
else if (!red && green && blue) {
|
|
colorLabel.setBackground(Color.CYAN);
|
|
colorLabel.setText("CYAN");
|
|
}
|
|
else if (red && green && blue) {
|
|
colorLabel.setBackground(Color.WHITE);
|
|
colorLabel.setText("WHITE");
|
|
}
|
|
else {
|
|
colorLabel.setBackground(null);
|
|
colorLabel.setText("No color selected");
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
new ColorSelector();
|
|
}
|
|
}
|