使组件在Swing中拖动变得不那么敏感 [英] Making a component less sensitive to Dragging in Swing

查看:95
本文介绍了使组件在Swing中拖动变得不那么敏感的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个 JComponent 正在激发 mouseDragged 事件。当用户尝试点击时,即使鼠标只移动了1个像素,它也解释为拖动。



如何为特定组件添加规则相当于:


不要将其视为拖动事件,除非
鼠标从
点移动了10个像素在那里被按下。


注意:我知道这不是我的操作系统中的系统设置,因为只有该组件上的事件遭受



谢谢。

解决方案

ve必须这样做。这是我的鼠标事件处理代码,裁剪到仅仅是与拖动相关的位,需要几个像素才被视为一个拖动。

  public void mousePressed(int mod,Point loc){
pressLocation = copyLocation(loc,pressLocation);
dragLocation = null;
}

public void mouseReleased(int mod,Point loc){
if(pressLocation!= null&& dragLocation!= null){
//鼠标拖动恢复为鼠标点击 - 未拖动足够远
//单击操作
pressLocation = null;
}
else if(dragLocation!= null){
//动作拖动完成
}
else {
// do nothing
}

pressLocation = null;
dragLocation = null;
}

public void mouseDragged(int mod,Point loc){
if(pressLocation!= null){//鼠标按下后的初始拖动操作
dragLocation = pressLocation; //考虑拖动从起始点
if(Math.abs(loc.x-pressLocation.x)< dragMinimum&& Math.abs(loc.y-pressLocation.y)< dragMinimum) {
return; //没有拖动到足够多的数据作为拖动(还)
}
//从新闻位置拖动的动作
pressLocation = null;
}
else {
//从最后一个拖动位置拖动的动作
dragLocation = copyLocation(loc,dragLocation);
}
}

注意,我也遇到了 Java 一些JVM生成的点击事件拖动后,我不得不检测和抑制。


A JComponent of mine is firing a mouseDragged event too vigorously. When the user is trying to click, it interprets is as a drag even if the mouse has only moved 1 pixel.

How would I add a rule for a particular component that amounted to:

Do not consider it a drag event unless the mouse has moved 10 pixels from the point at which is was pressed down.

Note: I know it's not a system setting in my OS, since only events on that component suffer from this over sensitivity.

Thank you.

解决方案

I've had to do exactly this before. Here's my mouse event processing code, cut down to just the bits relating to making drag require a few pixels before being treated as a drag.

public void mousePressed(int mod, Point loc) {
    pressLocation=copyLocation(loc,pressLocation);
    dragLocation=null;
    }

public void mouseReleased(int mod, Point loc) {
    if(pressLocation!=null && dragLocation!=null) {
        // Mouse drag reverted to mouse click - not dragged far enough
        // action for click
        pressLocation=null;
        }
    else if(dragLocation!=null) {
        // action for drag completed
        }
    else {
        // do nothing
        }

    pressLocation=null;
    dragLocation=null;
    }

public void mouseDragged(int mod, Point loc) {
    if(pressLocation!=null) {                                                   // initial drag actions following mouse press
        dragLocation=pressLocation;                                             // consider dragging to be from start point
        if(Math.abs(loc.x-pressLocation.x)<dragMinimum && Math.abs(loc.y-pressLocation.y)<dragMinimum) {
            return;                                                             // not dragged far enough to count as drag (yet)
            }
        // action drag from press location
        pressLocation=null;
        }
    else {
        // action drag from last drag location
        dragLocation=copyLocation(loc,dragLocation);
        }
    }

And note, I also had problems with Java some JVM's generating click events after dragging, which I had to detect and suppress.

这篇关于使组件在Swing中拖动变得不那么敏感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆