سؤال

When I create a object(BlueJ) and the window pops up, theres no menu bar, why is that? All is the is the windows and the text "title" and nothing more. Im fairly new with OOP and Java, and I am also using BlueJ. Here's my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Spel
{
//Varibler
private JFrame frame;


/**
 * Skapa själva spelet.
 *
 */
public Spel()
{
    makeFrame();
}

/**
 * Skapa framet.
 */
private void makeFrame()
{
    //Skapa framet.
    frame = new JFrame("Spel");
    Container contentPane = frame.getContentPane();
    //lägg till titel.
    JLabel label = new JLabel("titel");
    contentPane.add(label);

    frame.pack();
    frame.setVisible(true);        
}

/**
 * Skapa menu baren.
 */
private void makeMenuBar(JFrame frame)
{
    //skapa menu bar 
    JMenuBar menubar = new JMenuBar();
    frame.setJMenuBar(menubar);
    //skapa menu
    JMenu fileMenu = new JMenu();
    menubar.add(fileMenu);
    //lägg till menu knappar
    JMenuItem openItem = new JMenuItem("Open");
    fileMenu.add(openItem);
    JMenuItem quitItem = new JMenuItem("Quit");
    fileMenu.add(quitItem);
}
هل كانت مفيدة؟

المحلول

You forgot to call makeMenuBar which adds the menu bar

makeMenuBar(frame);

Incidentally you need to give your file menu a title otherwise the menu bar won't be visible

JMenu fileMenu = new JMenu("File");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top