首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JPopupMenu右键弹出菜单的施用

2012-12-24 
JPopupMenu右键弹出菜单的使用JPopupMenu和JMenuItem的定义://聊天信息JTextPane的右键弹出菜单private JP

JPopupMenu右键弹出菜单的使用

JPopupMenu和JMenuItem的定义:

//聊天信息JTextPane的右键弹出菜单private JPopupMenu msgInfoPopMenu = null;private JMenuItem copyItem = new JMenuItem("复制");private JMenuItem selectAllItem = new JMenuItem("选择全部");private JMenuItem clearItem = new JMenuItem("清屏");

注意:JTextPane一定要注册监听器

?

在右键弹出菜单:

if (e.getSource() == msgShowTxtPane && e.getButton() == MouseEvent.BUTTON3 && e.getClickCount() == 1) {//这样处理是防止msgInfoPopMenu加入多次Itemif (msgInfoPopMenu == null) {msgInfoPopMenu = new JPopupMenu();} else {msgInfoPopMenu.removeAll();}copyItem.addActionListener(this);selectAllItem.addActionListener(this);clearItem.addActionListener(this);msgInfoPopMenu.add(copyItem);msgInfoPopMenu.add(selectAllItem);//分割线msgInfoPopMenu.addSeparator();msgInfoPopMenu.add(clearItem);//显示的位置msgInfoPopMenu.show(e.getComponent(), e.getX(), e.getY());}

?

setIcon()可以设置Item的图片。

?JMenuItem的事件跟JButton的事件一样处理。

热点排行