我有一个 JRadioButton 并为其设置了一个 ComponentPopupMenu。
JRadioButton rdbtnTest = new JRadioButton();
rdbtnTest.setComponentPopupMenu(popupMenu);
popupMenu 是一个JPopupMenu
with one 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?打开网页时,我需要根据右键单击哪个单选按钮传递一个特定的参数。