/* 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 javax.swing.* ; 

2. import java.awt.* ; 

3. import java.awt.event.*; 

4.  

5. public class CheckSw 

6.     extends JApplet 

7.     implements ActionListener 

8.

9.   ButtonGroup rbgrp ; 

10.  

11.   JRadioButton rb1, rb2, rb3 ; 

12.  

13.   JCheckBox cb1, cb2 ; 

14.  

15.   JButton btn ; 

16.   JLabel lb1, lb2, lb3 ; 

17.   Container cp; 

18.  

19.   public void actionPerformed( ActionEvent ev ) 

20.   { 

21.     String str ; 

22.  

23.     if ( rb1.isSelected() ) 

24.       str = "So you want a fulfilling life"

25.     else 

26.       if ( rb2.isSelected() ) 

27.         str = "So you want a long life"

28.       else 

29.         str = "So you want an exciting life"

30.  

31.     lb1.setText ( str ) ; 

32.  

33.  

34.     boolean checked; 

35.  

36.     checked = cb1.isSelected() ; 

37.  

38.     if ( checked ) 

39.       str = "with plenty of money"

40.     else 

41.       str = "with food"

42.  

43.     lb2.setText ( str ) ; 

44.  

45.  

46.     checked = cb2.isSelected() ; 

47.  

48.     if ( checked ) 

49.       str = "and many friends"

50.     else 

51.       str = "alone"

52.  

53.     lb3.setText ( str ) ; 

54.   } 

55.  

56.   public CheckSw ( ) 

57.   { 

58.     rbgrp = new ButtonGroup ( ) ; 

59.     rb1 = new JRadioButton ( "Fulfilling", true ) ; 

60.     rb2 = new JRadioButton ( "Long", false ) ; 

61.     rb3 = new JRadioButton ( "Exciting", false ) ; 

62.  

63.     rbgrp.add( rb1 ); rbgrp.add( rb2 ); rbgrp.add( rb3); 

64.  

65.     cb1 = new JCheckBox ( "with obscene amounts of money", false ) ; 

66.     cb2 = new JCheckBox ( "lots of friends", false ) ; 

67.  

68.     btn = new JButton ( "Do it now !" ) ; 

69.     lb1 = new JLabel ( ) ; 

70.     lb2 = new JLabel ( ) ; 

71.     lb3 = new JLabel ( ) ; 

72.   } 

73.  

74.  

75.   public void init() 

76.   { 

77.     cp = getContentPane(); 

78.     cp.setLayout ( new GridLayout ( 11,0 ) ) ; 

79.     cp.setBackground ( Color.yellow ) ; 

80.  

81.     cp.add ( new JLabel ( "A life that is " ) ) ; 

82.  

83.     cp.add ( rb1 ) ; 

84.     cp.add ( rb2 ) ; 

85.     cp.add ( rb3 ) ; 

86.  

87.     cp.add ( cb1 ) ; 

88.     cp.add ( cb2 ) ; 

89.  

90.     cp.add ( btn ) ; 

91.     btn.addActionListener ( this ) ; 

92.  

93.     lb1.setBackground ( Color.orange ) ; 

94.     lb1.setOpaque(true); 

95.     lb2.setBackground ( Color.pink ) ; 

96.     lb2.setOpaque(true); 

97.     lb3.setBackground ( Color.cyan ) ; 

98.     lb3.setOpaque(true); 

99.  

100.     cp.add ( lb1 ) ; 

101.     cp.add ( lb2 ) ; 

102.     cp.add ( lb3 ) ; 

103.   } 

104.