Java 区分鼠标拖动和鼠标单击/释放

Java 区分鼠标拖动和鼠标单击/释放,java,swing,javafx-2,javafx-8,Java,Swing,Javafx 2,Javafx 8,场景。我有一个图形,我可以使用右键单击来执行平移。这很好用。然后我在右键单击时添加了菜单 问题。现在,即使在拖动操作完成后释放鼠标,也会显示右键单击菜单 在Java Swing或JavaFX中,有没有办法区分鼠标释放和鼠标拖动后的鼠标释放?鼠标事件是独立生成的 我假设平移代码与mousePressed/mouseMoved组合使用 因此,您需要添加一些逻辑来指示您处于“平移模式”。因此,如果一个mousePressed后跟一个mouseMoved,则设置一个布尔变量以指示“平移”模式 然后在Mo

场景。我有一个图形,我可以使用右键单击来执行平移。这很好用。然后我在右键单击时添加了菜单

问题。现在,即使在拖动操作完成后释放鼠标,也会显示右键单击菜单


在Java Swing或JavaFX中,有没有办法区分鼠标释放和鼠标拖动后的鼠标释放?

鼠标事件是独立生成的

我假设平移代码与mousePressed/mouseMoved组合使用

因此,您需要添加一些逻辑来指示您处于“平移模式”。因此,如果一个mousePressed后跟一个mouseMoved,则设置一个布尔变量以指示“平移”模式

然后在MouseRelease代码中,您需要检查变量。如果为“平移模式”,则将“平移模式”设置为关闭并返回。否则,您将处于“弹出模式”,因此可以显示弹出窗口。

因为event.isDragDetect()始终为true,所以我无法区分事件。我创建了一个java类来存储布尔值。这是修改内部类中的最终对象状态所必需的,使用布尔包装类是不可能的。稍后,我将基于鼠标单击和鼠标拖动修改最终对象状态。我还在检查鼠标释放是在拖动之后还是不拖动,如下所示:-

private void addRightClickMenu() {
    final SubMarineBooleanUtilityClass showMenu = new SubMarineBooleanUtilityClass(true);
    final MenuItem graphButton1 = new MenuItem("Save Graph as..");
    graphButton1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            saveGraphAs();
        }
    });

    final MenuItem graphButton2 = new MenuItem("Reset Graph");

    graphButton2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            resetGraph(controlGraph1.getLineChart(), controlGraph2.getLineChart());
        }
    });

    final ContextMenu menu = new ContextMenu(graphButton1, graphButton2);

    //Mouse Drag operation cycle=Mouse click+Mouse dragged+Mouse release

    getAnchorPaneGraphView().setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (MouseButton.SECONDARY.equals(event.getButton())) {
                showMenu.setValueBoolean(true);
            }
        }
    });

    getAnchorPaneGraphView().setOnMouseReleased(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (MouseButton.SECONDARY.equals(event.getButton()) && showMenu.isValueBoolean()) {
                menu.show(getAnchorPaneGraphView(), event.getScreenX(), event.getScreenY());
            }
        }
    });

    getAnchorPaneGraphView().setOnMouseDragged(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (MouseButton.SECONDARY.equals(event.getButton())) {
                showMenu.setValueBoolean(false);
            }
        }
    });
}

public class SubMarineBooleanUtilityClass {
boolean valueBoolean=false;

/**
 * @return boolean
 */
public boolean isValueBoolean() {
    return valueBoolean;
}


/** 
 * Constructor passing a boolean values
 * @param value
 */
public SubMarineBooleanUtilityClass(boolean value){
    this.valueBoolean=value;
}

/**set boolean value
 * @param value
 */
public void setValueBoolean(boolean valueBoolean) {
    this.valueBoolean = valueBoolean;
}
private void addRightClickMenu(){
final SubmarineboleAnutityClass showMenu=新的SubmarineboleAnutityClass(真);
最终菜单项图形按钮1=新菜单项(“将图形另存为…”);
graphButton1.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
saveGraphAs();
}
});
最终菜单项图形按钮2=新菜单项(“重置图形”);
graphButton2.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
resetGraph(controlGraph1.getLineChart(),controlGraph2.getLineChart());
}
});
最终上下文菜单=新上下文菜单(图形按钮1、图形按钮2);
//鼠标拖动操作周期=鼠标单击+鼠标拖动+鼠标释放
getAnchorPaneGraphView(){
@凌驾
公共无效句柄(MouseeEvent事件){
if(MouseButton.SECONDARY.equals(event.getButton())){
showMenu.setValueBoolean(真);
}
}
});
getAnchorPaneGraphView().setOnMouseReleased(新的EventHandler()){
@凌驾
公共无效句柄(MouseeEvent事件){
if(MouseButton.SECONDARY.equals(event.getButton())&&showMenu.isValueBoolean()){
show(getAnchorPaneGraphView(),event.getScreenX(),event.getScreenY());
}
}
});
getAnchorPaneGraphView(){
@凌驾
公共无效句柄(MouseeEvent事件){
if(MouseButton.SECONDARY.equals(event.getButton())){
showMenu.setValueBoolean(假);
}
}
});
}
公共级潜艇油性等级{
布尔值布尔=假;
/**
*@返回布尔值
*/
公共布尔值isValueBoolean(){
返回值布尔;
}
/** 
*传递布尔值的构造函数
*@param值
*/
公共SubmarineboleAnuUtilityClass(布尔值){
this.valueBoolean=值;
}
/**设置布尔值
*@param值
*/
public void setValueBoolean(布尔值boolean){
this.valueBoolean=valueBoolean;
}

}

课堂上应该有你所需要的大部分内容。仅供参考,单击鼠标与按下和释放鼠标不同。如果我正确,则鼠标拖动操作包括=按下鼠标+移动鼠标+释放鼠标。因此,如何区分独立鼠标释放和鼠标移动后的鼠标释放>希望我能够提出正确的问题我共享的是一个界面看看你想要使用什么实现!现在,您的目标ui框架是什么?swing还是fx?答案会有所不同(fx有专门的拖动循环事件)…感谢camickr,我已经做了类似的代码更改,现在它工作得非常完美。再次感谢。