/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import java.awt.* ; 

2. import java.applet.* ; 

3. import java.awt.event.*; 

4.  

5. public class Statik 

6.     extends Applet implements ActionListener 

7.

8.   OurTF [] tfs ; 

9.   int inx = 0

10.  

11.   final static int MAXTFS = 15

12.  

13.   Button btn ; 

14.   Label lbl; 

15.  

16.   public void 

17.   actionPerformed ( ActionEvent  e ) 

18.   { 

19.     if ( inx >= MAXTFS

20.     { 

21.       Toolkit.getDefaultToolkit().beep() ; 

22.       return

23.     } 

24.  

25.     tfs [inx] = new OurTF ( ) ; 

26.     add ( tfs[inx] ) ; 

27.     inx++ ; 

28.  

29.     lbl.setText 

30.     ( "Now " 

31.       + OurTF.currentNrOfInstances 

32.       + " instances." ) ; 

33.  

34.     validate ( ) ; 

35.   } 

36.  

37.   public Statik ( ) 

38.   { 

39.     tfs = new OurTF [ MAXTFS ] ; 

40.     btn = new Button ( "Make another one") ; 

41.     lbl = new Label ( "None yet" ) ; 

42.   } 

43.   public void init() 

44.   { 

45.     add (btn); 

46.     btn.addActionListener ( this ) ; 

47.     add (lbl); 

48.  

49.     lbl.setBackground ( Color.white ) ; 

50.     setBackground ( Color.yellow ) ; 

51.     validate() ; 

52.   } 

53.

54. //================ OurTF.java ================== 

55. class OurTF 

56.   extends java.awt.TextField 

57.

58.   static int currentNrOfInstances = 0

59.   static 

60.   {  currentNrOfInstances = 0 ;  } 

61.   int randomColor = 0

62.  

63.   OurTF (  ) 

64.   { 

65.     super ( ) ; 

66.     currentNrOfInstances ++ ; 

67.  

68.     randomColor = (int) ( 

69.       Math.random ( ) 

70.       * Integer.MAX_VALUE  ) ; 

71.  

72.     setBackground ( new Color ( randomColor )  ) ; 

73.     setText ( randomColor + ""  ) ; 

74.   } 

75.  

76.