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

弹出菜单是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:

图标目录:

时间: 2024-10-27 11:20:51

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

弹出菜单的创建与使用

-------------siwuxie095 工程名:TestSwingPopupMenu 包名:com.siwuxie095.popupmenu 类名:MyFrame.java 工程结构目录如下: MyFrame.java: package com.siwuxie095.popupmenu; import java.awt.BorderLayout; import java.awt.Component; import java.awt.EventQueue; import java.awt.

iOS开发——实战技术OC篇&关于蒙板和弹出菜单

关于蒙板和弹出菜单 一:关于蒙板 蒙板的作用一般就是用来实现不能做其他操作还有一些模糊效果提示用户 我们只需要自定义一个View,并且创建两个类方法用来给外界调用实现显示和隐藏 + (void)show; + (void)hide; 显示:直接添加到window上,并且设置和window同样大小,然后设置相应的透明度(alpla) 1 + (void)show 2 3 { 4 5 6 7 8 9 iCocosCover *cover = [[self alloc] init]; 10 11 12

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

本文将使用css+html+js实现横向菜单.具有多级弹出菜单下拉. 首先我们来看看效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvajkwMzgyOTE4Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 首先应该写html部分的代码,代码比較简单,代码例如以下: <body> <div id="men

css+html+js实现下拉及多级弹出菜单

本文将使用css+html+js实现横向菜单,具有下拉的多级弹出菜单. 首先我们来看看效果图: 首先应该写html部分的代码,代码比较简单,代码如下: <body> <div id="menu"> <ul> <li><a href="#" id="current">首页</a></li> <li><a href="#">

第四天 纵向导航菜单及二级弹出菜单

http://www.aa25.cn/div_css/905.shtml 今天我们开始学习<十天学会web标准(div+css)>的纵向导航菜单及二级弹出菜单,包含以下内容和知识点: 纵向列表 标签的默认样式 css派生选择器 css选择器的分组 纵向二级列表 相对定位和绝对定位 一.纵向列表 纵向列表或称为纵向导航,在网站的产品列表中应用比较广泛,如淘宝网左侧的淘宝服务,今天我们就学习一下纵向导航的制作 先新建一个页面,然后插入一个ID为menu的div,然后在设计视图中选中文字,点击工具栏

Android ListView两种长按弹出菜单方式

* 知识点1:ListView item:两种长按弹出菜单方式* 知识点2:ListView SimpleAdapter的使用*  知识点 3:在java代码中创建一个ListView*/ -----------------------------------------------------Activity[mw_shl_code=java,true]package org.gxl.com; public class ListOnLongClickActivity extends Activ

弹出菜单 阻止冒泡

<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8"> <title>弹出菜单,阻止冒泡</title> <style> </style> <script src="jquery-1.9.1.js"></script> <script> $(fu

糍粑大叔的独游之旅-u3d实现弹出菜单(上)-动态列表

在u3d5.x中,使用ugui作为默认的界面系统,但控件实在太少,很多需求都不能满足,比如弹出菜单(PopupMenu) 我也懒得去网上找现成的实现,再加上现有代码已经有很多有关列表控件的功能,不想再重新动这些代码. 所以自己实现一个,目前先只实现核心.搭建控件相关类的骨干,后期再慢慢丰富和做的更花哨. 开篇之前声明,我的u3d理解非常有限,有很多也许本身自带的功能或有现成库功能我不知道,所以选择了自己探索或实现, 感觉太low欢迎给出好的意见. 定义和代码结构 PopupMenu是点击鼠标或按

第5章(7) 弹出菜单(PopUp Menus)

分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果用户在菜单外单击,则直接消失弹出的菜单.当菜单消失时,会引发DismissEvent事件(利用此事件可在菜单消失时做一些后续处理). 二.示例7-- Demo07PopupMenu 1.运行效果 2.添加菜单项 在Resources文件夹下添加一个menu子文件夹,然后在此子文件夹下添加一个名为de