SOLVED Forgot to instantiate my menuBar...
I have extended the class JMenuBar
, and also the class JFrame
. However, when I setJMenuBar(menubar)
, the MenuBar does not appear in the Frame
when testing.
Here's my code:
Class Frame:
import javax.swing.JFrame;
public class Frame extends JFrame{
public static MenuBar menubar;
public Frame(){
setTitle("LoL Teamcomps");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menubar);
}
}
Class MenuBar:
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.lolteamcomps.code.components.MenuItem;
public class MenuBar extends JMenuBar{
public MenuBar(){
JMenuItem players = new JMenuItem("Players");
JMenuItem champions = new JMenuItem("Champions");
JMenuItem newPlayer = new JMenuItem("Add new");
JMenuItem newChampion = new JMenuItem("Add new");
players.add(newPlayer);
champions.add(newChampion);
add(players);
add(champions);
}
}
And in my function called in my main:
private static Frame frame = new Frame();
public static void CreateWindow(){
frame.pack();
frame.setVisible(true);
}
Only an empty frame
appears, with nothing in it. What am I doing wrong?
EDIT: My own thought was that setJMenuBar(menubar)
won't work, because menubar
is not a JMenuBar
, but MenuBar extends JMenuBar
, so in fact it is a JMenuBar
, right? Also, I don't get an error at this line, so I don't think that is the case. Just a thought.
You need to instantiate MenuBar
public MenuBar menubar=new MenuBar();
public Frame(){
....
setJMenuBar(menubar);
}