2011-12-30 43 views
1

我正在創建一個簡單的便籤應用程序。當我點擊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){ 

    } 
} 
+0

嗯我很想念這裏的東西。 'JTextArea'是一個組件,所以我應該使用'e.getComponent'? – Sobiaholic 2011-12-30 21:51:42

回答

3

我已經盡我所能遵循SSCE

這怎麼可能是一個SSCCE考慮到代碼甚至沒有編譯?嘗試再次閱讀鏈接。

我建議你首先閱讀Swing教程中關於Bringing Up a Popup Menu的部分,作爲一個工作示例。

嗯,我在這裏的東西

你缺少@Override語句時,你重寫的方法應該preceed方法簽名。這將防止您輸入錯誤。

+3

他還缺少setComponentPopupMenu方法:http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#setComponentPopupMenu%28javax.swing.JPopupMenu%29 – 2011-12-30 22:56:37