JFrame 中不显示菜单栏

Menu bar not showing in JFrame

我已将自定义 JPane 和自定义 JMenuBar 添加到我的 JFrame。 JPane 显示得很好,但不是 JMenuBar。我在不同的 classes

中定义了框架和菜单栏

这是框架 class: public class pixelFrame { //这个框架将容纳一个用于艺术创作的pixelPane。这还将包含一个在另一个文件中定义的菜单栏。 pixelPane编辑区域; 像素菜单栏菜单栏; public像素帧() { EventQueue.invokeLater(new Runnable() { //在运行时创建一个新线程

        @Override
        public void run() {  //when the program runs, do the following
            JFrame frame = new JFrame("Pixel Art Creator");  //create a new JFrame (similar to a regular window), and give it the following title
            editingArea = new pixelPane();
            menuBar = new pixelMenuBar();
            frame.add(editingArea);  //add a pixelPane named editingArea to the JFrame
            frame.setJMenuBar(menuBar); //adds a menu to the JFrame
            frame.pack(); //make the window big enough to fit all components (in this case, editingArea)
            frame.setLocationRelativeTo(null); //set window to the center of the screen
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Stop the thread and terminate the program.
            frame.validate();
            frame.setVisible(true); //You can see the window.

            menuBar.setVisible(true);
        }

    });
}

public static void main(String[] args)
{
    pixelPane.gridEnabled = true; //changing another variable from pixelPane just for testing.
    new pixelFrame(); //create an instance of pixelFrame.
}

}

这是菜单栏:

public class pixelMenuBar extends JMenuBar {
/**
 * 
 */
private static final long serialVersionUID = 1L;
JMenuBar menuBar;
JMenu file, tool;
JMenuItem save, load, changeColor;
JCheckBoxMenuItem gLines;
public pixelMenuBar() {
    menuBar = new JMenuBar();

    //creates a "File" menu in the menu bar
    file= new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    menuBar.add(file); //add the menu to the menu bar

    //creates a "Tools" menu in the menu bar
    tool = new JMenu("Tools");
    tool.setMnemonic(KeyEvent.VK_T);
    menuBar.add(tool);


    //Menu items that go under the File menu
    save = new JMenuItem("Save File"); //save and export the image as an .svg file
    save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_S)); //a Ctrl+S hotkey. Handy dandy!
    file.add(save);//adds the Save button to the File menu

    load = new JMenuItem("Load File"); //load the file into BufferedImage, make it into a JLabel and add it to the panel.
    load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_L));//a Ctrl+L hotkey. Also handy dandy!
    file.add(load);//adds the Load button to the File menu


    //Menu items that will go under the Tools menu
    gLines = new JCheckBoxMenuItem("Gridlines"); //creates a new checkbox for enabling grid lines. will toggle pixelPane.gridEnabled
    gLines.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_G));
    tool.add(gLines);//adds the gridlines tool to the Tools menu
    tool.addSeparator(); //adds a separating line in the Tools menu. Organization.

    changeColor = new JMenuItem("Change Color");//creates a new menu item that will let the user the color of the pixel. Uses a color picker
    tool.add(changeColor);//adds Change Color to the Tools menu
}

}

我没有正确地向 UI 添加内容吗?我再次检查我使用的是 addJMenuBar() 而不是 addMenuBar()。

首先 class 名称应该以大写字符开头。你见过 Java API 中没有的 class 吗?遵循 Java 惯例。通过实例学习。

public class pixelMenuBar extends JMenuBar 
{
...
public pixelMenuBar() 
{
    menuBar = new JMenuBar();

您的 class "is a " JMenuBar,但是您在 class 中做的第一件事是创建一个 JMenuBar。

不要创建 JMenuBar!

只需将菜单项添加到 JMenuBar class 本身:

//menuBar.add(file); //add the menu to the menu bar
add(file); //add the menu to the menu bar

实际上甚至不需要 PixelMenuBar class,因为您没有向 JMenuBar 添加任何新功能。只需向主 class 添加一个方法,如 createMenuBar(...) 即可创建 JMenuBar 并添加 JMenu/JMenuItem 对象。