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
You create a class called
Menuand create an instance of aJMenuin this class.You then create an instance of the
Menuclass and attempt to add it to the JMenuBar, which can't be done because theMenuclass is NOT aComponent.Instead you want to add the
bordersvariable to the menubar.This means you need to create a method in your
Menuclass like:Then you use: