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

6.     extends JApplet 

7.     implements ActionListener 

8.

9.   JButton  btn ; 

10.   JTextField tf ; 

11.   JTextArea ta ; 

12.   JLabel lbl ; 

13.   Container cp ; 

14.  

15.   public void 

16.   actionPerformed( ActionEvent ev ) 

17.   { 

18.     Dimension cd = cp.getSize () ; 

19.     Dimension bd = btn.getSize () ; 

20.     Dimension td = tf.getSize () ; 

21.     Dimension ld = lbl.getSize () ; 

22.  

23.     ta.append ( "CP's width : " + cd.width + "\n" ) ; 

24.     ta.append ( "CP's height : " + cd.height + "\n" ) ; 

25.  

26.     ta.append ( "Button size : " + bd + "\n" ) ; 

27.     ta.append ( "TextField size : " + td + "\n" ) ; 

28.     ta.append ( "Label size : " + ld + "\n" ) ; 

29.  

30.     ta.append ( "=====================================\n" ) ; 

31.  

32.     int x, y ; 

33.     x = cd.width / 2 - bd.width / 2

34.     y = cd.height - bd.height ; 

35.  

36.     cp.setLayout( null ); 

37.     btn.setLocation (  x, y  ) ; 

38.   } 

39.   public 

40.   SizeSw( ) 

41.   { 

42.     btn= new JButton ( "Clickable" ) ; 

43.     tf = new JTextField ( "A TextField" ); 

44.     ta = new JTextArea ( 10, 30 ); 

45.     lbl = new JLabel ( "My name's Mabel, I'm a Label" ) ; 

46.   } 

47.   public void 

48.   init() 

49.   { 

50.     cp = getContentPane() ; 

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

52.     cp.add ( btn ) ; 

53.     cp.add ( tf ) ; 

54.     cp.add ( lbl ) ; 

55.     cp.add ( ta ) ; 

56.  

57.     cp.setBackground ( Color.yellow ) ; 

58.     btn.addActionListener ( this ) ; 

59.   } 

60.