/* 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 Interfacing1Sw 

6.     extends JApplet 

7.

8.   JButton bt ; 

9.   AnotherComponentSw un ; 

10.   YetAnotherOneSw dos ; 

11.  

12.   public Interfacing1Sw (  ) 

13.   { 

14.     bt = new JButton ( "Annnnnnd, ACTION !" ) ; 

15.     un = new AnotherComponentSw(); 

16.     dos = new YetAnotherOneSw(); 

17.   } 

18.   public void init() 

19.   { 

20.     Container cp = getContentPane(); 

21.     cp.setLayout( new FlowLayout() ); 

22.     un.setBackground( Color.yellow ); 

23.     un.setPreferredSize( new Dimension(100,200) ); 

24.     cp.add ( un ); 

25.     cp.add ( bt); 

26.     dos.setBackground( Color.cyan ); 

27.     dos.setPreferredSize( new Dimension(200,150) ); 

28.     cp.add ( dos ); 

29.  

30.     bt.addActionListener( un ); 

31.     bt.addActionListener( dos ); 

32.   } 

33.

34. //========================= 

35. class AnotherComponentSw 

36.   extends JPanel 

37.   implements ActionListener 

38.

39.   public void actionPerformed ( ActionEvent ev ) 

40.   { 

41.     add ( new JLabel (" Abc ") ) ; 

42.     revalidate(); 

43.   } 

44.

45. //========================= 

46. class YetAnotherOneSw 

47.   extends JPanel 

48.   implements ActionListener 

49.

50.   public void actionPerformed ( ActionEvent ev ) 

51.   { 

52.     add ( new JTextField (" Xyz ") ) ; 

53.     revalidate(); 

54.   } 

55.

56. //========== ActionListener.java ================ 

57. public interface ActionListener extends EventListener 

58.

59.     public void actionPerformed( ActionEvent e ) 

60.     ; 

61.

62. //======== in JButton.java ================ 

63. public void addActionListener 

64.              ( ActionListener l ) .... 

65.  

66.