Trouble creating menu bar
I'm very green when it comes to writing code and I'm trying to create a menu bar and pull down menus. The menu bar contains three options: File, Edit and Help.
The File menu contains four options: New, Open, Close and Send To. Among these four, the Send To entry itself is a menu with the following three options: Mail Recipient, Exchange Server and Floppy Disk
The Edit menu has the following entries: Cut, Copy and Paste.
The Help menu has two options: Contact Us and About Software
Any help would be greatly appreciated.
Take the tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
Here's what I have so far - but when I compiled I have 18 errors and I have no idea where I'm wrong - I'm lost with this stuff please help!
public class JMenu {
JMenuBar createMenuBar() {
JMenuBar bar= new JMenuBar();
bar.add(createFileMenu());
bar.add(createEditMenu());
bar.add(createHelpMenu());
return bar;
}
JMenu createFileMenu()
{
JMenu menu= new JMenu("File");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
JMenu createEditMenu()
{
JMenu menu= new JMenu("Edit");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
JMenu createHelpMenu()
{
JMenu menu= new JMenu("Help");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
}
A couple of things stand out: You don't have the necessary improt statements at the start, and you invoke methods like createNewMenuItem() that aren't defined anywhere.
Have you worked through the material pointed to in reply 1?
When you post code you should use the "code" tags. They are described here: http://forum.java.sun.com/help.jspa?sec=formatting
The idea is that you put [code] at the start of your code and [/code] at the end. That way your code remains readable when it appears in the forum.
For instance:
[code]if(tagsUsed) {
outputGood = true;
}[/code]
will appear like:
if(tagsUsed) {
outputGood = true;
}
(If no-one has replied to our last post you can edit it and put the code tags in - you will also have to fix the indenting if you do that. If you get compiler messages it's also a good idea to copy and post them along with the code.)
Ok - Here goes -
public class JMenu {
JMenuBar createMenuBar() {
JMenuBar bar= new JMenuBar();
bar.add(createFileMenu());
bar.add(createEditMenu());
bar.add(createHelpMenu());
return bar;
}
JMenu createFileMenu()
{
JMenu menu= new JMenu("File");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
JMenu createEditMenu()
{
JMenu menu= new JMenu("Edit");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
JMenu createHelpMenu()
{
JMenu menu= new JMenu("Help");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
Please help as I'm completely confused
You only error, besides perhaps forgetting to import and not defining all the methods,
is calling your class JMenu. Bad idea. Don't name you class after something
in the API you want to use. It will only end in confusion. The following compiles.
import javax.swing.*;
public class MenuFactory {
JMenuBar createMenuBar() {
JMenuBar bar= new JMenuBar();
bar.add(createFileMenu());
bar.add(createEditMenu());
bar.add(createHelpMenu());
return bar;
}
JMenu createFileMenu() {
JMenu menu= new JMenu("File");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
JMenuItem createNewMenuItem() {
return null;
}
JMenuItem createOpenMenuItem() {
return null;
}
JMenuItem createCloseMenuItem() {
return null;
}
JMenuItem createSendToMenu() {
return null;
}
JMenu createEditMenu() {
JMenu menu= new JMenu("Edit");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
JMenu createHelpMenu() {
JMenu menu= new JMenu("Help");
menu.add(createNewMenuItem());
menu.add(createOpenMenuItem());
menu.add(createCloseMenuItem());
menu.add(createSendToMenu());
return menu;
}
}
Suggestion: start smaller: one menu, one menu item. If you start with
three menus and a dozen menu items, you're going to make the same
mistake a dozen times. Get the simplest possible thing up and working
before you expand. You will be more productive and more successful.
