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

6. public class OurWindowSw 

7.     extends JApplet 

8.     implements ActionListener 

9.

10.   JButton  btn = new JButton ( "Make or break a Frame window" ) ; 

11.   OurFrameSw wnd ; 

12.   Container cp ; 

13.  

14.   public void 

15.   actionPerformed( ActionEvent ev ) 

16.   { 

17.     if  ( wnd != null

18.     { 

19.       wnd.dispose() ; 

20.       wnd = null

21.       return

22.     } 

23.     wnd = new OurFrameSw ( "I'm a JFrame window", this ) ; 

24.     wnd.setSize ( 400,200 ); 

25.     wnd.setLocation ( 200,300 ); 

26.     wnd.setVisible ( true ); 

27.     wnd.setResizable( false ); 

28.   } 

29.   public void 

30.   init() 

31.   { 

32.     cp = getContentPane(); 

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

34.     cp.setBackground ( Color.pink ) ; 

35.     cp.add ( btn) ; 

36.     btn.addActionListener ( this ) ; 

37.   } 

38.

39. //================ 

40. class OurFrameSw 

41.     extends JFrame 

42.     implements ActionListener 

43.

44.   OurWindowSw parent ; 

45.   JButton  btn ; 

46.   Container cp ; 

47.  

48.   public void 

49.   actionPerformed( ActionEvent ev ) 

50.   { 

51.     Toolkit.getDefaultToolkit().beep(); 

52.   } 

53.  

54.   OurFrameSw ( String t, OurWindowSw  p ) 

55.   { 

56.     super ( t ); 

57.     parent = p ; 

58.     cp = getContentPane(); 

59.     btn = new JButton ( "B e e p" ) ; 

60.     cp.setBackground ( Color.pink ) ; 

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

62.     cp.add ( btn ) ; 

63.     addWindowListener ( 

64.       new WindowAdapter() 

65.       { 

66.        public void windowClosing(WindowEvent ev) 

67.        { 

68.         parent.wnd = null

69.         dispose(); 

70.        } 

71.       } 

72.      ); 

73.     btn.addActionListener ( this ) ; 

74.   } 

75.