2
我有一個JRadioButton併爲其設置了一個ComponentPopupMenu。JPopupMenuItem獲取觸發JPopupMenu的組件
JRadioButton rdbtnTest = new JRadioButton();
rdbtnTest.setComponentPopupMenu(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
用相同的彈出菜單,我能確定這兩個單選按鈕的觸發JPopupMenu的?打開網頁時,我需要根據右鍵單擊哪個單選按鈕來傳遞特定參數。
+1使用'setComponentPopupMenu()'。 – trashgod