Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6.  
  7. /**
  8. * .
  9. *
  10. * @author (MB)
  11. * @version (20.10.10)
  12. */
  13. public class TrafficLight extends JFrame implements ActionListener
  14. {
  15.  
  16. /**
  17. * Konstruktor zum konstruieren von einer Straßenampel
  18. */
  19. private JPanel pnPanel;
  20. private JTextField tfLight1, tfLight2, tfLight3;
  21. private JLabel lbTFBg1, lbTFBg2;
  22. private JButton btLight1, btLight2, btLight3, btNext;
  23. private int iActivePhase;
  24.  
  25. public TrafficLight()
  26. {
  27. super("TrafficL");
  28. pnPanel = new JPanel();
  29. getContentPane().add(pnPanel);
  30. pnPanel.setLayout(null);
  31. setBounds(50, 70, 40, 170);
  32.  
  33. tfLight1 = new JTextField("");
  34. tfLight1.setForeground( Color.RED );
  35. tfLight1.setBackground( Color.GRAY );
  36. tfLight1.setOpaque(true);
  37. tfLight1.setBounds(10, 10, 30, 30);
  38.  
  39. tfLight2 = new JTextField("");
  40. tfLight2.setForeground( Color.YELLOW );
  41. tfLight2.setBackground( Color.GRAY );
  42. tfLight2.setOpaque(true);
  43. tfLight2.setBounds(10, 40, 30, 30);
  44.  
  45. tfLight3 = new JTextField("");
  46. tfLight3.setForeground( Color.RED );
  47. tfLight3.setBackground( Color.GRAY );
  48. tfLight3.setOpaque(true);
  49. tfLight3.setBounds(10, 70, 30, 30);
  50.  
  51.  
  52.  
  53.  
  54. btNext = new JButton("Go");
  55. btNext.setBounds(32, 100, 60, 20);
  56. btNext.addActionListener(this);
  57. btNext.setActionCommand("Go");
  58.  
  59. lbTFBg1 = new JLabel("");
  60. lbTFBg1.setBackground( Color.BLACK );
  61. lbTFBg1.setOpaque(true);
  62. lbTFBg1.setBounds(5, 5, 40, 90);
  63.  
  64. lbTFBg2 = new JLabel("");
  65. lbTFBg2.setBackground( Color.BLACK );
  66. lbTFBg2.setOpaque(true);
  67. lbTFBg2.setBounds(20, 100, 10, 50);
  68.  
  69.  
  70. pnPanel.add(tfLight3, 0);
  71. pnPanel.add(tfLight2, 1);
  72. pnPanel.add(tfLight1, 2);
  73.  
  74.  
  75.  
  76. pnPanel.add(btNext);
  77.  
  78. pnPanel.add(lbTFBg1, 3);
  79. pnPanel.add(lbTFBg2);
  80.  
  81. iActivePhase = 0;
  82. tfLight1.setBackground( Color.RED );
  83.  
  84. setVisible(true);
  85. }
  86.  
  87. }
  88. public void actionPerformed(ActionEvent e) {
  89.  
  90. tfLight1.setBackground( Color.GRAY );
  91. tfLight2.setBackground( Color.GRAY );
  92. tfLight3.setBackground( Color.GRAY );
  93.  
  94.  
  95.  
  96. if("Go" == e.getActionCommand())
  97. {
  98. iActivePhase++;
  99.  
  100. if(iActivePhase > 3)
  101. iActivePhase = 0;
  102.  
  103. if(iActivePhase == 0)
  104. tfLight1.setBackground( Color.RED );
  105.  
  106. if(iActive
  107.  
  108. if(iActivePhase == 1)
  109. tfLight2.setBackground( Color.YELLOW );
  110.  
  111. if(iActivePhase == 1)
  112. tfLight1.setBackground( Color.RED );
  113.  
  114. if(iActivePhase == 2)
  115. tfLight3.setBackground( Color.GREEN );
  116.  
  117. if(iActivePhase == 3)
  118. tfLight2.setBackground( Color.YELLOW );
  119.  
  120.  
  121.  
  122.  
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement