Traffic lights

 file name :Traffic 



program :

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


class Traffic extends JFrame implements ActionListener {

    JLabel l1, l2, l3;

    JRadioButton r1, r2, r3;

    ButtonGroup bg;


    Traffic() {

        l1 = new JLabel(" ");

        l1.setBounds(50, 200, 200, 30);

        add(l1);

        l2 = new JLabel(" ");

        l2.setBounds(50, 240, 200, 30);

        add(l2);

        l3 = new JLabel(" ");

        l3.setBounds(50, 260, 200, 30);

        add(l3);

        r1 = new JRadioButton("RED");

        r1.setBounds(100, 60, 100, 30);

        r1.setBackground(Color.RED);

        r2 = new JRadioButton("YELLOW");

        r2.setBounds(100, 100, 100, 30);

        r2.setBackground(Color.YELLOW);

        r3 = new JRadioButton("GREEN");

        r3.setBounds(100, 160, 100, 30);

        r3.setBackground(Color.GREEN);

        bg = new ButtonGroup();

        bg.add(r1);

        bg.add(r2);

        bg.add(r3);

        add(r1);

        add(r2);

        add(r3);

        r1.addActionListener(this);

        r2.addActionListener(this);

        r3.addActionListener(this);

        setSize(400, 400);

        setTitle("TRAFFIC LIGHT");

        setLayout(null);

        setVisible(true);


    }


    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == r1) {

            l1.setText("STOP");

            l1.setForeground(Color.RED);

        } else if (e.getSource() == r2) {

            l2.setText("GET READY");

            l2.setForeground(Color.YELLOW);

        } else if (e.getSource() == r3) {

            l3.setText("GO");

            l3.setForeground(Color.GREEN);

        }

    }


    public static void main(String[] args) {

        new Traffic();

    }

}





output :




Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

CLL by smd

QUEUE USING ARRAYS by smd