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

3. import java.awt.*; 

4. import java.awt.event.*; 

5.  

6. public class BoxLaySw 

7.     extends JApplet 

8.

9.   JLabel lbl, lbl2 ; 

10.   JTextField tf ; 

11.   JButton bt ; 

12.   JSlider sl ; 

13.   JPanel pl2 = new JPanel() ; 

14.   JPanel pl3 = new JPanel() ; 

15.   Container cp ; 

16.  

17.   public void init() 

18.   { 

19.     lbl = new JLabel( "Living in a box" ); 

20.     lbl2 = new JLabel( "Do, Be, Do, Be, Do" ); 

21.     tf = new JTextField ( "Aurore", 20 ); 

22.     bt = new JButton ( "JLessons button" ) ; 

23.     sl = new JSlider ( JSlider.HORIZONTAL, 0, 50, 10 ); 

24.  

25.     cp = getContentPane() ; 

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

27.  

28.     pl2.setLayout 

29.      ( new BoxLayout ( pl2, BoxLayout.X_AXIS ) ) ; 

30.     pl3.setLayout 

31.      ( new BoxLayout ( pl3, BoxLayout.Y_AXIS ) ) ; 

32.  

33.     lbl.setBackground ( Color.pink ) ; 

34.     lbl.setOpaque ( true ) ; 

35.     tf.setMaximumSize( tf.getPreferredSize() ); 

36.  

37.     lbl.setAlignmentY( Component.BOTTOM_ALIGNMENT ); 

38.     tf.setAlignmentY( Component.BOTTOM_ALIGNMENT ); 

39.  

40.     pl2.add ( lbl ) ; 

41.     pl2.add( Box.createRigidArea(new Dimension(40,0)) ) ; 

42.     pl2.add ( tf ) ; 

43.  

44.     lbl2.setBackground ( Color.white ) ; 

45.     lbl2.setOpaque ( true ) ; 

46.  

47.     bt.setAlignmentX( Component.LEFT_ALIGNMENT ); 

48.     lbl2.setAlignmentX( Component.LEFT_ALIGNMENT ); 

49.     sl.setAlignmentX( Component.LEFT_ALIGNMENT ); 

50.  

51.     pl3.add ( bt ); 

52.     pl3.add ( Box.createRigidArea(new Dimension(0,20)) ); 

53.     pl3.add ( lbl2 ); 

54.     pl3.add ( Box.createRigidArea(new Dimension(0,60)) ); 

55.     pl3.add ( sl ); 

56.  

57.     pl2.setPreferredSize ( new Dimension(200,100) ) ; 

58.     pl3.setPreferredSize ( new Dimension(250,300) ) ; 

59.     pl2.setBackground ( Color.green ) ; 

60.     pl3.setBackground ( Color.yellow ) ; 

61.  

62.     cp.add ( pl2 ) ; 

63.     cp.add ( pl3 ) ; 

64.  

65.     cp.setBackground ( Color.white ) ; 

66.   } 

67.

68.