站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题为什么我的程序在eclipse下运行不显示MenuBar?

为什么我的程序在eclipse下运行不显示MenuBar?

摘要: 为什么我的程序在eclipse下运行不显示MenuBar?


为什么我的程序在eclipse下运行不显示MenuBar?
程序是照书打的,应该不会有错阿
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Menudemo extends Frame implements ActionListener {
TextArea ta;

public Menudemo() {
ta = new TextArea(30, 60);
add(ta);
MenuBar menubar = new MenuBar();
setMenuBar(menubar);
Menu file = new Menu("File");
MenuItem open = new MenuItem("open", new MenuShortcut(KeyEvent.VK_Q));
MenuItem quit = new MenuItem("quit", new MenuShortcut(KeyEvent.VK_Q));
file.add(open);
file.addSeparator();
file.add(quit);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Frame frm = (Frame) (e.getSource());
frm.dispose();

}
});
open.addActionListener(this);
quit.addActionListener(this);
setSize(300, 300);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Quit") {
dispose();
} else {
ta.setText("read a file content and write here");

}
}

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

}



你只是定义了Menu file,但是并没有把它添加到MenuBar中,要在定义Menu file后添加menubar.add(file);
这句就可以了。


另外你的程序如果想实现点击quit菜单就退出的功能,还要加上
open.setActionCommand("Open");
quit.setActionCommand("Quit");
因为你在actionPerformed方法中根据ActionCommand判断,但是你却没有给quit和open设置actionCommand,所以你的程序永远也执行不到dispose,因为根本就没有任何事件的actioinCommand是Quit。


我加了menubar.add(file); ,可是add下却多了条红色波浪线,显示The method add(Menu) in the type MenuBar is not applicable for the arguments
(Menu)

这是为什么阿???


↑返回目录
前一篇: eclipse插件无法激活
后一篇: 如何将NetBeans工程导入Eclipse?