0

Hey guys i am trying to make a menu bar to my JFrame from other class, but it siply denies to work ! I am on a deadend...

I really do not know what is going wrong, i tried some other ways, but it didnt worked...

The code is this :

  public Engine(int width, int height) {
    System.out.println("TEST ENGINE CLAASS");
    setTitle("Battleship Board - Place Your Ships");
    setSize(width, height);
    setLocation(624, 373);
    setDefaultCloseOperation(3);
    setResizable(false);
    setVisible(true);
    setLayout(null);

    System.out.println("TEST ENGINE clas2");
    Menu menu = new Menu(this);
    super.setJMenuBar(menu.getMenuBar());




    setDefaultCloseOperation(3);
}

and at Menu.class:

public class Menu implements ActionListener {
    private Engine cc;
    private JMenuBar menuBar;
    private JMenu game;
    private JMenu help;
    private JMenuItem aboutItem;
    private JMenuItem quitItem;
    private JMenuItem newGameItem;

    public Menu (Engine cc) {
        System.out.println("testing menu class");
        this.cc = cc;
        this.menuBar = new JMenuBar();
        this.game = new JMenu("Game");
        this.help = new JMenu("Help");

        makeGameMenu();
        makeAboutMenu();


    }



   private void makeGameMenu() {
        System.out.println("Making game menu");
        this.newGameItem = new JMenuItem("New Game");
        this.game.add(this.newGameItem);
        this.newGameItem.addActionListener(this);
        this.game.addSeparator();

        this.quitItem.addActionListener(this);


        this.game.add(this.quitItem);
         this.menuBar.add(this.game);

    }

   private void makeAboutMenu() {
        this.aboutItem = new JMenuItem("About");
        this.help.add(aboutItem);
        this.menuBar.add(this.help);


    }

   public void actionPerformed(ActionEvent event) {
       JMenuItem source = (JMenuItem)event.getSource();

       //If user clicks new game then:
       if (source == this.newGameItem) {
           this.cc.newGame();
       }
       //If user clicks Quit then:
       else if (source == this.quitItem) {
           System.exit(0);
       }
       //If user clicks Help - About then:
       else if (source == this.aboutItem) {
           JOptionPane.showMessageDialog(null, "This Battleship Game was created by Manos Kontakis for Object Oriented Programming Lab", "About", 1);
       }
   }

   public JMenuBar getMenuBar()
  {
    return this.menuBar;
  }


}
Manos
  • 1,471
  • 1
  • 28
  • 45

2 Answers2

2

The JMenuItem quitItem has not been initialized anywhere, so an exception will be thrown before the JMenuBar can be added to the JFrame:

quitItem = new JMenuItem("Quit Game");
quitItem.addActionListener(...);

Aside: Avoid the use of absolute positioning (null layout) and always use a layout manager.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • True fixed it, but the problem is that it doesnt even goes to the Menu Class. the problem is here: super.setJMenuBar(menu.getMenuBar());.... Do you know what is wrong there ? – Manos Apr 09 '13 at 18:16
  • What is the signature of the `GameEngine` class? – Reimeus Apr 09 '13 at 18:21
  • Sorry mistake, it is Engine, it is correct on my code this is not the fault (Copied from wrong package). – Manos Apr 09 '13 at 18:25
0

I think you have to extend from JMenuBar and add the components to "this".

Aitor Gonzalez
  • 125
  • 1
  • 14