弹出菜单是GUI程序中非常常见的一种控件。它通常由鼠标右击事件触发,比如在windows系统桌面上右击时,会弹出一个包含“刷新”、“属性”等菜单的弹出菜单。Swing中的弹出菜单是JPopupMenu,它的基本性质与JMenu类似,可以使用add方法给它内部添加JMenu或者JMenuItem。手动显示JPopupMenu时,需使用show(parentComponent, x, y)方法,指定父控件和显示坐标。用户的操作习惯是在右击某个 控件时显示弹出菜单,那么需要使用component.setComponentPopupMenu(popupMenu)方法来添加该弹出菜单。偶尔会遇到在已经包含弹出菜单的控件内再添加子控件的情况,这时子控件可以使用child.setInheritsPopupMenu(true)来继承该弹出菜单。

下面是JPopupMenu常用方法:

为了练手,我们来模仿实现eclipse中编辑区域的弹出菜单,代码如下:

package Swing;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent; import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke; /*
*模仿实现eclipse中编辑区域的弹出菜单
*/
public class JPopupMenuDemo extends JFrame { JPopupMenu popupMenu; public JPopupMenuDemo(){
JMenuItem menuItem;//用作临时变量 popupMenu = new JPopupMenu();
popupMenu.add(createMenuItem("Undo Typing", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\undoTyping.png"), KeyStroke.getKeyStroke('Z', InputEvent.CTRL_MASK)));
popupMenu.add(createMenuItem("Revert File", null, null, null));
popupMenu.add(createMenuItem("Save", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\save.png"), KeyStroke.getKeyStroke('S', InputEvent.CTRL_MASK)));
popupMenu.addSeparator();
popupMenu.add(createMenuItem("Open Declaration", null, null, KeyStroke.getKeyStroke("F3")));
popupMenu.add(createMenuItem("Open Type Hierarchy", null, null, KeyStroke.getKeyStroke("F4")));
popupMenu.add(createMenuItem("Open Call Hierarchy", null, null, KeyStroke.getKeyStroke('H', InputEvent.CTRL_MASK | InputEvent.ALT_MASK)));
popupMenu.add(createMenuItem("Show in Breakcrumb", null, null, KeyStroke.getKeyStroke('B', InputEvent.SHIFT_MASK | InputEvent.ALT_MASK)));
popupMenu.add(createMenuItem("Quick Outline", null, null, KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK)));
popupMenu.add(createMenuItem("Quick Type Hierarchy", null, null, KeyStroke.getKeyStroke('T', InputEvent.CTRL_MASK)));
popupMenu.add(createMenuNamedOpenWith());
popupMenu.add(createMenuNamedShowIn());
popupMenu.addSeparator();
menuItem = popupMenu.add(createMenuItem("Cut", null, null, KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK)));
menuItem.setEnabled(false);
menuItem = popupMenu.add(createMenuItem("Copy", null, null, KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK)));
menuItem.setEnabled(false);
popupMenu.add(createMenuItem("Copy Qualified Name", null, null, null));
popupMenu.add(createMenuItem("Paste", null, null, KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK)));
popupMenu.addSeparator();
popupMenu.add(createMenuItem("Quick Fix", null, null, KeyStroke.getKeyStroke('1', InputEvent.CTRL_MASK)));
popupMenu.add(createMenuNamedSource());
popupMenu.add(createMenuNamedRefactor());
popupMenu.add(createMenuNamedLocalHistory());
popupMenu.addSeparator();
popupMenu.add(createMenuNamedReferences());
popupMenu.add(createMenuNamedDeclarations());
popupMenu.add(createMenuItem("Add To Snippets", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\addToSnippets.png"), null));
popupMenu.addSeparator();
popupMenu.add(createMenuNamedDebugAs());
popupMenu.add(createMenuNamedRunAs());
popupMenu.add(createMenuItem("Validate", null, null, null));
popupMenu.add(createMenuNamedTeam());
popupMenu.add(createMenuNamedCompareWith());
popupMenu.add(createMenuNamedReplaceWith());
popupMenu.addSeparator();
popupMenu.add(createMenuItem("Prefrences", null, null, null));
popupMenu.addSeparator();
popupMenu.add(createMenuItem("Remove from Context", null, null, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK | InputEvent.ALT_MASK | InputEvent.SHIFT_MASK))); JPanel panel = new JPanel();
panel.setComponentPopupMenu(popupMenu);
add(panel);
} private JMenu createMenuNamedReplaceWith() {
/*
* * 创建名称为ReplaceWith的menu
*/
JMenu menu = createMenu("Replace With", null);
menu.add(createMenuItem("Local History", null, null, null));
menu.add(createMenuItem("Previous From History", null, null, null)); return menu;
} private JMenu createMenuNamedCompareWith() {
/*
* * 创建名称为CompareWith的menu
*/
JMenu menu = createMenu("Compare With", null);
JMenuItem menuItem = menu.add(createMenuItem("Each Other", null, null, null));
menuItem.setEnabled(false);
menu.add(createMenuItem("Local History", null, null, null)); return menu;
} private JMenu createMenuNamedTeam() {
/*
* * 创建名称为Team的menu
*/
JMenu menu = createMenu("Team", null);
menu.add(createMenuItem("Apply Patch", null, null, null));
menu.add(createMenuItem("Show Local History", null, null, null));
JMenuItem menuItem= menu.add(createMenuItem("Share Project", null, null, null));
menuItem.setEnabled(false);
return menu;
} private JMenu createMenuNamedRunAs() {
/*
* * 创建名称为RunAs的menu
*/
JMenu menu = createMenu("Run As", null);
menu.add(createMenuItem("Java Application", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\javaApplication.png"),KeyStroke.getKeyStroke('X', InputEvent.ALT_MASK | InputEvent.SHIFT_MASK)));
menu.addSeparator();
menu.add(createMenuItem("Debug Configurations", null, null, null)); return menu;
} private JMenu createMenuNamedDebugAs() {
/*
* * 创建名称为DebugAs的menu
*/
JMenu menu = createMenu("Debug As", null);
menu.add(createMenuItem("Java Application", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\javaApplication.png"),KeyStroke.getKeyStroke('D', InputEvent.ALT_MASK | InputEvent.SHIFT_MASK)));
menu.addSeparator();
menu.add(createMenuItem("Debug Configurations", null, null, null)); return menu;
} private JMenu createMenuNamedDeclarations() {
/*
* * 创建名称为Declarations的menu
*/
JMenu menu = createMenu("Declarations", null);
menu.add(createMenuItem("workSpace", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\declarationsIcon.png"),KeyStroke.getKeyStroke('G', InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)));
menu.add(createMenuItem("Project", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\declarationsIcon.png"),null));
menu.add(createMenuItem("Hierarchy", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\declarationsIcon.png"),null));
menu.addSeparator();
menu.add(createMenuItem("WorkingSet", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\declarationsIcon.png"),null)); return menu;
} private JMenu createMenuNamedReferences() {
/*
* * 创建名称为References的menu
*/
JMenu menu = createMenu("References", null);
menu.add(createMenuItem("workSpace", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\ReferencesIcon.png"),KeyStroke.getKeyStroke('G', InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)));
menu.add(createMenuItem("Project", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\ReferencesIcon.png"),null));
menu.add(createMenuItem("Hierarchy", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\ReferencesIcon.png"),null));
menu.addSeparator();
menu.add(createMenuItem("WorkingSet", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\ReferencesIcon.png"),null)); return menu;
} private JMenu createMenuNamedLocalHistory() {
/*
* * 创建名称为LocalHistory的menu
*/
JMenu menu = createMenu("Local History", null);
menu.add(createMenuItem("Compare With", null, null,null));
menu.add(createMenuItem("Replace With Previous", null, null,null));
menu.add(createMenuItem("Replace With", null, null,null));
menu.add(createMenuItem("Restore From", null, null,null)); return menu;
} private JMenu createMenuNamedRefactor() {
/*
* * 创建名称为Refactor的menu
*/
JMenu menu = createMenu("Refactor", null);
menu.add(createMenuItem("Move", null, null,KeyStroke.getKeyStroke('V', InputEvent.SHIFT_MASK | InputEvent.ALT_MASK)));
menu.addSeparator();
menu.add(createMenuItem("Change Method Signature", null, null,KeyStroke.getKeyStroke('C', InputEvent.SHIFT_MASK | InputEvent.ALT_MASK)));
menu.addSeparator();
menu.add(createMenuItem("Extract Interface", null, null,null));
menu.add(createMenuItem("Extract Superclass", null, null,null));
menu.add(createMenuItem("Use Suppertype where Possible", null, null,null));
menu.add(createMenuItem("Pull Up", null, null,null));
menu.add(createMenuItem("Push Down", null, null,null));
menu.addSeparator();
menu.add(createMenuItem("Extract Class", null, null,null));
menu.add(createMenuItem("Introduce Parameter Object", null, null,null));
menu.addSeparator();
menu.add(createMenuItem("Infer Generic Type Arguments", null, null,null)); return menu;
} private JMenu createMenuNamedSource() {
/*
* * 创建名称为Source的menu
*/
JMenu menu = createMenu("Source", null);
menu.add(createMenuItem("Toggle Comment", null, null,KeyStroke.getKeyStroke('/', InputEvent.CTRL_MASK)));
menu.add(createMenuItem("Remove Bolck Comment", null, null,KeyStroke.getKeyStroke('\\', InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)));
menu.add(createMenuItem("Generate Element Comment", null, null,KeyStroke.getKeyStroke('J', InputEvent.SHIFT_MASK | InputEvent.ALT_MASK)));
menu.addSeparator();
menu.add(createMenuItem("Corrent Indentation", null, null, KeyStroke.getKeyStroke('I', InputEvent.CTRL_MASK)));
menu.add(createMenuItem("Format Element", null, null,null));
menu.add(createMenuItem("Format ", null, null,KeyStroke.getKeyStroke('F', InputEvent.SHIFT_MASK | InputEvent.ALT_MASK)));
menu.addSeparator();
menu.add(createMenuItem("Add Import", null, null,KeyStroke.getKeyStroke('M', InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)));
menu.add(createMenuItem("Organize Imports", null, null,KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)));
menu.add(createMenuItem("Sort Members", null, null,null));
menu.add(createMenuItem("Clean Up", null, null,null));
menu.addSeparator();
menu.add(createMenuItem("Override/Implement Methods", null, null,null));
menu.add(createMenuItem("Generate Getters and Setters", null, null,null));
menu.add(createMenuItem("Generate Delegate Methods", null, null,null));
menu.add(createMenuItem("Generate hashCode() and equals()", null, null,null));
menu.add(createMenuItem("Generate toString()", null, null,null));
menu.add(createMenuItem("Generate Constructor using Fields", null, null,null));
menu.add(createMenuItem("Generate Constructor from Superclass", null, null,null));
menu.addSeparator();
menu.add(createMenuItem("Externalize Strings", null, null,null)); return menu;
} private JMenu createMenuNamedShowIn() {
/*
* * 创建名称为ShowIn的menu
*/
JMenu menu = createMenu("Show In", null);
menu.add(createMenuItem("Git Repositories", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\gitRepositories.png"), null));
menu.add(createMenuItem("Package Explorer", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\packageExplorer.png"), null));
menu.add(createMenuItem("History", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\history.png"), null));
menu.add(createMenuItem("Navigator", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\navigator.png"), null));
menu.add(createMenuItem("Project Explorer", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\projectExplorer.png"), null));
menu.add(createMenuItem("Outline", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\outline.png"), null));
menu.add(createMenuItem("System Explorer", null, null, null));
menu.add(createMenuItem("Properties", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\Properties.png"), null));
return menu;
} private JMenu createMenuNamedOpenWith() {
/*
* * 创建名称为OpenWith的menu
*/
JMenu menu = createMenu("Open With", null);
menu.add(createMenuItem("Java Editor", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\JavaEditor.png"), null));
menu.add(createMenuItem("Text Editor", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\textEditor.png"), null));
menu.add(createMenuItem("Window Builder Editor", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\windowBuilderEditor.png"), null));
menu.addSeparator();
menu.add(createMenuItem("System Editor", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\systemEditor.png"), null));
menu.add(createMenuItem("In-Place Editor", null, new ImageIcon("resources\\JPopupMenuDemo\\icon\\inPlaceEditor.png"), null));
menu.add(createMenuItem("Default Editor", null, null, null));
menu.addSeparator();
menu.add(createMenuItem("Other", null, null, null));
return menu;
} private JMenu createMenu(String name, String mnemonic) {
/*
* 根据名称和快捷键创建menu并添加到menuBar
*/
JMenu menu = new JMenu(name);
if (mnemonic != null)
menu.setMnemonic(mnemonic.toCharArray()[0]);
return menu;
} private JMenuItem createMenuItem(String name, String mnemonic, Icon icon, KeyStroke keyStroke) {
/*
* 根据名称和快捷键创建menu并添加到menuBar
*/
JMenuItem menuItem = new JMenuItem(name, icon);
if (mnemonic != null)
menuItem.setMnemonic(mnemonic.toCharArray()[0]);
if (keyStroke != null)
menuItem.setAccelerator(keyStroke);
return menuItem;
} public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建窗体
JPopupMenuDemo frame = new JPopupMenuDemo();
//指定标题
frame.setTitle("JPopupMenuDemo");
//指定icon
frame.setIconImage(new ImageIcon("resources\\JMenuDemo\\icon\\Eclipse.png").getImage());
// 关闭窗体后退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 自动适配所有控件大小
frame.setSize(frame.getPreferredSize().width, 200);
//frame.pack();
// 设置窗体位置在屏幕中央
frame.setLocationRelativeTo(null);
// 显示窗体
frame.setVisible(true);
} }

运行效果图1:

运行效果图2:

图标目录:

Swing-JPopupMenu弹出菜单用法-入门的更多相关文章

  1. Web标准:四、纵向导航菜单及二级弹出菜单

    Web标准:四.纵向导航菜单及二级弹出菜单 知识点: 1.纵向列表 2.标签的默认样式 3.css派生选择器 4.css选择器的分组 5.纵向二级列表 6.相对定位和绝对定位   1)纵向列表 可以看 ...

  2. css+html+js实现多级下拉和弹出菜单

    本文将使用css+html+js实现横向菜单.具有多级弹出菜单下拉. 首先我们来看看效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvajkwMzgy ...

  3. 向上弹出菜单jQuery插件

    插件名:柯乐义英文名:Keleyijs文件名称:jquery.keleyi.js插件功能:该插件可以让你轻易地在页面上构建一个向上弹出的二级菜单. 示例查看:http://keleyi.com/kel ...

  4. html5手机端遮罩弹出菜单代码

    效果体验:http://hovertree.com/texiao/html5/17/ 效果图: 代码如下: <!doctype html> <html lang="zh&q ...

  5. DIV+CSS制作二级横向弹出菜单,略简单

    没有使用JavaScript控制二级菜单的显示,结果如上图所示. 代码如下: <!DOCTYPE html> <html> <head> <meta char ...

  6. vc++ 如何添加右键弹出菜单

    一.创建新工程 二.编辑菜单资源 1.添加菜单 按"Ctrl+R",双击"Menu"图标 2.于菜单编辑器内编辑菜单 四.添加代码(红色部分) void CCM ...

  7. 创建 iPhone/iOS8 弹出菜单(窗口)

    基本步骤 添加视图:主视图与弹出视图 关联视图 配置弹出视图 编码实现:弹出菜单样式及控制器委托 override func prepareForSegue(segue: UIStoryboardSe ...

  8. android 单选、多选弹出菜单

    菜单单选窗口: import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInte ...

  9. [Flex] PopUpButton系列 —— 弹出菜单的行高设置

    <?xml version="1.0" encoding="utf-8"?> <!--Flex中如何通过variableRowHeight样式 ...

随机推荐

  1. 细说 Java 的深拷贝和浅拷贝

    版权声明: 本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有. 未经允许,不得转载. 一.前言 任何变成语言中,其实都有浅拷贝和深拷贝的概念,Java 中也不例外.在对一个现 ...

  2. nodejs 文件读取一行

    作者QQ:1095737364    QQ群:123300273     欢迎加入!   废话没有,直接上代码: app.get('/company', function (req, res, nex ...

  3. Dom的增删查改以及常用事件

    dom的增删查改 // 查 var _input = document.getElementById('_input'); var _div = document.getElementsByClass ...

  4. 关于JS递归函数细化认识及实用实例

    程序调用自身的编程技巧称为递归( recursion). 一个过程或函数在其定义或说明中又直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归 ...

  5. 利用intellijidea创建maven多模块项目

    一.项目结构 multi-module-PRoject是主工程,里面包含两个模块(Module): web-app是应用层,用于界面展示,依赖于web-service参的服务. web-service ...

  6. Netty SSL安全配置

    Netty SSL安全配置 [TOC] 摘要 在研发蜂鸟部署平台的过程中,涉及到平台网关和前置agent的通信加密,虽然目前软件在内网中,但是由于蜂鸟平台和agent的特殊性,一旦被控制,部署的软件就 ...

  7. 改造继续之eclipse集成tomcat开发spring mvc项目配置一览

    在上一篇的环境配置中,你还只能基于maven开发一个javase的项目,本篇来看如果开发一个web项目,所以还得配置一下tomcat和spring mvc. 一:Tomcat安装 在.net web开 ...

  8. PyQt4简单小demo

    #coding=utf-8 import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class FontPropertiesDl ...

  9. React渲染问题研究以及Immutable的应用

    写在前面 这里主要介绍自己在React开发中的一些总结,关于react的渲染问题的一点研究. 另外本人一直希望在React项目中尝试使用,因此在之前已经介绍过immutable的API,可以参看这里I ...

  10. 入侵拿下DVBBS php官网详细过程(图)

    几 个月前,DVBBS php2.0暴了一个可以直接读出管理员密码的sql注入漏洞,当时这个漏洞出来的时候,我看的心痒,怎么还会有这么弱智的漏洞,DVBBS php2.0这套代码我还没仔细看过,于是5 ...