我有一个JRadioButton并为它设置了ComponentPopupMenu。
JRadioButton rdbtnTest = new JRadioButton();
rdbtnTest.setComponentPopupMenu(popupMenu);
popupMenu是JPopupMenu
,其中一个JPopupMenuItem
。 JPopupMenuItem
有MouseListener
,因此点击鼠标即可打开网页。
JPopupMenu popupMenu = new JPopupMenu("GO");
PopupMenuListener popupMenuListener = new MyPopupMenuListener();
popupMenu.addPopupMenuListener(popupMenuListener);
MenuItem openMenuItem = new JMenuItem("Open Webpage");
openMenuItem.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent arg0) {
Component comp = arg0.getComponent(); // will only return the JPopupMenuItem
// Determine the right-clicked radio button and open webpage
}
popupMenu.add(openMenuItem);
我的问题是:如果我有另一个JRadioButton rdbtnTest2
使用相同的popupMenu,我可以确定哪两个单选按钮触发了JPopupMenu?打开网页时,我需要根据右键单击的单选按钮传递一个特定的参数。
答案 0 :(得分:3)
当您在MouseListener中显示弹出菜单时,只需获取事件源并将其存储在某处(调用菜单的单选按钮)。在你的代码中只需检查弹出调用程序。