Help with program...Please......

Reply

Join Date: Aug 2004
Posts: 15
Reputation: ZEEPLE is an unknown quantity at this point 
Solved Threads: 0
ZEEPLE ZEEPLE is offline Offline
Newbie Poster

Help with program...Please......

 
0
  #1
Aug 26th, 2004
Hello everyone,

I am writing a program that is for a programming class..This program has to display syntax when the appropriate button is pushed..it also has a menu bar that must be functional. It must also run as an applet and as an application.
The above is complete and my program functions as expected with a few minor problems...This is interesting...When I run it from Eclipse3.0 as an applet and use the exit function from the menu bar it exits with no problem..but, when I run it from the command line as an applet it will not exit properly. Also when I run it as an applet all the Syntax Buttons work without generating exceptions. If I run it as an application all the Syntax Buttons generate exceptions..???But yet it still works as expected...I am posting the code for anyone who may be able to help me..I am not asking for you to do it for me as you will see I have tried multiple ways of fixing it..I have commented out things that did not work.any help would be greatly appreciated.

ZEEPLE

  1.  
  2. /**
  3.  * Zeeple
  4.  *
  5.  *
  6.  */
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.JApplet;
  12. //import java.applet.Applet;
  13.  
  14. import javax.swing.JPanel;
  15. import javax.swing.BorderFactory;
  16. import java.awt.GridBagConstraints;
  17. import java.awt.GridBagLayout;
  18.  
  19. import javax.swing.JButton;
  20. import javax.swing.JTextArea;
  21.  
  22. public class MenuTest extends JApplet implements ActionListener{
  23.  
  24. private Container container;
  25. private GridBagLayout layout;
  26. private GridBagConstraints constraints;
  27. private JPanel pnPanel0;
  28.  
  29. private JPanel pnPanel2;
  30. private JButton btBut0;
  31. private JButton btBut1;
  32. private JButton btBut2;
  33.  
  34. private JPanel pnPanel3;
  35. private JButton btBut3;
  36. private JButton btBut4;
  37. private JButton btBut5;
  38. private JTextArea outputArea;
  39.  
  40.  
  41. private JLabel displayLabel;
  42. private ButtonGroup fontGroup, colorGroup;
  43. // private int style;
  44.  
  45. // set up GUI
  46. public MenuTest()
  47. {
  48. // super( "Using JMenus" );
  49.  
  50. // set up File menu and its menu items
  51. JMenu fileMenu = new JMenu( "File" );
  52. fileMenu.setMnemonic( 'F' );
  53.  
  54. // set up About... menu item
  55. JMenuItem helpItem = new JMenuItem( "Help..." );
  56. helpItem.setMnemonic( 'H' );
  57. fileMenu.add( helpItem );
  58. helpItem.addActionListener(
  59.  
  60. new ActionListener() { // anonymous inner class
  61.  
  62. // display message dialog when user selects About...
  63. public void actionPerformed( ActionEvent event )
  64. {
  65. JOptionPane.showMessageDialog( MenuTest.this,
  66. "This is my final project..Ver1.0..CSC211",
  67. "Help", JOptionPane.PLAIN_MESSAGE );
  68. }
  69.  
  70. } // end anonymous inner class
  71.  
  72. ); // end call to addActionListener
  73.  
  74. // set up Exit menu item
  75. JMenuItem exitItem = new JMenuItem( "Exit" );
  76. exitItem.setMnemonic( 'x' );
  77. fileMenu.add( exitItem );
  78. exitItem.addActionListener(
  79.  
  80. new ActionListener() { // anonymous inner class
  81.  
  82. // terminate application when user clicks exitItem
  83. public void actionPerformed( ActionEvent event )
  84. {
  85. System.exit( 0 );
  86. }
  87.  
  88. } // end anonymous inner class
  89.  
  90. ); // end call to addActionListener
  91.  
  92. // create menu bar and attach it to MenuTest window
  93. JMenuBar bar = new JMenuBar();
  94. setJMenuBar( bar );
  95. bar.add( fileMenu );
  96.  
  97.  
  98. pnPanel0 = new JPanel();
  99. GridBagLayout gbPanel0 = new GridBagLayout();
  100. GridBagConstraints gbcPanel0 = new GridBagConstraints();
  101. pnPanel0.setLayout( gbPanel0 );
  102.  
  103.  
  104.  
  105. pnPanel2 = new JPanel();
  106. pnPanel2.setBorder( BorderFactory.createTitledBorder( "Selection" ) );
  107. pnPanel2.setBackground(Color.BLUE);
  108. GridBagLayout gbPanel2 = new GridBagLayout();
  109. GridBagConstraints gbcPanel2 = new GridBagConstraints();
  110. pnPanel2.setLayout( gbPanel2 );
  111.  
  112. btBut0 = new JButton( "IF" );
  113. btBut0.setForeground(Color.BLUE);
  114. btBut0.addActionListener(this);
  115. /* new ActionListener(){
  116.   public void actionPerformed(ActionEvent event){
  117.  
  118.   // if(event.getSource() == btBut0)
  119.   String output="";
  120.   output += "if (condition) {" + "\nstatements;" + "\n};";
  121. outputArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 18));
  122.   outputArea.setBackground(Color.BLUE) ;
  123.   outputArea.setText( output );
  124.  
  125.   showStatus("You clicked the If button..");
  126.  
  127.  
  128.   }
  129. }
  130. );
  131. */
  132. gbcPanel2.gridx = 2;
  133. gbcPanel2.gridy = 1;
  134. gbcPanel2.gridwidth = 13;
  135. gbcPanel2.gridheight = 4;
  136. gbcPanel2.fill = GridBagConstraints.BOTH;
  137. gbcPanel2.weightx = 1;
  138. gbcPanel2.weighty = 0;
  139. gbcPanel2.anchor = GridBagConstraints.NORTH;
  140. gbPanel2.setConstraints( btBut0, gbcPanel2 );
  141. pnPanel2.add( btBut0 );
  142.  
  143. btBut1 = new JButton( "IF...ELSE" );
  144. btBut1.setForeground(Color.BLUE);
  145. btBut1.addActionListener(this);
  146. gbcPanel2.gridx = 2;
  147. gbcPanel2.gridy = 7;
  148. gbcPanel2.gridwidth = 13;
  149. gbcPanel2.gridheight = 4;
  150. gbcPanel2.fill = GridBagConstraints.BOTH;
  151. gbcPanel2.weightx = 1;
  152. gbcPanel2.weighty = 0;
  153. gbcPanel2.anchor = GridBagConstraints.NORTH;
  154. gbPanel2.setConstraints( btBut1, gbcPanel2 );
  155. pnPanel2.add( btBut1 );
  156.  
  157. btBut2 = new JButton( "SWITCH" );
  158. btBut2.setForeground(Color.BLUE);
  159. btBut2.addActionListener(this);
  160. gbcPanel2.gridx = 2;
  161. gbcPanel2.gridy = 13;
  162. gbcPanel2.gridwidth = 13;
  163. gbcPanel2.gridheight = 4;
  164. gbcPanel2.fill = GridBagConstraints.BOTH;
  165. gbcPanel2.weightx = 1;
  166. gbcPanel2.weighty = 0;
  167. gbcPanel2.anchor = GridBagConstraints.NORTH;
  168. gbPanel2.setConstraints( btBut2, gbcPanel2 );
  169. pnPanel2.add( btBut2 );
  170. gbcPanel0.gridx = 1;
  171. gbcPanel0.gridy = 4;
  172. gbcPanel0.gridwidth = 17;
  173. gbcPanel0.gridheight = 19;
  174. gbcPanel0.fill = GridBagConstraints.BOTH;
  175. gbcPanel0.weightx = 1;
  176. gbcPanel0.weighty = 0;
  177. gbcPanel0.anchor = GridBagConstraints.NORTH;
  178. gbPanel0.setConstraints( pnPanel2, gbcPanel0 );
  179. pnPanel0.add( pnPanel2 );
  180.  
  181. pnPanel3 = new JPanel();
  182. pnPanel3.setBorder( BorderFactory.createTitledBorder( "Repetition" ) );
  183. pnPanel3.setBackground(Color.RED);
  184. GridBagLayout gbPanel3 = new GridBagLayout();
  185. GridBagConstraints gbcPanel3 = new GridBagConstraints();
  186. pnPanel3.setLayout( gbPanel3 );
  187.  
  188. btBut3 = new JButton( "WHILE" );
  189. btBut3.setForeground(Color.RED);
  190. btBut3.addActionListener(this);
  191. gbcPanel3.gridx = 2;
  192. gbcPanel3.gridy = 1;
  193. gbcPanel3.gridwidth = 14;
  194. gbcPanel3.gridheight = 4;
  195. gbcPanel3.fill = GridBagConstraints.BOTH;
  196. gbcPanel3.weightx = 1;
  197. gbcPanel3.weighty = 0;
  198. gbcPanel3.anchor = GridBagConstraints.NORTH;
  199. gbPanel3.setConstraints( btBut3, gbcPanel3 );
  200. pnPanel3.add( btBut3 );
  201.  
  202. btBut4 = new JButton( "DO..WHILE" );
  203. btBut4.setForeground(Color.RED);
  204. btBut4.addActionListener(this);
  205. gbcPanel3.gridx = 2;
  206. gbcPanel3.gridy = 7;
  207. gbcPanel3.gridwidth = 14;
  208. gbcPanel3.gridheight = 4;
  209. gbcPanel3.fill = GridBagConstraints.BOTH;
  210. gbcPanel3.weightx = 1;
  211. gbcPanel3.weighty = 0;
  212. gbcPanel3.anchor = GridBagConstraints.NORTH;
  213. gbPanel3.setConstraints( btBut4, gbcPanel3 );
  214. pnPanel3.add( btBut4 );
  215.  
  216. btBut5 = new JButton( "FOR" );
  217. btBut5.setForeground(Color.RED);
  218. btBut5.addActionListener(this);
  219. gbcPanel3.gridx = 2;
  220. gbcPanel3.gridy = 13;
  221. gbcPanel3.gridwidth = 14;
  222. gbcPanel3.gridheight = 4;
  223. gbcPanel3.fill = GridBagConstraints.BOTH;
  224. gbcPanel3.weightx = 1;
  225. gbcPanel3.weighty = 0;
  226. gbcPanel3.anchor = GridBagConstraints.NORTH;
  227. gbPanel3.setConstraints( btBut5, gbcPanel3 );
  228. pnPanel3.add( btBut5 );
  229. gbcPanel0.gridx = 21;
  230. gbcPanel0.gridy = 4;
  231. gbcPanel0.gridwidth = 18;
  232. gbcPanel0.gridheight = 19;
  233. gbcPanel0.fill = GridBagConstraints.BOTH;
  234. gbcPanel0.weightx = 1;
  235. gbcPanel0.weighty = 0;
  236. gbcPanel0.anchor = GridBagConstraints.NORTH;
  237. gbPanel0.setConstraints( pnPanel3, gbcPanel0 );
  238. pnPanel0.add( pnPanel3 );
  239.  
  240. outputArea = new JTextArea(2,10);
  241. gbcPanel0.gridx = 1;
  242. gbcPanel0.gridy = 25;
  243. gbcPanel0.gridwidth = 38;
  244. gbcPanel0.gridheight = 14;
  245. gbcPanel0.fill = GridBagConstraints.BOTH;
  246. gbcPanel0.weightx = 1;
  247. gbcPanel0.weighty = 1;
  248. gbcPanel0.anchor = GridBagConstraints.NORTH;
  249. gbPanel0.setConstraints( outputArea, gbcPanel0 );
  250. pnPanel0.add( outputArea );
  251.  
  252. setContentPane( pnPanel0 );
  253. // pack();
  254. setVisible( true );
  255.  
  256.  
  257.  
  258.  
  259. } // end constructor GridBagDemo
  260.  
  261. public void actionPerformed(ActionEvent event){
  262.  
  263. if(event.getSource() == btBut0)
  264. {String output="";
  265. output += "if (condition) {" + "\nstatements;" + "\n};";
  266. outputArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 18));
  267. outputArea.setBackground(Color.BLUE) ;
  268. outputArea.setText( output );
  269.  
  270. showStatus("You clicked the If button..");
  271.  
  272. }
  273.  
  274.  
  275.  
  276. else if(event.getSource() == btBut4)
  277. { String output="";
  278. output += "do {" + "\nstatements;"+ "\n} while (condition);";
  279. outputArea.setFont(new Font("Serif", Font.BOLD, 18));
  280. outputArea.setBackground(Color.RED) ;
  281. outputArea.setText( output );
  282.  
  283. showStatus("You clicked the Do while button..");
  284. } //end if
  285.  
  286. else if(event.getSource() == btBut1)
  287. {String output="";
  288. output += "if (condition) {" + "\nstatements;" + "\n} else {" + "\nstatements;" + "\n};";
  289.  
  290. outputArea.setFont(new Font("SansSerif", Font.PLAIN, 14));
  291. outputArea.setBackground(Color.BLUE) ;
  292. outputArea.setText( output );
  293.  
  294. showStatus("You clicked the If..else button..");
  295. }//end if
  296.  
  297. else if(event.getSource() == btBut3)
  298. {String output="";
  299. output += "while (condition) {"+ "\nstatements;" + "\n};";
  300. outputArea.setFont(new Font("Monospaced", Font.ITALIC, 12));
  301. outputArea.setBackground(Color.RED) ;
  302. outputArea.setText( output );
  303.  
  304.  
  305. showStatus("You clicked the While button..");
  306. } //end if
  307. else if(event.getSource() == btBut2)
  308. { String output="";
  309. output += "switch (condition) {" + "\ncase ABC:" + "\nstatements:" + "\n/*falls through" +
  310. "\ncase DEF:" + "\nstatements;" + "\nbreak;" + "\ncase XYZ:" +
  311. "\nstatements;" + "\nbreak;" + "\ndefault:" + "\nstaments;" + "\nbreak;" + "\n};";
  312. outputArea.setFont(new Font("Serif", Font.ITALIC, 16));
  313. outputArea.setBackground(Color.GREEN) ;
  314. outputArea.setText( output );
  315.  
  316. showStatus("You clicked the Switch button..");
  317. } //end if
  318.  
  319. else if(event.getSource() == btBut5)
  320. {String output="";
  321. output += "for (initialization; condition;" + "\nupdate)" + " {" + "\nstatements;" +
  322. "\n};";
  323. outputArea.setFont(new Font("Aerial", Font.BOLD + Font.ITALIC, 14));
  324. outputArea.setBackground(Color.RED) ;
  325. outputArea.setText( output );
  326.  
  327. showStatus("You clicked the For button..");
  328. }//end if
  329.  
  330.  
  331. }//end method action performed
  332.  
  333. public static void main( String args[] )
  334.  
  335. {Frame f = new Frame(); //Create a new frame object
  336.  
  337. MenuTest menuTest = new MenuTest(); //Create an instance of DrawHouse
  338.  
  339. f.add( menuTest );
  340. f.setSize(230,440);
  341. f.setVisible(true);
  342.  
  343. //An "anonymous" inner class used to close the window
  344. f.addWindowListener( new WindowAdapter()
  345. {
  346. public void windowClosing(WindowEvent we)
  347. {
  348. System.exit(0);
  349. }///end window closing method
  350. }// end window adapter method
  351. );// end parameter argument
  352.  
  353. menuTest.init(); //Call the applet's init method
  354.  
  355. } //End of main
  356.  
  357.  
  358.  
  359. } // end class MenuTest

:cry: :cry: :-|
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC