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