java显示在另一个类/文件中创建的菜单栏
我有两个文件/类,主类和菜单栏类
这是我的主要课程:
public static void main(String[] args){
journalFrame = new JFrame("Journal Viewer");
journalFrame.setJMenuBar(menuBar = new JMenuBar());
journalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
journalFrame.setPreferredSize(new Dimension(500, 800));
journalFrame.pack();
journalFrame.setLocationRelativeTo(null);
journalFrame.setVisible(true);
}
这是我的菜单栏类:
MenuBar(){
mMbar = new JMenuBar();
fileMenu = new JMenu("File");
aboutMenu = new JMenu("About");
openItem = new JMenuItem("Open");
openItem.addActionListener((ActionListener) this);
fileMenu.add(openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener((ActionListener) this);
fileMenu.add(exitItem);
mMbar.add(fileMenu);
}
我的输出只是一个没有菜单栏的普通窗口。我如何使它出现
# 1 楼答案
您没有创建类的对象,您只是在调用
menuBar = new JMenuBar()
时创建了一个JMenuBar
对象。另外,如果这是您的完整代码,那么它会有很多问题,老实说,我不会费心去弄清楚为什么我会在点和分号上出现编译器错误。请注意setJMenuBar(JMenuBar menuBar)
只能使用JMenuBar对象作为参数调用。所以要么Override
使用该方法使其按您想要的方式运行,要么尝试其他方法我将加入@AndrewThompson的观点,他认为在GUI类中创建菜单栏会更有意义
下面是您的代码可能(或可能应该)的样子: