单击JTextArea时显示

单击JTextArea时显示

我正在创建一个简单的即时贴应用程序。我想创建一个JPopupMenu以在我单击JTextArea时显示。因为是便签,所以整个应用显然是textArea

短代码:

    //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){

    }
}

最佳答案

我已经尽力跟随SSCE


假设代码甚至不编译,怎么可能是SSCCE?尝试再次阅读链接。

我建议您先阅读Bringing Up a Popup Menu的Swing教程中的部分,以获取有效示例。


  嗯,我在这里想念什么


您丢失了@Override语句,该语句在覆盖方法时应位于方法签名之前。这样可以防止您输入错误。

07-24 09:21