一些jmenu不显示



我是一个全新的代码,但我真的有乐趣学习一切。不幸的是,我遇到了一个我无法解决的问题。我正在学习如何制作菜单栏,菜单和项目,但是当我运行我的程序时,它只显示"文件"菜单。在我向文件菜单添加任何菜单项之前,帮助菜单会出现,但是当我添加项时,它不再出现。有人知道我哪里做错了吗?谢谢大家。

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Window {
    public static void drawWindow(int HEIGHT, int WIDTH, String TITLE){
        JFrame window = new JFrame(TITLE);
        window.setVisible(true);
        window.setSize(WIDTH, HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        window.setJMenuBar(menuBar);
        JMenu file = new JMenu("File");
        menuBar.add(file);
        JMenuItem exit = new JMenuItem("Exit");
        file.add(exit);
        JMenu help = new JMenu("Help");
        menuBar.add(help);
        JMenuItem about = new JMenuItem("About");
        help.add(about);

当我将代码片段window.setVisible(true);剪切并粘贴到末尾时,您的内容似乎看起来很好。

自己去看看吧:

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Window {
public static void drawWindow(int HEIGHT, int WIDTH, String TITLE) {
    JFrame window = new JFrame(TITLE);
    window.setSize(WIDTH, HEIGHT);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    window.setJMenuBar(menuBar);
    JMenu file = new JMenu("File");
    menuBar.add(file);
    JMenuItem exit = new JMenuItem("Exit");
    file.add(exit);
    JMenu help = new JMenu("Help");
    menuBar.add(help);
    JMenuItem about = new JMenuItem("About");
    help.add(about);
    window.setVisible(true);
}
public static void main(String[] args) {
    new Window().drawWindow(500, 500, "xyz");
}
}

最新更新