java:右键单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7386771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:44:48  来源:igfitidea点击:

java : right click event

javaswingeventsmouse

提问by lola

on TableHeader, I try to make a right click with the mouse as follow :

在 TableHeader 上,我尝试用鼠标右键单击,如下所示:

tableHeader.click(MouseEvent.BUTTON3);

but this is not working, have you any idea/suggestion ?

但这不起作用,你有什么想法/建议吗?

thanks,

谢谢,

回答by Neifen

  • if it is because you want to show a popupmenu then you can do it like this::

    popupMenu.show(tableHeader, 0, 0);

  • 如果是因为你想显示一个弹出菜单,那么你可以这样做:

    popupMenu.show(tableHeader, 0, 0);



  • if it is because you want to get into a mouselistener like:

    class MyMouseListener implements MouseListener{
    @Override
    public void mouseReleased(MouseEvent arg0) {
        if(SwingUtilities.isRightMouseButton(arg0)){
            //my code
        }
    }
    //...
    
  • 如果是因为您想进入鼠标侦听器,例如:

    class MyMouseListener implements MouseListener{
    @Override
    public void mouseReleased(MouseEvent arg0) {
        if(SwingUtilities.isRightMouseButton(arg0)){
            //my code
        }
    }
    //...
    

then you can put your code into a method and open the method

然后您可以将您的代码放入一个方法中并打开该方法

 //open the method:
 myMouseListenerReplacement();


//the method
private void myMouseListenerReplacement(){
 //My code
}