I have a SIMPLE JFrame class and a SIMPLE JMenuBar class. I want the JFrame class to call/instantiate the JMenuBar that I have written, but I don't think I have the JMenuBar set up right. I get the following two errors when I try to compile it:
That often suggests your {} are not matching up somewhere. Make sure you haven't got an extra one somewhere or you're not are missing a closing one and that they are all where you intended them to be.
Edit: Or you can just wait for Stephan to tell you where the problem is
Craig Boyd wrote:Am I not calling the MyMenuBar correctly?
I assume you're trying to create a new one? Then you need a "new".
Edit: although that still won't work, since MyMenuBar isn't a JMenuBar.
So you can either do this:
Or, and I think this is more sensible, have MyMenuBar extend JMenuBar, and swap your main() method for a constructor. For example:
Fair enough. I can not seem to find the right file extension to use so I am including the code for both here. They are both VERY small.
They both compile and run w/the exception of the menu bar not showing up.
MyJFrame.java
You haven't actually done what I suggested. You've made MyMenuBar extend JMenuBar, but you haven't made the other changes I said would be necessary:
- change the main() method into a constructor
- add the menus to the MyMenuBar object you're creating, rather than creating another new menu bar and adding them to that.
So the problem at the moment is that two JMenuBars get created. One has all the items added to it, and the other is what you're adding to the JFrame. My last post gave an extract of what needs doing.
OK. I got both classes compiling, but I think I am still creating two instances of the JMenuBar and the one I am getting is not the one I want.
Anyone have any idea where I am going astray?
You've gone back to how you had it originally (except your main method is now called menuBar).
Change menuBar into a constructor and remove the return statement. Constructors don't return anything.
Also you can remove lines 9 and 10. You don't need to create a JMenuBar.