Java AWT的菜单实例演示

2019-04-15 16:48发布

[img]http://dl.iteye.com/upload/picture/pic/75002/0ada4806-0665-3921-b3c4-822358ef1a16.jpg[/img]

package com.jtang.zhe.java;
import java.awt.*;
import java.awt.event.*;

import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.PopupMenu;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class SimpleMenu {
private Frame f=new Frame("测试");
private MenuBar mb=new MenuBar();
PopupMenu pop=new PopupMenu();
Menu file=new Menu("文件");
Menu edit=new Menu("编辑");
MenuItem newItem=new MenuItem("新建");
MenuItem saveItem=new MenuItem("保存");
//创建exitItem菜单项,指定使用Ctrl+X快捷键
MenuItem exitItem=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
CheckboxMenuItem autoWrap=new CheckboxMenuItem("自动换行");
MenuItem copyItem=new MenuItem("复制");
MenuItem pasteItem=new MenuItem("粘贴");
Menu format=new Menu("格式");
//创建commentItem菜单项,指定使用Ctrl+Shift+/快捷键
MenuItem commentItem=new MenuItem("注释",new MenuShortcut(KeyEvent.VK_SLASH,true));
MenuItem cancelItem=new MenuItem("取消注释");
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CheckboxMenuItem autoWrap1=new CheckboxMenuItem("自动换行");
MenuItem copyItem1=new MenuItem("复制");
MenuItem pasteItem1=new MenuItem("粘贴");
Menu format1=new Menu("格式");
//创建commentItem1菜单项
MenuItem commentItem1=new MenuItem("注释");
MenuItem cancelItem1=new MenuItem("取消注释");
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private TextArea ta=new TextArea(6,40);
public void init()
{
//以匿名内部类的形式创建菜单******
ActionListener menuListener=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
ta.append("单击'"+cmd+"'菜单"+" ");
if(cmd.equals("退出"))
{
System.exit(0);
}
}
};
commentItem.addActionListener(menuListener);
//
commentItem1.addActionListener(menuListener);
exitItem.addActionListener(menuListener);
//为file菜单添加菜单项
file.add(newItem);
file.add(saveItem);
file.add(exitItem);
//为edit菜单添加菜单项
edit.add(autoWrap);
//使用addSeparator方法来添加菜单分隔线
edit.addSeparator();
edit.add(copyItem);
edit.add(pasteItem);
//为format菜单添加菜单项
format.add(commentItem);
format.add(cancelItem);
//------------------------------------------------------------
//为format1菜单添加菜单项
format1.add(commentItem1);
format1.add(cancelItem1);
//------------------------------------------------------------
//使用添加new MenuItem("-")的方式添加菜单分隔线
edit.add(new MenuItem("-"));
//将format菜单组合到edit菜单中,从而形成二级菜单
edit.add(format);
//============================================================
//为pop菜单添加菜单项
pop.add(autoWrap1);
//使用addSeparator方法来添加菜单分隔线
pop.addSeparator();
pop.add(copyItem1);
pop.add(pasteItem1);
pop.add(new MenuItem("-"));
pop.add(format1);
ta.add(pop);
ta.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
{
pop.show(ta,e.getX(),e.getY());
}
}
});
//============================================================
//将file、edit菜单添加到mb菜单条中
mb.add(file);
mb.add(edit);
//为f窗口设置菜单条
f.setMenuBar(mb);
//以匿名内部类的形式来创建事件******对象
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
ta.append("用户试图关闭窗口! ");
System.exit(0);
}
});
f.add(ta);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new SimpleMenu().init();
}

}