菜单栏未显示在 JFrame 中



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

下面是帧类: 公共类 pixelFrame {//此帧将保存用于艺术创作的 pixelPane。这也将包含一个菜单栏,在另一个文件中定义。 像素窗格编辑区域; 像素菜单栏菜单栏; public 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添加任何新功能。只需向主类添加一个方法,例如创建 JMenuBar 并添加 JMenu/JMenuItem 对象createMenuBar(...)

相关内容

  • 没有找到相关文章

最新更新