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);
}
}
I just tested the call to the repaint() method, but with no success regarding the visibility of the options bar.
– Guilherme Silva Sousa