Trying to create a MenuBar using Java.swing, and created an object of JMenu(), but it wont let me add It to the frame and not sure why?

196 views Asked by At
public class Menu  {

    private JMenu borders;

    //constructor
    public Menu()
    {

        //crate object
        //this.menuBar = new JMenuBar();

        //------create menu items----

        //menu item for borders
        borders = new JMenu("Border");

        // add border options
        borders.setMnemonic(KeyEvent.VK_U);

        //add border options
        borders.add(new JMenuItem("Etched"));
        borders.add(new JMenuItem("Raised"));
        borders.add(new JMenuItem("Matte"));
        borders.add(new JMenuItem("Tilted"));
        borders.add(new JMenuItem("Compounded"));

        JMenuItem quit = new JMenuItem("Quit");
        quit.setMnemonic(KeyEvent.VK_Q);

        borders.add(quit);

        //set Accelerator
        quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));

        //add action listener
        //@Override
        quit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }

        });


    }

}

public class DrawingFrame extends JFrame{
    protected CanvasEditor canvasEditor;
    protected DrawingCanvas drawingCanvas;
    private Menu menu;



    public DrawingFrame()
    {
        this.setTitle("Drawing Application");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        drawingCanvas = new DrawingCanvas();
        drawingCanvas.setMinimumSize(new Dimension(300,700));
        this.add(drawingCanvas);

        JPanel toolBarPanel = getToolBarPanel();

        this.add(drawingCanvas,BorderLayout.CENTER);

        this.add(toolBarPanel,BorderLayout.SOUTH);

        //create menu from class
        this.menu = new Menu();

        //create menu bar
        JMenuBar menuBar = new JMenuBar();

        //add menu to menuBar
        menuBar.add(menu);

error: cannot resolve method 'add'

not sure why it won't accept the add method

The drawingFrame class accepts objects from canvasEditor and DrawingCanvas but it doesn't accept objects from Menu class.

I want to create a menuBar and add a menu to it, and then have it on the same frame -- drawing Frame

pretty sure it's a simple fix, but not sure how to fix it

1

There are 1 answers

3
camickr On BEST ANSWER
public class Menu  {
    private JMenu borders;

You create a class called Menu and create an instance of a JMenu in this class.

    this.menu = new Menu();

    //create menu bar
    JMenuBar menuBar = new JMenuBar();

    //add menu to menuBar
    menuBar.add(menu);

You then create an instance of the Menu class and attempt to add it to the JMenuBar, which can't be done because the Menu class is NOT a Component.

Instead you want to add the borders variable to the menubar.

This means you need to create a method in your Menu class like:

public JMenu getJMenu()
{
    return borders;
}

Then you use:

//menuBar.add(menu);
menuBar.add( menu.getJMenu() );