-1

Does anyone of you have an idea why my JMenuBar menuBar isn't showing up? I'm using a JFrame and a JPanel.

My class extends JPanel and has a paint method inherited (already with super.paint(g)).

I want to display some JLabels and JTextFields on my JMenuBar (I know that's not the purpose of it)

Here's my code:

public void createMenuBar(){

    menuBar = new JMenuBar();
    menuBar.setBounds(0,0,1463,29);
    menuBar.setLayout(null);
    this.add(menuBar);

    ipLbl = new JLabel("IP-Adresse:");
    ipLbl.setBounds(5,2,150,25);
    ipLbl.setLabelFor(ip);
    menuBar.add(ipLbl);

    ip = new JTextField();
    ip.setBounds(150,2,100,25);
    menuBar.add(ip);
}

I'm calling this method after creating the JFrame, but right before doing setVisible(true).

Look:

public IceHockey(){

    try {
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e){}

    fenster = new JFrame("IceHockey");
    fenster.setSize(1479, 941);
    fenster.setLayout(null);
    fenster.addKeyListener(this);
    fenster.addMouseListener(this);
    fenster.setResizable(false);
    fenster.setLocationRelativeTo(null);

    fenster.setContentPane(this);

    this.addKeyListener(this);
    this.addMouseListener(this);

    createMenuBar();

    fenster.setVisible(true);
    fenster.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}
Matthias
  • 4,481
  • 12
  • 45
  • 84
Michael Gierer
  • 401
  • 2
  • 6
  • 15
  • **Don't use a null layout!!!**. This has already been suggested in one of your previous postings: http://stackoverflow.com/questions/36751366/some-swing-components-not-showing-on-jframe. Pay attention to suggestions. We give them for a reason. Read the Swing tutorial on [How to Use Menus](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html) for a working example. The code will also show you how to better structure your class. – camickr Jun 09 '16 at 17:24
  • camickr I know, but this program is only for me and even my teacher at school told us, that we CAN use null layouts. So it's up to me what I'm using! Besides, the null layout is sooo simple to use! – Michael Gierer Jun 10 '16 at 03:48
  • `Besides, the null layout is sooo simple to use! ` - no it isn't. You have absolutely no idea how big a component should be to display properly. Hardcoding random numbers is a waste of time and will only cause problems in the future, especially when changes need to be made. Simple things like changing a Font will cause problems. Just because you can do something doesn't mean you should. Learn to use Swing properly! You haven't even learned the Swing basics yet (or you wouldn't be asking this question) so you have no idea what is or isn't easy. That only comes from experience. – camickr Jun 10 '16 at 04:55

2 Answers2

2

You can add the menubar to the frame with fenster.setJMenuBar(menuBar);

If you want to add the menu bar to the JPanel this post has an approach you can use:

add JMenuBar to a JPanel?

Community
  • 1
  • 1
Peter
  • 1,592
  • 13
  • 20
1

After creating the MenuBar, I think you still need to add it to your JFrame. Change createMenuBar() to return the JMenuBar instead of void, then you can write the following:

fenster.setJMenuBar(createMenuBar());

I'm unclear what the this is in your code for createMenuBar(), but I'm guessing it isn't fenster yet is something where adding is permitted.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41