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

3. import java.applet.* ; 

4. import java.awt.event.*; 

5.  

6. public class PanelDivisionSw 

7.     extends JApplet 

8.     implements ActionListener 

9.

10.   JPanel p1, p2 ; 

11.   JButton btn ; 

12.   JLabel lbl ; 

13.  

14.   public PanelDivisionSw ( ) 

15.   { 

16.     p1 = new JPanel(); 

17.     p2 = new JPanel(); 

18.     btn = new JButton ( "Click me, please" ) ; 

19.     btn.add(new JPanel()); 

20.     lbl = new JLabel ( "I'm a Label, contained in a Panel" ) ; 

21.   } 

22.  

23.   Container cp; 

24.   public void init() 

25.   { 

26.     cp=this.getContentPane(); 

27.     cp.setLayout ( new GridLayout ( 2,0 ) ) ; 

28.  

29.     p1.add ( btn ) ; 

30.  

31.     p2.add ( lbl ) ; 

32.     p2.add ( new JButton ( "A Button, that nobody listens to" ) ) ; 

33.  

34.     p1.setBackground ( Color.yellow ) ; 

35.     p2.setBackground ( Color.orange ) ; 

36.  

37.     cp.add ( p1 ) ; 

38.     cp.add ( p2 ) ; 

39.  

40.     btn.addActionListener ( this ) ; 

41.   } 

42.   int count = 0

43.   public void actionPerformed( ActionEvent ev ) 

44.   { 

45.     p1.setBackground ( Color.pink ) ; 

46.     p2.setBackground ( Color.green ) ; 

47.     p2.add( new JLabel( "Nr." + count++ )  ); 

48.     p2.revalidate(); 

49.   } 

50.