错误截图:
源代码:- import java.awt.FileDialog;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.Menu;
- import java.awt.MenuBar;
- import java.awt.MenuItem;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class MenuDemo {
- private Frame f;
- private MenuBar bar;
- private Menu menu;
- private MenuItem open;
- private FileDialog fd;
- public MenuDemo() {
- init();
- }
- public void init() {
- //建立一个窗体
- f = new Frame("wo de Frame");
- //设置窗体属性
- f.setBounds(500, 400, 300, 200);
- f.setLayout(new FlowLayout());
- bar=new MenuBar();
- menu=new Menu("文件");
- open=new MenuItem("打开");
- menu.add(open);
- bar.add(menu);
- f.setMenuBar(bar);
- fd=new FileDialog(f,"我要打开",FileDialog.SAVE);
- myEvent();
- f.setVisible(true);
- }
-
- private void myEvent() {
-
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.out.println("我被关闭了");
- System.exit(0);
- }
- });
-
- open.addActionListener(new ActionListener() {
-
- public void actionPerformed(ActionEvent e) {
- fd.setVisible(true);
- }
- });
- }
-
- public static void main(String[] args) {
- new MenuDemo();
- }
- }
复制代码
|
|