简体   繁体   中英

Java Swing JMenuBar

my JMenuBar is not showing AT ALL. This is just a barebones GUI. I have sat here for quite a while now, and I cannot see the problem, soo, as they say, four eyes are better than two.

Thanks.

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

public class MainGui{
    private DrawPanel drawPanel;
    private JFrame mainFrame;
    private JPanel drawPanel;
    private JMenuBar menuBar;
    private JMenu fileMenu, imgMenu, helpMenu;
    private JMenuItem fileNew, fileOpen, fileSave, fileExit;
    private JMenuItem imgBtn1;
    private JMenuItem hlpAbout;

    public MainGui(DrawPanel drawPanel){
    mainFrame = new JFrame("JDraw v1");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setLayout(new BorderLayout());

    this.drawPanel = drawPanel;

    menuBar = new JMenuBar();
    fileMenu = new JMenu("File");
    imgMenu = new JMenu("Image");
    helpMenu = new JMenu("Help");

    fileNew = new JMenuItem("New File");
    fileOpen = new JMenuItem("Open File");
    fileSave = new JMenuItem("Save File");
    fileExit = new JMenuItem("Exit");
    imgBtn1 = new JMenuItem("Useless Button");
    hlpAbout = new JMenuItem("About this program");

    mainFrame.add(menuBar, BorderLayout.PAGE_START);
    menuBar.add(fileMenu);
    menuBar.add(imgMenu);
    menuBar.add(hlpMenu);

    fileMenu.add(fileNew);
    fileMenu.add(fileOpen);
    fileMenu.add(fileSave);
    fileMenu.add(fileExit);
    imgMenu.add(imgBtn1);
    helpMenu.add(hlpAbout);

    mainFrame.add(drawPanel, BorderLayout.CENTER);

    mainFrame.pack();
    mainFrame.setSize(640,480);
    mainFrame.setResizable(false);
    mainFrame.setVisible(true);
    }
}

You want to do:

mainFrame.setJMenuBar(menuBar);

not:

mainFrame.add(menuBar, BorderLayout.PAGE_START);

我认为您应该使用:

mainFrame.setMenuBar(menuBar);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM