Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.Color;
- import java.awt.Dimension;
- /**
- * .
- *
- * @author (MB)
- * @version (20.10.10)
- */
- public class TrafficLight extends JFrame implements ActionListener
- {
- /**
- * Konstruktor zum konstruieren von einer Straßenampel
- */
- private JPanel pnPanel;
- private JTextField tfLight1, tfLight2, tfLight3;
- private JLabel lbTFBg1, lbTFBg2;
- private JButton btLight1, btLight2, btLight3, btNext;
- private int iActivePhase;
- public TrafficLight()
- {
- super("TrafficL");
- pnPanel = new JPanel();
- getContentPane().add(pnPanel);
- pnPanel.setLayout(null);
- setBounds(50, 70, 40, 170);
- tfLight1 = new JTextField("");
- tfLight1.setForeground( Color.RED );
- tfLight1.setBackground( Color.GRAY );
- tfLight1.setOpaque(true);
- tfLight1.setBounds(10, 10, 30, 30);
- tfLight2 = new JTextField("");
- tfLight2.setForeground( Color.YELLOW );
- tfLight2.setBackground( Color.GRAY );
- tfLight2.setOpaque(true);
- tfLight2.setBounds(10, 40, 30, 30);
- tfLight3 = new JTextField("");
- tfLight3.setForeground( Color.RED );
- tfLight3.setBackground( Color.GRAY );
- tfLight3.setOpaque(true);
- tfLight3.setBounds(10, 70, 30, 30);
- btNext = new JButton("Go");
- btNext.setBounds(32, 100, 60, 20);
- btNext.addActionListener(this);
- btNext.setActionCommand("Go");
- lbTFBg1 = new JLabel("");
- lbTFBg1.setBackground( Color.BLACK );
- lbTFBg1.setOpaque(true);
- lbTFBg1.setBounds(5, 5, 40, 90);
- lbTFBg2 = new JLabel("");
- lbTFBg2.setBackground( Color.BLACK );
- lbTFBg2.setOpaque(true);
- lbTFBg2.setBounds(20, 100, 10, 50);
- pnPanel.add(tfLight3, 0);
- pnPanel.add(tfLight2, 1);
- pnPanel.add(tfLight1, 2);
- pnPanel.add(btNext);
- pnPanel.add(lbTFBg1, 3);
- pnPanel.add(lbTFBg2);
- iActivePhase = 0;
- tfLight1.setBackground( Color.RED );
- setVisible(true);
- }
- }
- public void actionPerformed(ActionEvent e) {
- tfLight1.setBackground( Color.GRAY );
- tfLight2.setBackground( Color.GRAY );
- tfLight3.setBackground( Color.GRAY );
- if("Go" == e.getActionCommand())
- {
- iActivePhase++;
- if(iActivePhase > 3)
- iActivePhase = 0;
- if(iActivePhase == 0)
- tfLight1.setBackground( Color.RED );
- if(iActive
- if(iActivePhase == 1)
- tfLight2.setBackground( Color.YELLOW );
- if(iActivePhase == 1)
- tfLight1.setBackground( Color.RED );
- if(iActivePhase == 2)
- tfLight3.setBackground( Color.GREEN );
- if(iActivePhase == 3)
- tfLight2.setBackground( Color.YELLOW );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement