Java 尽管设置为框架,但菜单栏未出现在 Swing GUI 中

Java Menu Bar not appearing in Swing GUI despite setting to frame

我正在编写一个基本程序,以便练习和学习使用 swing GUI,我构建了一个带有基本菜单的框架,并将其添加到框架中,但由于某种原因,当我 运行程序它没有出现在框架中。

public class GUITest {

private static int windowWidth = 500;
private static int windowHeight = 500;

private static JFrame frame;
private static JMenuBar menuBar;

public static void main(String[] args){
    build();
}

private static void build(){
    windowGen();
    menuGen();
}

private static void windowGen(){
    JFrame frame = new JFrame();
    frame.setLayout(null);
    frame.setSize(windowWidth,windowHeight);
    frame.setVisible(true);
}

private static void menuGen(){
    JMenuBar menuBar = new JMenuBar();
    JMenu menuFile = new JMenu("File");
    JMenuItem menuFileExit = new JMenuItem("Exit");
    menuFile.add(menuFileExit);

    menuBar.add(menuFile);

    frame.setJMenuBar(menuBar);
}   
}

你们知道为什么会这样吗?

引用上面声明的它的静态变量,但是不要新建一个,这是第一个错误,还有一个就是你的JFrame没有定义Layout

public class GUITest {

private static int windowWidth = 500;
private static int windowHeight = 500;

private static JFrame frame;
private static JMenuBar menuBar;

public static void main(String[] args){
    build();
}

private static void build(){
    windowGen();
    menuGen();
}

private static void windowGen(){
    frame = new JFrame();
    frame.setLayout(null);
    frame.setSize(windowWidth,windowHeight);
    frame.setVisible(true);
}

private static void menuGen(){
    JMenuBar menuBar = new JMenuBar();
    JMenu menuFile = new JMenu("File");
    JMenuItem menuFileExit = new JMenuItem("Exit");
    menuFile.add(menuFileExit);

    menuBar.add(menuFile);

    frame.setJMenuBar(menuBar);
}   
}