我在我的JFrame中添加了一个自定义的JPane和一个自定义的JMenuBar。 JPane显示很好,但JMenuBar却没有。我在不同的类中定义了框架和菜单栏

这是框架类:
    public class pixelFrame {//此框架将保存一个用于艺术创作的pixelPane。这还将包含在另一个文件中定义的菜单栏。
    pixelPane editArea;
    pixelMenuBar menuBar;
    公共pixelFrame()
    {
        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()。

最佳答案

首先,类名应该以大写字母开头。您见过Java API中没有的类吗?遵循Java约定。通过例子学习。

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


您的类“是一个JMenuBar,但您在该类中要做的第一件事就是创建一个JMenuBar。

不要创建JMenuBar!

只需将菜单项添加到JMenuBar类本身即可:

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


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

10-08 17:16