Professional Documents
Culture Documents
Java CA-1
Java CA-1
Java CA-1
CA-1
Write a java program that simulates a traffic light. The program lets the user select one of three
lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one
light can be on at a time. No light is on when the program starts.
Solution:
package ca1swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CA1Swing{
JFrame frame = new JFrame("Traffic Lights");
JRadioButton radB1 = new JRadioButton("Red");
JRadioButton radB2 = new JRadioButton("Yellow");
JRadioButton radB3 = new JRadioButton("Green");
JTextField textF1 = new JTextField();
JTextField textF2 = new JTextField();
JTextField textF3 = new JTextField();
ButtonGroup buttonG = new ButtonGroup();
CA1Swing()
{
buttonG.add(radB1);
buttonG.add(radB2);
buttonG.add(radB3);
textF1.setEditable(false);
textF2.setEditable(false);
textF3.setEditable(false);
textF1.setBackground(Color.WHITE);
textF2.setBackground(Color.WHITE);
textF3.setBackground(Color.WHITE);
radB1.addActionListener(new ActionListener() {
Output: