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

2. import javax.swing.* ; 

3. import java.awt.* ; 

4. import java.awt.event.*; 

5.  

6. public class MenuSw 

7.     extends JApplet 

8.     implements ActionListener 

9.

10.   OwnFrameSw wnd ; 

11.   Container cp ; 

12.   Button bt = new Button( "Enlarge" ); 

13.  

14.   public void 

15.   init() 

16.   { 

17.     cp = getContentPane() ; 

18.     cp.setBackground ( Color.yellow ) ; 

19.  

20.     wnd = new OwnFrameSw ( "Frame with menu", this ) ; 

21.     wnd.setSize ( 400,350 ); 

22.     wnd.setLocation ( 100, 75 ); 

23.     wnd.setVisible ( true ); 

24.  

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

26.     cp.add(bt); 

27.     bt.setActionCommand( "GROW" ); 

28.     bt.addActionListener( wnd ); 

29.   } 

30.   public void 

31.   actionPerformed( ActionEvent ev ) 

32.   { 

33.     Object src = ev.getSource () ; 

34.     if ( src ==  wnd.toolscolorpink ) 

35.       cp.setBackground ( Color.pink ) ; 

36.     else 

37.       if  ( src ==  wnd.toolscolororange ) 

38.         cp.setBackground ( Color.orange ) ; 

39.   } 

40.

41. ====  OwnFrameSw.java  ============ 

42. import javax.swing.* ; 

43. import javax.swing.event.* ; 

44. import java.awt.*; 

45. import java.awt.event.*; 

46.  

47. public class OwnFrameSw 

48.     extends JFrame 

49.     implements ActionListener, ItemListener 

50.

51.   JMenuBar mb ; 

52.  

53.   JMenu view ; 

54.   JMenu tools ; 

55.  

56.   JMenuItem viewsize ; 

57.   JCheckBoxMenuItem viewendisable ; 

58.  

59.   JMenu toolscolor ; 

60.   JMenuItem toolscolorpink ; 

61.   JMenuItem toolscolororange ; 

62.   JMenuItem toolssmt ; 

63.  

64.   MenuSw parent ; 

65.  

66.   public OwnFrameSw ( String tit , MenuSw apl ) 

67.   { 

68.     super ( tit ) ; 

69.     parent = apl ;  ///MenuSw 

70.  

71.     mb = new JMenuBar () ; 

72.     setJMenuBar ( mb ) ; 

73.  

74.     view = new JMenu ( "View" ) ; 

75.  

76.     viewsize = new JMenuItem ( "Size" ) ; 

77.     viewsize.setAccelerator 

78.     ( KeyStroke.getKeyStroke( KeyEvent.VK_T, Event.CTRL_MASK ) ); 

79.     viewendisable = new JCheckBoxMenuItem 

80.       ( "Enable/Disable Tools,Sumthin" , true ) ; 

81.  

82.     view.add ( viewsize ) ; 

83.     view.add ( viewendisable ) ; 

84.  

85.     mb.add ( view ) ; 

86.  

87.     tools = new JMenu ( "Tools" ) ; 

88.  

89.     toolscolor = new JMenu ( "Color applet" ) ; 

90.     toolscolorpink = new JMenuItem ( "Pink" ) ; 

91.     toolscolororange = new JMenuItem ( "Orange" ) ; 

92.  

93.     toolssmt = new JMenuItem ( "Sumthin, dude" ) ; 

94.     toolssmt.setActionCommand("GROW"); 

95.     toolssmt.addActionListener(this); 

96.  

97.     tools.add ( toolscolor ); 

98.     toolscolor.add ( toolscolorpink ); 

99.     toolscolor.add ( toolscolororange ); 

100.     tools.addSeparator (); 

101.     tools.add ( toolssmt ); 

102.  

103.     mb.add ( tools ) ; 

104.  

105.     viewsize.addActionListener ( this ) ; 

106.     viewendisable.addItemListener ( this ) ; 

107.  

108.     toolscolorpink.addActionListener ( parent ) ; 

109.     toolscolororange.addActionListener ( parent ) ; 

110.  

111.     addWindowListener ( new WA () ) ; 

112.   } 

113.   public void 

114.   actionPerformed( ActionEvent ev ) 

115.   { 

116.     Object src = ev.getSource () ; 

117.  

118.     if ( src == viewsize ) 

119.     { 

120.       Dimension sz = getSize () ; 

121.       setSize (  (sz.width * 9) / 10 , (sz.height * 9) / 10  ) ; 

122.       return

123.     } 

124.     String str = ev.getActionCommand(); 

125.     if ( str.equals("GROW") ) 

126.       setSize( 400,300 ); 

127.   } 

128.   public void 

129.   itemStateChanged ( ItemEvent  ev ) 

130.   { 

131.     Object src = ev.getSource () ; 

132.  

133.     if ( src == viewendisable ) 

134.     { 

135.       boolean on = viewendisable.getState (); 

136.       if ( on ) 

137.         toolssmt.setEnabled ( true ) ; 

138.       else 

139.         toolssmt.setEnabled ( false ) ; 

140.       return

141.     } 

142.   } 

143.  

144.   class WA extends WindowAdapter 

145.   { 

146.     public void windowClosing ( WindowEvent ev ) 

147.     { 

148.       dispose (); 

149.     } 

150.   } 

151.

152. ======= (miscellaneous) ======================== 

153.   mb.remove ( view ); 

154.   tools.add ( anotheritem ) ; 

155.   tools.insert ( someitem, 2 ) ; 

156. ===== unified : ================== 

157. public class ActionUni 

158.     extends JFrame 

159.

160.   JMenuBar mb ; 

161.   JMenu axem ; 

162.   JMenuItem axeitem ; 

163.   JButton bt ; 

164.   DoIt doo ; 

165.  

166. class DoIt 

167.  extends AbstractAction 

168.

169.   public DoIt ( String s, ImageIcon pic, 

170.                     String dsc, Integer mnem ) 

171.   { 

172.     super( s, pic ); 

173.     putValue( SHORT_DESCRIPTION, dsc ); 

174.     putValue( MNEMONIC_KEY, mnem ); 

175.   } 

176.   int cnt = 0

177.   public void 

178.   actionPerformed( ActionEvent ev ) 

179.   { 

180.     cnt++ ; 

181.     if ( cnt > 4 ) doo.setEnabled( false ); 

182.     Toolkit.getDefaultToolkit().beep(); 

183.   } 

184. } //end DoIt 

185.  

186.   public ActionUni() 

187.   { 

188.     super ( "Some Title" ) ; 

189.     doo = new DoIt ( "Do it now"

190.       new ImageIcon("pic.gif"), 

191.        "A trivial axion"

192.         new Integer ( KeyEvent.VK_D ) ); 

193.  

194.     axeitem = new JMenuItem ( doo ) ; 

195.     bt = new JButton ( doo ) ; 

196. ..etc.. 

197.