Java -- AWT 菜单建立, Menu, 右键菜单
1. Menu类结构
2. 菜单示例: MenuBar容器中可以装Menu,Menu容器中可以装MenuItem。
public class SimpleMenu {
Frame f = new Frame("test Menu");
MenuBar mb = new MenuBar();
Menu file = new Menu("File");
Menu edit = new Menu("Edit");
MenuItem newItem = new MenuItem("New");
MenuItem saveItem = new MenuItem("Save");
MenuItem exitItem = new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_X));
CheckboxMenuItem autoWrap = new CheckboxMenuItem("AutoWrap");
MenuItem copyItem = new MenuItem("Copy");
MenuItem pasteItem = new MenuItem("Paste");
Menu format = new Menu("Format");
MenuItem commentItem = new MenuItem("Comment", new MenuShortcut(KeyEvent.VK_SLASH,true));
MenuItem cancelItem = new MenuItem("non-Comment");
TextArea ta = new TextArea(6, 40);
public void init()
{
ActionListener menuListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String cmd = arg0.getActionCommand();
ta.append("Click" + cmd + "menu\n" );
if( cmd.equals("cancel") )
{
System.exit(0);
}
}
};
commentItem.addActionListener(menuListener);
exitItem.addActionListener(menuListener);
file.add(newItem);
file.add(saveItem);
file.add(exitItem);
edit.add(autoWrap);
edit.addSeparator();
edit.add(copyItem);
edit.add(pasteItem);
format.add(commentItem);
format.add(cancelItem);
edit.add(new MenuItem("-"));
edit.add(format);
mb.add(file);
mb.add(edit);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setMenuBar(mb);
f.add(ta);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("SimpleMenu");
new SimpleMenu().init();
}
}
添加右键菜单,上面的程序修改一下, 可以变换TextArea的右键菜单, 其他控件也可以用这种方法添加右键菜单。
PopupMenu pop = new PopupMenu(); //右键菜单 pop.add(autoWrap);
pop.addSeparator();
pop.add(copyItem);
pop.add(pasteItem);
ta.add(pop);
ta.addMouseListener(new MouseAdapter() { //鼠标事件监听
public void mouseReleased(MouseEvent e)
{
if( e.isPopupTrigger() )
{
pop.show(ta, e.getX(), e.getY()); //显示右键菜单
}
}
});
Java -- AWT 菜单建立, Menu, 右键菜单的更多相关文章
- JAVA GUI学习 - JPopupMenu鼠标右键菜单组件学习
public class JPopmenuKnow { public void test() { //为表格添加鼠标右键菜单 JMenuItem jMenuItemFileInfo = new JMe ...
- 怎样在Windows资源管理器中添加右键菜单以及修改右键菜单顺序
有时,我们需要在Windows资源管理器的右键菜单中添加一些项,以方便使用某些功能或程序. 比如我的电脑上有一个免安装版的Notepad++,我想在所有文件的右键菜单中添加一项用Notepad++打开 ...
- Win7/Win8右键菜单管理工具(Easy Context Menu) v1.5 绿色版
软件名称: Win7/Win8右键菜单管理工具(Easy Context Menu)软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: ...
- 添加删除系统右键菜单(就是上下文菜单,也就是Context Menu)中的一些选项
随着电脑安装的东西越来越多,右侧菜单也原来越长,很不方面.所以打算清理一下 我删除的大约以下几个,友好一点的都可以配置.当然也可以通过注册表直接删除. 特:注册表备份,即导入导出,避免一失足成千古恨. ...
- FineUI Grid控件右键菜单的实现
FineUI官方Demo上一直没有Grid右键菜单的实现,其实从4.1.x的版本开始,允许添加自定义的事件监听(Listeners),所以要实现这个功能已经相当容易了. ExtJs右键菜单有很多种,对 ...
- 关于JFace中的右键菜单Action类,ActgionGroup类,MenuManager类
Action类,ActionGroup类,MenuManager类介绍 SWT中菜单是Menu类,在前面章节中已经介绍过Menu类的使用. 菜单项用MeauItem类来实现.但是在实际开发中,同一种功 ...
- 《ArcGIS Engine+C#实例开发教程》第六讲 右键菜单添加与实现
原文:<ArcGIS Engine+C#实例开发教程>第六讲 右键菜单添加与实现 摘要:在这一讲中,大家将实现TOCControl控件和主地图控件的右键菜单.在AE开发中,右键菜单有两种实 ...
- QML添加右键菜单
MouseArea { id: mouseRegion anchors.fill: parent; acceptedButtons: Qt.LeftButton | Qt.RightButton // ...
- JS简单实现自定义右键菜单
RT,一个简单的例子,仅仅讲述原理 <div id="menu" style="width: 0;height: 0;background: cadetblue;p ...
- AS3.0 自定义右键菜单类
AS3.0 自定义右键菜单类: /** * 自定义右键菜单类 * 自定义菜单项不得超过15个,每个标题必须至少包含一个可见字符. * 标题字符不能超过100个,并且开头的空白字符会被忽略. * 与任何 ...
随机推荐
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 专用于高并发的map类-----Map的并发处理(ConcurrentHashMap)
oncurrentModificationException 在这种迭代方式中,当iterator被创建后集合再发生改变就不再是抛出ConcurrentModificationException, 取 ...
- 修改NameNode端口后,hive表查询报错
在进行使用hive查询表数据的时候,抛出异常 hive> select*from blackList;FAILED: SemanticException Unable to determine ...
- [转载]Axis2 and CXF的比较
在Celtix 和XFire 宣布合并的同年,另一个著名开源Web 服务框架Axis 的后继者Axis2 也诞生了.Axis2 并非Axis 的2.0 版,而是完全重写了Axis 的新项目.作为功能和 ...
- Currency Exchange - poj 1860
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22111 Accepted: 7986 Description Seve ...
- ubuntu 16.04查询文件安装目录
dpkg -L filename dpkg -l | grep filename whereis filename find / -name filename
- Android双缓冲技术
参考文章: 1.http://djt.qq.com/article/view/987 2.http://blog.csdn.net/i_lovefish/article/details/7913623 ...
- 【文献阅读】Perceptual Generative Adversarial Networks for Small Object Detection –CVPR-2017
Perceptual Generative Adversarial Networks for Small Object Detection 2017CVPR 新鲜出炉的paper,这是针对small ...
- JSP 生命周期 理解JSP底层功能的关键就是去理解它们所遵守的生命周期
JSP 生命周期 理解JSP底层功能的关键就是去理解它们所遵守的生命周期. JSP生命周期就是从创建到销毁的整个过程,类似于servlet生命周期,区别在于JSP生命周期还包括将JSP文件编译成ser ...
- [转]基于fiddler的APP抓包及服务端模拟
在HTTP接口的测试过程中,一般我们会按照如下的步骤进行: 1)测试环境的准备 2)HTTP消息体的构造 3)HTTP消息的发送及断言 如果我们可以拿到项目组的接口文档,并且HTTP后台服务是可以工作 ...