Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By ninjakreborn

Thread: Strange errors in simple code.

  1. #1
    ninjakreborn is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Strange errors in simple code.

    Java Code:
    /****
     * Author: Joyel Puryear
     * Website: http://www.joyelpuryear.com
     * Title: Morrowind Swiss Army Knife
     ****/
    package morrowind.swiss.army.knife;
    
    
    /***** IMPORTS *****/
    import javax.swing.*;
    import java.awt.event.*;
    
    public class MorrowindSwissArmyKnife {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            /***** CREATE FRAME *****/
            JFrame frame = new JFrame("Morrowind Swiss Army Knife - Author Joyel Puryear (http://www.joyelpuryear.com)");
            frame.setVisible(true);
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            /***** CREATE MENU BAR *****/
            
            JMenuBar menubar = new JMenuBar();
            frame.setJMenuBar(menubar);
            
            /***** CREATE FILE MENU *****/
                    
            // File menu item.
            JMenu file = new JMenu("File");
            menubar.add(file);
            
            // Exit sub item within File menu.
            JMenuItem exit = new JMenuItem("Exit");
            file.add(exit);
            
            class exitaction implements ActionListener {
                public void actionPerformed (ActionEvent e) {
                    System.exit(0);
                }
            }
            exit.addActionListener(new exitaction());
            
            /***** CREATE HELP MENU *****/
            JMenu help = new JMenu("Help");
            menubar.add(help);
            JMenuItem about = new JMenuItem("About");
            help.add(about);
            
        }
    }
    This works great the "first" time it is "run". It opens, I exit. Second time it shows less menu items, third it shows none. It's like each time I "run" it more and more menu options disappear. I tried moving around in what order stuff was done. Any feedback is appreciated, thanks.

  2. #2
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: Strange errors in simple code.

    I tested it on my machine and nothing showed up in the menu bar. So I realized that you had added the menu bar before you defined the items. Also, the setVisible needed to be at the end. Here is the new code:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class MorrowindSwissArmyKnife
    {
    
    	/**
    	 * @param args
    	 *            the command line arguments
    	 */
    	public static void main(String[] args)
    	{
    
    		/***** CREATE FRAME *****/
    		JFrame frame = new JFrame(
    				"Morrowind Swiss Army Knife - Author Joyel Puryear (http://www.joyelpuryear.com)");
    		
    		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		/***** CREATE MENU BAR *****/
    
    		JMenuBar menubar = new JMenuBar();
    		JMenu file = new JMenu("File");
    		menubar.add(file);
    
    		// Exit sub item within File menu.
    		JMenuItem exit = new JMenuItem("Exit");
    		file.add(exit);
    
    		class exitaction implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				System.exit(0);
    			}
    		}
    		exit.addActionListener(new exitaction());
    
    		/***** CREATE HELP MENU *****/
    		JMenu help = new JMenu("Help");
    		menubar.add(help);
    		JMenuItem about = new JMenuItem("About");
    		help.add(about);
    		frame.setJMenuBar(menubar);
    		frame.setVisible(true);
    
    	}
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Strange errors in simple code.

    ninjakreborn, please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ninjakreborn is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Strange errors in simple code.

    Quote Originally Posted by phyzix5761 View Post
    I tested it on my machine and nothing showed up in the menu bar. So I realized that you had added the menu bar before you defined the items. Also, the setVisible needed to be at the end. Here is the new code:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class MorrowindSwissArmyKnife
    {
    
    	/**
    	 * @param args
    	 *            the command line arguments
    	 */
    	public static void main(String[] args)
    	{
    
    		/***** CREATE FRAME *****/
    		JFrame frame = new JFrame(
    				"Morrowind Swiss Army Knife - Author Joyel Puryear (http://www.joyelpuryear.com)");
    		
    		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		/***** CREATE MENU BAR *****/
    
    		JMenuBar menubar = new JMenuBar();
    		JMenu file = new JMenu("File");
    		menubar.add(file);
    
    		// Exit sub item within File menu.
    		JMenuItem exit = new JMenuItem("Exit");
    		file.add(exit);
    
    		class exitaction implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				System.exit(0);
    			}
    		}
    		exit.addActionListener(new exitaction());
    
    		/***** CREATE HELP MENU *****/
    		JMenu help = new JMenu("Help");
    		menubar.add(help);
    		JMenuItem about = new JMenuItem("About");
    		help.add(about);
    		frame.setJMenuBar(menubar);
    		frame.setVisible(true);
    
    	}
    }

    Thanks. That helped a lot. I am an 8 year experienced programmer. I did a lot with C++ but just now trying to learn Java. I knew it had to be something simple. So I learned a few things about menu items, and frames. Thanks again for taking the time to assist me, I appreciate that. Now I can continue building this app and learning more. Thanks Again!

  5. #5
    ninjakreborn is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Strange errors in simple code.

    Quote Originally Posted by DarrylBurke View Post
    ninjakreborn, please go through the Forum Rules -- particularly the third paragraph.

    db
    Sorry about that. I'm familiar with that from other forums. I'll make sure I don't do that again.
    DarrylBurke likes this.

Similar Threads

  1. applet loading errors strange?!
    By newbie_jar in forum Java Applets
    Replies: 1
    Last Post: 11-08-2011, 05:35 PM
  2. Strange problem occurring with my code
    By Tech2011 in forum Eclipse
    Replies: 5
    Last Post: 10-13-2011, 10:30 AM
  3. Colorchooser - Strange Exceptions in simple code.
    By spec8320 in forum AWT / Swing
    Replies: 3
    Last Post: 04-09-2011, 04:40 PM
  4. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  5. strange code
    By tghn2b in forum New To Java
    Replies: 3
    Last Post: 12-22-2008, 11:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •