0

I am just started learning Java and I've been reading through this documentation. I don't like to copy a bunch of code and paste it. So I have been trying to work my way through the documentation one thing at at time.

I already have a working JFrame and decided I would start by adding a menu.

HERE IS MY CODE:

package mainframe;

import javax.swing.*;

public class menuBar extends JMenuBar {
    JMenuBar mainMenu = JMenuBar("Menu");
}

MY ERROR:

error: cannot find symbol
  JMenuBar mainMenu = JMenuBar("Menu");
  symbol:   method JMenuBar(String)
  location: class menuBar
1 error

So anyways. I am not really sure what the "cannot find symbol error" means. Maybe I am searching wrong. But every time I Google it it takes me to more complex questions with no clear answer. Any advice as to what I am doing wrong and or to what the cannot find symbol error means would be very much appreciated. Thanks in advance.

khollenbeck
  • 16,028
  • 18
  • 66
  • 101

3 Answers3

1

The constructor for JMenuBar never takes any arguments. Also remember to use the new keyword when you instantiate (create an instance of) a new object. Consider using the following code:

JMenuBar mainMenu = new JMenuBar();
JMenu fileMenu = new JMenu("File");
mainMenu.add(fileMenu);
SimonT
  • 2,219
  • 1
  • 18
  • 32
  • What is mainMenu.add() referenceing? Is mainMenu suppose to represent the package? – khollenbeck May 28 '13 at 22:36
  • @KrisHollenbeck JMenuBar.add(JMenu) adds a JMenu to the JMenuBar. When you create the JMenuBar in the first line, it is initially empty. You build a JMenu (adding JMenuItems to it), and then add that JMenu to the JMenuBar. – SimonT May 28 '13 at 22:39
  • Okay the reason I ask is because I get "package mainMenu does not exist" on the last line. Anyways. Thanks for the help. – khollenbeck May 28 '13 at 22:41
1

In response to your particular code here, I suggest that you do not extend the JMenuBar class. You may have seen it in many tutorials or examples where the JFrame class is extended, although that is considered bad practice. To add a JMenuBar to your window, I would suggest doing the following:

public class MyProgram {
    JFrame frame;
    public MyProgram() {
        ...
        frame = new JFrame();
        JMenuBar mainMenu = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(new JMenuItem("Open..."));
        mainMenu.add(fileMenu); // adds a single JMenu to the menubar
        frame.setJMenuBar(mainMenu); // adds the entire menubar to the window
        ...
        frame.setVisible();
        ...
    }

The only reason you would extend the JMenuBar class would be if you wanted to make a class that had additional functionality in terms of methods defined in your subclass, but that seems unlikely especially given the fact that you're just learning Swing.

Community
  • 1
  • 1
SimonT
  • 2,219
  • 1
  • 18
  • 32
  • 1
    You shouldn't extend `JFrame` either; that is bad practice. – tckmn May 28 '13 at 22:52
  • Ah my bad, if I'd paid closer attention I wouldn't have put the `extends JFrame` there. That also makes it hard to understand the origins of `add` and how `addActionListener(this)` fails while `addMouseListener(this)` works. – SimonT May 28 '13 at 22:57
  • Okay, thanks. I really appreciate all the time you took to explain this to me. – khollenbeck May 29 '13 at 00:49
0
JMenuBar mainMenu = JMenuBar("Menu");

should be

JMenuBar mainMenu = new JMenuBar("Menu");

You forgot the new keyword. You must always use new when creating a new object with a constructor. Otherwise, Java will think that it is a method, which it is not.

Furthermore, if you look at the documentation here. you will find that JMenuBar's constructor does not take any arguments. Therefore, don't pass anything:

JMenuBar mainMenu = new JMenuBar();
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Okay.. well that at least got me a different error. ----- error: constructor JMenuBar in class JMenuBar cannot be applied to given types; – khollenbeck May 28 '13 at 22:29
  • @KrisHollenbeck That means that you aren't giving the right arguments. Are you *sure* that `JMenuBar`'s constructor accepts a `String`? – tckmn May 28 '13 at 22:30
  • The `JMenuBar` class only has one constructor, and it doesn't take any arguments. – SimonT May 28 '13 at 22:31
  • No I think I just threw that in there when I was trying to get it to work. I believe it is just a container for the actual menu. Thanks – khollenbeck May 28 '13 at 22:32
  • Kris, regarding your error `constructor JMenuBar in class JMenuBar cannot be applied to given types`, that is possibly because you are trying to extend the `JMenuBar` class without implementing your own constructor. – SimonT May 28 '13 at 22:51