- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法计算出Java中鼠标的第一次拖动移动?
例如:
public void mouseDragged(MouseEvent e)
{
if (first drag of the mouse) // what should I write here?
System.out.println("This is the first drag");
else System.out.println("This isn't the first drag");
}
如果我拖动鼠标 5 次,我应该在控制台中看到以下文本:
This is the first drag
This isn't the first drag
This isn't the first drag
This isn't the first drag
This isn't the first drag
最佳答案
boolean first=true;//this should be a instance variable.
先拖!使用 boolean 变量来检测是否首先检测。就像这样
public void mouseDragged(MouseEvent e)
{
if (first) { // this is only true if it's first drag
System.out.println("This is the first drag");
first=false;
}
else {
System.out.println("This isn't the first drag");
}
}
更新...
这是你如何检测是否是第一次拖动的方法。但是通常在拖动时会触发鼠标拖动事件。为了避免这种情况,你可以修改这一点。
声明实例变量
boolean draging = true;
boolean mark = true;
boolean first = true;
仅在拖动开始时打印。当我们打印鼠标拖动时,我们会停止打印它,直到释放鼠标并重新拖动。
public void mouseDragged(java.awt.event.MouseEvent evt) {
draging = true;
if (mark) {
if (first) {
System.out.println("This is the first drag");
}else{
System.out.println("This isn't the first drag");
}
mark = false;
}
}
将第一个更改为 false,这样第一次拖动就足够了。并准备好打印新的拖动[mark = true]
public void mouseReleased(java.awt.event.MouseEvent evt) {
if (draging) {
mark = true;
first=false;
}
}
这是第一个和更新示例的输出。第一个代码存在问题[因为事件拖动是在拖动时连续触发的,而不是拖动]。
第一个例子
This is the first drag
This is the first drag
This is the first drag
.............................//this continues until u finish[released] first drag
This isn't the first drag
This isn't the first drag
This isn't the first drag
................................
更新了
This is the first drag //a drag [ click--move--relesed] mark only 1time
This isn't the first drag
This isn't the first drag
This isn't the first drag
...............................
关于java - 第一次鼠标拖动移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29576840/
我是一名优秀的程序员,十分优秀!