Swing-JPopupMenu弹出菜单用法-入门

时间:2022-06-23 06:29:28

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

下面是JPopupMenu常用方法:

Swing-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:

Swing-JPopupMenu弹出菜单用法-入门

运行效果图2:

Swing-JPopupMenu弹出菜单用法-入门

图标目录:

Swing-JPopupMenu弹出菜单用法-入门

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&plus;CSS制作二级横向弹出菜单,略简单

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

  6. vc&plus;&plus; 如何添加右键弹出菜单

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

  7. 创建 iPhone&sol;iOS8 弹出菜单(窗口)

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

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

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

  9. &lbrack;Flex&rsqb; PopUpButton系列 —— 弹出菜单的行高设置

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

随机推荐

  1. 过滤Xss

    /** * 防xss过滤 * * @author rentingshuang <tingshuang@rrkd.cn> * @param type $string * @param typ ...

  2. 每天一个 Linux 命令(13):less 命令

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性. 在 more 的时候,我们并没有办法向前面 ...

  3. spring AOP AspectJ 定义切面实现拦截

    总结记录一下AOP常用的应用场景及使用方式,如有错误,请留言. 1.  讲AOP之前,先来总结web项目的几种拦截方式    A:  过滤器 使用过滤器可以过滤URL请求,以及请求和响应的信息,但是过 ...

  4. Who Gets the Most Candies&quest; POJ - 2886 (线段树)

    按顺时针给出n个小孩,n个小孩每个人都有一个纸,然后每个人都有一个val,这个val等于自己的因子数,如果这个val是正的,那就顺时针的第val个孩子出去,如果是负的话,就逆时针的第val个孩子出去, ...

  5. JSAP102

    JSAP102 1.API //案例:禁用文本框 <body> <input type="button" value="禁用" id=&quo ...

  6. 使用Nodpad&plus;&plus;正则替换

    例如有以下格式数据: 现在需要将每一行的10位长度的数字串后面增加等号“=”

  7. 读取web&period;config和app&period;config配置文件

    app.config:       <add key="Password" value="123456"/> C#:   string TQpwd ...

  8. python 四种数值类型&lpar;int&comma;long&comma;float&comma;complex&rpar;介绍

    Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下. 数字数据 ...

  9. SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例

    1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...

  10. hadoop源码分析

    hadoop 源代码分析(一) Google 的核心竞争技术是它的计算平台.HadoopGoogle的大牛们用了下面5篇文章,介绍了它们的计算设施. GoogleCluster:http://rese ...