Search code examples
javaswingawtjmenujmenuitem

JMenu-JMenuItem doesn't appear


My problem with this code is that when I run it the JMenuItem doesn't appear. I'm so confused! Anyone can help?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewClass extends JFrame implements ActionListener{
JFrame window = new JFrame("Ηλεκτρονικό Έντυπο Υπερωριών");
JMenu menu = new JMenu("Example");
JMenuItem menuItem = new JMenuItem("hi");
public NewClass(){
    window.setSize(200,200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    menu.add(menuItem);
    menuItem.addActionListener(this);

    window.add(menu);
    window.pack();
    window.setVisible(true);
        }
public static void main(String []args){
    NewClass example = new NewClass();
}

@Override
public void actionPerformed(ActionEvent ae) {
    throw new UnsupportedOperationException("Not supported yet.");
    }
}

Solution

  • You need to add a menubar to the window. To this menubar you add your menu(s). Take a look at oracle's tutorial on How to Use Menus.