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

6.   extends JApplet implements ActionListener 

7.

8.   JPanel pl1, pl2 ; 

9.   JButton p1btn, btn, xbtn ; 

10.   JLabel lbl; 

11.   JTextField tf; 

12.   boolean firsttime = true

13.   Container cp ; 

14.  

15.   public GLaySw ( ) 

16.   { 

17.     pl1 = new JPanel (); 

18.     pl2 = new JPanel (); 

19.  

20.     p1btn = new JButton ( "Change the GridLayout" ) ; 

21.     btn = new JButton ( "You can click me, but don't feel obligated" ) ; 

22.     xbtn = new JButton ( "Hello" ) ; 

23.     tf = new JTextField ( "Move those virtuoso fingers" ); 

24.     lbl = new JLabel ( "This is too much, I need my lawyer" ) ; 

25.   } 

26.  

27.   public void init() 

28.   { 

29.     cp = getContentPane(); 

30.     cp.setLayout ( new BorderLayout () ) ; 

31.     pl2.setLayout ( new GridLayout ( 5, 0 ) ) ; 

32.  

33.     pl1.setBackground ( Color.green ) ; 

34.  

35.     p1btn.addActionListener ( this ) ; 

36.     pl1.add ( p1btn ) ; 

37.  

38.     pl2.add ( btn ) ; 

39.     pl2.add ( tf ) ; 

40.     pl2.add ( xbtn ) ; 

41.     lbl.setBackground ( Color.yellow ) ; 

42.     lbl.setOpaque(true); 

43.     pl2.add ( lbl ) ; 

44.     pl2.setBackground ( Color.pink ) ; 

45.  

46.     cp.add ( pl1,  BorderLayout.NORTH  ) ; 

47.     cp.add ( pl2,  BorderLayout.CENTER ) ; 

48.   } 

49.   public void actionPerformed(ActionEvent parm1) 

50.   { 

51.     if ( firsttime ) 

52.     { 

53.       pl2.setLayout ( new GridLayout ( 0, 2 ) ) ; 

54.       pl2.revalidate() ; 

55.       firsttime = false

56.     } 

57.     else 

58.     { 

59.       pl2.setLayout ( new GridLayout ( 0, 2, 30, 15 ) ) ; 

60.       pl2.revalidate() ; 

61.     } 

62.   } 

63.