Implementing JMenuBar in a JFrame

3

I'm having trouble trying to insert a JMenuBar component into my main frame. I do not get any problems running the code, but the options bar in the program does not appear. What is missing?!

Principal.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());
    }

}

Tela.java

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;
    }
}

Menu.java

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);
    }
}

Barra.java

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);
    }
}
    
asked by anonymous 24.08.2015 / 21:18

2 answers

1

You should call the setVisible() method only after all changes to the screen have finished. Therefore, your main gets:

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 Screen class. I suggest removing extends JFrame if you want to continue using composition.

    
22.12.2015 / 10:02
0

I think it's missing calling the repaint( ) method to apply changes to the screen

    
24.08.2015 / 21:46