【发布时间】:2018-10-07 02:26:38
【问题描述】:
这是我的大型机
public class MainFrame extends JFrame {
private ToolBar tb;
private JeuPanel panel;
MainFrame() {
super("TP1");
setLayout(new BorderLayout());
panel = new JeuPanel();
tb = new ToolBar();
setJMenuBar(tb);
add(panel, BorderLayout.CENTER);
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
这是我的工具栏类:
public class ToolBar extends JFrame {
private JMenuBar monMenu;
private JMenu menuFichier;
private JMenuItem menuDemarrer;
private JMenuItem menuQuitter;
public ToolBar() {
}
public ToolBar(String title) {
monMenu = new JMenuBar();
menuFichier = new JMenu("Menu");
this.menuDemarrer = new JMenuItem("Demarrer");
this.menuQuitter = new JMenuItem("Quitter");
menuQuitter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
monMenu.add(menuFichier);
menuFichier.add(menuDemarrer);
menuFichier.add(menuQuitter);
this.setJMenuBar(monMenu);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
这是错误:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: tp1.prog.MainFrame.setJMenuBar
at tp1.prog.MainFrame.<init>(MainFrame.java:27)
at tp1.prog.TP1Prog.main(TP1Prog.java:20)
嗨,
我不明白为什么我不能设置我的 JMenu。它说类 Toolbar 不能是 JMenuBar
谢谢
【问题讨论】:
-
将代码和可能的错误消息显示为问题中格式正确的文本,而不是图像。
-
在问题中,不作为评论。
-
在问题中
-
ToolBar类是一个 JFrame。当您在MainFrame构造函数中尝试时,它不能用作菜单栏。在ToolBar的构造函数中,您通过创建和修改一个真正的JMenuBar来做正确的事情,最终将其设置为ToolBar的菜单栏。 -
ToolBar在MainFrame中需要什么?也许你应该简单地将菜单栏构造代码从ToolBar移动到MainFrame。