Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: menubars and menu items

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default menubars and menu items

    I am learning Java on my own
    I am not in school and my progress is slow but I think , sure

    This forum has been extremely useful to me and thanks

    Here is the code I am having a problem with. Please help me to fix it
    Thanks


    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;

    public class Menu1 extends JFrame implements ActionListener
    {
    // opens jframe
    private static final long serialVersionUID = 1L;

    public static void main (String[] args)
    {
    //opens main
    new Menu1().setVisible(true);
    }
    private Menu1()
    {
    super("Tutorial = Rhoen Menu #1");
    setSize (600,600);
    setResizable (false);
    setDefaultCloseOperation (EXIT_ON_CLOSE);

    JButton button = new JButton ("Click me");
    button.addActionListener(this);
    button.setActionCommand("Click");

    JMenuBar bar = new JMenuBar();
    JMenu file = new JMenu ("File");
    JMenuItem newMenuItem = new JMenu("New");
    JMenuItem saveMenuItem = new JMenu("Save");
    JMenuItem close = new JMenuItem("Exit");
    close.addActionListener(this) ;

    JMenuItem extra = new JMenu("Extra");
    JMenuItem hello = new JMenuItem("Hello");
    JMenuItem hello2 = new JMenuItem("Hello 2");

    extra.add(hello);
    extra.add(hello2);

    file.add (newMenuItem);
    file.add(save);
    file.add(extra);
    file.addSeparator();
    file.add(close);

    bar.add(file);
    add(button, BorderLayout.WEST);
    setJMenuBar (bar);
    }

    public void actionPerformed(ActionEvent e)
    {
    String name = e.getActionCommand();
    if (name.equals("Click"))
    {
    System.out.println("Click me");
    }
    else if (name.equals("Exit"))
    {
    System.out.println("Closed");
    System.exit(0);
    }
    }
    }


    When I run it I get the following message
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    save cannot be resolved

    at Menu1.<init>(Menu1.java:48)
    at Menu1.main(Menu1.java:20)

    What does this mean

    I notice a X beside the line (file.add(save)
    and save does not appear in the menu created


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: menubars and menu items

    Hello.
    Did you read what the error message says?
    It says "save" cannot be resolved?
    Can you check where is "save" defined in your program?
    Also, double-check once if you are creating the menu bar rightly or not? I would say you are not.
    I am giving you chance to figure out on your own. So try your best.

    Syed.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: menubars and menu items

    You have been asked in the past to use code tags when posting code, and provided this link for finding help on their use. What seems to be the trouble?

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: menubars and menu items

    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
     
    public class Menu1 extends JFrame implements ActionListener {
    	// opens jframe
    	private static final long serialVersionUID = 1L;
     
    	public static void main(String[] args) {
    		// opens main
    		new Menu1().setVisible(true);
    	}
     
    	private Menu1() {
    		super("Tutorial = Rhoen Menu #1");
    		setSize(600, 600);
    		setResizable(false);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    		JButton button = new JButton("Click me");
    		button.addActionListener(this);
    		button.setActionCommand("Click");
     
    		JMenuBar bar = new JMenuBar();
    		JMenu file = new JMenu("File");
    		JMenuItem newMenuItem = new JMenu("New");
    		JMenuItem saveMenuItem = new JMenu("Save");
    		JMenuItem close = new JMenuItem("Exit");
    		close.addActionListener(this);
     
    		JMenuItem extra = new JMenu("Extra");
    		JMenuItem hello = new JMenuItem("Hello");
    		JMenuItem hello2 = new JMenuItem("Hello 2");
     
    		extra.add(hello);
    		extra.add(hello2);
     
    		file.add(newMenuItem);
    		file.add(save);
    		file.add(extra);
    		file.addSeparator();
    		file.add(close);
     
    		bar.add(file);
    		add(button, BorderLayout.WEST);
    		setJMenuBar(bar);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		String name = e.getActionCommand();
    		if (name.equals("Click")) {
    			System.out.println("Click me");
    		} else if (name.equals("Exit")) {
    			System.out.println("Closed");
    			System.exit(0);
    		}
    	}
    }

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    save cannot be resolved to a variable

    at Menu1.<init>(Menu1.java:45)
    at Menu1.main(Menu1.java:17)
    "save" has not been declared isn't it?

    file.add(saveMenuItem);
    //instead of
    file.add(save);
    //maybe?

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: menubars and menu items

    Thanks I will study this code and see how I can improve my understanding of the source of the error

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: menubars and menu items

    Learn to understand the information given in the error report:

    This basically says a variable named save is being used somewhere outside of its scope, which can mean it was never declared in the first place.
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    save cannot be resolved

    This tells you where the compiler was working when it ran into the problem described above. (Basically the class and line number of where to look)
    at Menu1.<init>(Menu1.java:48)
    at Menu1.main(Menu1.java:20)

    I assume you understand a variable must be declared before it can be used, if not, a search on variables will provide plenty of information

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: menubars and menu items

    Here is a little details I must review before I ask again

    Thanks

Similar Threads

  1. Java to HTML, enable user to enter a number of menu items
    By @passat in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 3rd, 2012, 08:42 AM
  2. JComboBox help again but different help 75+ items in it
    By derekxec in forum AWT / Java Swing
    Replies: 7
    Last Post: August 27th, 2011, 07:53 PM
  3. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 09:19 AM
  4. misalignment of list items
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: March 15th, 2010, 09:33 AM
  5. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 06:07 AM