O
O
olololosh2015-03-18 23:27:37
Java
olololosh, 2015-03-18 23:27:37

Why is the method not being called?

There is a class with menu drawing. There is also a class with drawing JFrame. In the frame class I want to call the menu method:
frame.setJMenuBar(menuBar);
frame.setPreferredSize(new Dimension(800, 600));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

//Method menuCreator
MenuCreator menuCreator = new MenuCreator();
menuCreator.menuCreator();

swears at 1 line frame.setJMenuBar(menuBar);
writes cannot find symbol - variable menuBar
what does he not like?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Timur, 2015-03-20
@olololosh

olololosh : I didn't get it - are you just accessing a variable from another class? ("menuBar"). This is not possible in Java.
First, the menuCreator() method of the MenuCreator class is static. Accordingly, you need to access it MenuCreator.menuCreator(); But it's not very pretty. Therefore, I would advise you to remove the static modifier.
Secondly, if you want to get some variable from another class. it must be declared in the body of the class and not the method, as you have. (I'm talking about menuBar)
Thirdly, to get this variable, it is desirable to make a "getter" for it, which will return it.
Fifth, even if you had access to the menuBar variable, this line frame.setJMenuBar(menuBar); would throw a NullpointerException since menuCreator.menuCreator() is called later.
In short, such a hodgepodge is horror.
Here's what it could look like (but you need to look at the whole code of course):

package application;
import java.awt.Font;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

// Этот клас может создавать нужные вам меню
public class MenuCreator {

  public JMenuBar createMenuBar() {
    // Create menuBar
    JMenuBar menuBar = new JMenuBar();
    return menuBar;
  }

  public JMenu createMenu() {
    // Create font
    Font font = new Font("Verdana", Font.PLAIN, 11);
    // Create File Menu
    JMenu fileMenu = new JMenu("File");
    fileMenu.setFont(font);
    return fileMenu;
  }
}

package application;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenuBar;

// в этом классе у вас происходит создание фрейма и всякая другая логика
public class MyMainClassWithJFrame {

  public static void main(String[] args) {

    // ваш какой-то код...
    JFrame frame = new JFrame();

    // создаем обьект MenuCreator
    // теперь мы можем создавать необходимые меню
    MenuCreator menuCreator = new MenuCreator();
    // создаем новый обьект JMenuBar
    JMenuBar menuBar = menuCreator.createMenuBar();

    frame.setJMenuBar(menuBar);
    frame.setPreferredSize(new Dimension(800, 600));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    // ваш какой-то код..
  }
}

Yes, and try to name the methods somehow more logically. :) Good luck!

I
IceJOKER, 2015-03-18
@IceJOKER

What he doesn't like, he wrote to you:
cannot find symbol - variable menuBar

K
Kostya Bakay, 2015-03-19
@kostyabakay

Maybe you missed this moment?
JMenuBar menuBar = new JMenuBar();
It is possible that the method does not see this variable. Try to do everything in one method or give that method the desired parameter. Here you can see in more detail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question