Hey. Im having problems adding my Jmenubar to my board. It compiles fine just wont display. Any advice really appreciated. Thanks, Pd

import java.awt.*;
import javax.swing.*;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.Font;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.WindowConstants;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;


public class Board extends JFrame
{

    public JPanel[][] squares;
    public JFrame boardFrame;
    public Container container;





    public Board()
    {

        boardFrame = new JFrame("MyDraughts");
        setJMenuBar(MyMenu());
        container = boardFrame.getContentPane();
        container.setLayout(new GridLayout(8,8));
        create_squares();
        boardFrame.setSize(400,450);
        boardFrame.setVisible(true);
    }


    public void create_squares()
    {

        squares = new JPanel[8][8];
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
               JPanel p = new JPanel();
                p.setBackground(setColor(i,j));
                squares[i][j]=p;
                container.add(p);
            }
        }
    }

    public Color setColor(int x, int y)
    {
        if((x+y)%2 == 0)
            return Color.WHITE;
        else
            return Color.BLACK;
    }

    private JMenuBar MyMenu()

    {

			// Create a menu Bar
			JMenuBar MyMenuBar = new JMenuBar();


			JMenu fileMenu = new JMenu("File");

			//Declare Menu Items and add them to the first menu

			JMenuItem newMenuItem = new JMenuItem ("New Game");

			fileMenu.add(newMenuItem);
			//fileMenu.addSeparator();


			//ButtonGroup myButtonGroup = new ButtonGroup();

			MyMenuBar.add(fileMenu);
			return MyMenuBar;


	}




}

I mean i think it is something small that i am missing, but i cannot for the life of me figure it out!

Your JMenuBar method is working properly it's just for the wrong JFrame, you have two. The one you created and the one by inheritance.

You dont need the extends JFrame since you created your own JFrame.

So make boardFrame.setJMenuBar(....)

When creating the menu bar have boardFrame.setJMenuBar(...)

public Board()
    {

        boardFrame = new JFrame("MyDraughts");
        boardFrame.setJMenuBar(MyMenu());
    }

and same project as Xav123? :?

Edited 7 Years Ago by .11: n/a

This article has been dead for over six months. Start a new discussion instead.