Implementation of Jmenubar in a Jframe

Asked

Viewed 797 times

3

I’m having trouble trying to insert a component JMenuBar to my main frame. I don’t get any kind of problem when executing the code, but the options bar is not displayed in the program. What is missing?!

Main java.

public class Principal {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Tela telaPrincipal = new Tela("Home", 700, 500);
    telaPrincipal.onVisible();
    Tela about = new Tela("Sobre", 500, 400);
    about.offVisible();

    ItemMenu menuItem = new ItemMenu("Sair");
    Menu menu = new Menu("Menu");
    Barra barraMenu = new Barra();
    menu.setMenuItem(menuItem.getItem());
    barraMenu.putMenu(menu.getMenu());
    telaPrincipal.setBar(barraMenu.getBar());
    }

}

Java screen.

package calculadora;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class Tela extends JFrame{

    private JFrame tela;

    public Tela(String title, int largura, int altura){

        tela = new JFrame(title);
        tela.setSize(largura,altura);
        tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void onVisible(){
        tela.setVisible(true);
    }
    public void offVisible(){
        tela.setVisible(false);
    }

    public void setPanel(JPanel painel){
        tela.add(painel);
    }

    public void setBar(JMenuBar barraMenu){
        tela.setJMenuBar(barraMenu);
    }
}

Itemmenu.java

package calculadora;
import javax.swing.JMenuItem;

public class ItemMenu {

    JMenuItem menuItem;

    public ItemMenu(String name){

        menuItem = new JMenuItem(name);
    }

    public JMenuItem getItem(){
        return menuItem;
    }
}

Java menu.

package calculadora;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class Menu {

    private JMenu menu;

    public Menu(String name){
        menu = new JMenu(name);
    }

    public JMenu getMenu(){
        return menu;
    }

    public void setMenuItem(JMenuItem item){
        menu.add(item);
    }
}

Java bar.

package calculadora;
import javax.swing.JMenuBar;
import javax.swing.JMenu;

public class Barra {

    JMenuBar barra;

    public Barra(){
        barra = new JMenuBar();
    }

    public JMenuBar getBar(){
        return barra;
    }

    public void putMenu(JMenu menu){
        barra.add(menu);
    }
}

2 answers

2


You must call the method setVisible() just after finishing all the screen changes. So, your main stays:

public static void main(String[] args) {
    // TODO code application logic here
    Tela telaPrincipal = new Tela("Home", 700, 500);
    Tela about = new Tela("Sobre", 500, 400);
    about.offVisible();

    ItemMenu menuItem = new ItemMenu("Sair");
    Menu menu = new Menu("Menu");
    Barra barraMenu = new Barra();
    menu.setMenuItem(menuItem.getItem());
    barraMenu.putMenu(menu.getMenu());
    telaPrincipal.setBar(barraMenu.getBar());
    telaPrincipal.onVisible();
}

Another thing: you seem to be mixing inheritance with composition in your Canvas class. I suggest you remove the extends JFrame if you want to continue using composition.

0

I believe it is missing to call the method repaint( ) to apply the changes on the screen

  • I just tested the call to the repaint() method, but with no success regarding the visibility of the options bar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.