我正在創建一個簡單的便籤應用程序。當我點擊JTextArea
時,我想要顯示JPopupMenu
。因爲它是一個便條如此明顯的整個應用程序將是一個textArea
在Java中的JTextArea上RightClick時顯示JPopupMenu?
短代碼:
//I've tried my best to follow SSCE
private JTextArea textArea = new JTextArea();
private JPopupMenu popup = new JPopupMenu("Popup Menu");
private JMenuItem hideBar = new JMenuItem("Hide Bar");
private JMenuItem hideTitle = new JMenuItem("Hide Item");
public mySticky(){
add(textArea); //Text Area is using the whole Frame "Sticky Note"
popup.add(hideBar); //adding MenuItem
popup.add(hideTitle); //adding MenuItem
//addMouseListener(new popupTriggerListener());
textArea.addMouseListener(new popupTriggerListener());
}
private class popupTriggerListener extends MouseAdapter{
public void MousePressed(MouseEvent e){
if(e.isPopupTrigger())
popup.show(textArea,e.getX(),e.getY()); //I've added texArea I'm not sure what to add inside.
}
public void MouseReleased(MouseEvent e){
if(e.isPopupTrigger())
popup.show(textArea,e.getX(),e.getY()); //I've added texArea I'm not sure what to add inside.
}
public void MouseClicked(MouseEvent e){
}
}
嗯我很想念這裏的東西。 'JTextArea'是一個組件,所以我應該使用'e.getComponent'? – Sobiaholic 2011-12-30 21:51:42