监听 `for` 循环内的鼠标点击事件,并在以后的代码中使用该事件的坐标。

huangapple go评论75阅读模式
英文:

Listen for a mouse click event inside a for loop and use the coordinates from that event in later code

问题

我想要做的是在我的代码的其余部分等待并识别鼠标点击事件,然后能够在代码的其余部分使用该点击的坐标,而不只是在点击时将其打印到屏幕上,因为我发现几乎所有其他示例都是这样做的。

我是Java的新手,这是我第一次制作界面,所以如果有任何错误,我会尽快修复它们。

不幸的是,由于我不知道如何做我要求的事情,我只能以一种伪代码与代码片段混合的方式来表示它,否则我就不会在这里提问了。

编辑:下面的评论说不要这样做,我理解,但我真的需要能够在代码的那部分中使用那些坐标(代码比下面显示的要长得多),所以如果有人有任何其他方式可以实现这一点,请分享。

真正让我困扰的是在两个不同位置进行两次点击以获得两个不同的坐标,然后进行操作。

.....
for(int i = 0; i < 15; i++) {
    等待鼠标点击;
    int x = 鼠标点击的x坐标;
    int y = 鼠标点击的y坐标;
    等待鼠标点击;
    int x2 = 第二次鼠标点击的x坐标;
    int y2 = 第二次鼠标点击的y坐标;
    if (closestVertex(x,y) != closestVertex(x2,y2) && ! graph.isline(x,y,x2,y2)) {
        graph.insertLine(x,y,x2,y2);
    }
}
.... 等等 ...

到目前为止,我看到的解决方案涉及查看鼠标在给定时刻的位置,但这不是仅在点击时的位置。这种解决方案如下所示:

int x = MouseInfo.getPointerInfo().getLocation.x;
int y = MouseInfo.getPointerInfo().getLocation.y;

还有另一种常见的方法是修改MouseListener,如下所示:

@Override
public void mouseClicked(MouseEvent e) {
    int x=e.getX();
    int y=e.getY();
    System.out.println(x+","+y);
}

但对于这个选项,是的,你可以打印坐标,但我不知道如何在MouseClicked函数之外使用坐标详细信息。

在等待操作发生而不仅仅是继续循环的问题上,我有一些想法。我考虑在内部添加一个while循环,然后使用wait()和Thread.sleep(100),但问题是我不知道如何表达实际上:

while(事件未发生) {
    等待()
}
int x = 鼠标点击的x坐标;
int y = 鼠标点击的y坐标;
... 等等

如果你有任何新的想法我可以尝试,我将不胜感激,因为我已经查看了无数谷歌链接,试图找到使这个工作的方法。

英文:

What I want to do is inside the rest of my code have it wait for and recognise a mouse click event and then be able to use the coordinates of that click in the rest of the code, not just print it to the screen on click as pretty much every other example I have found does.

I am new to java and this is the first time I'm making an interface so I do apologise for any mistakes and I will fix them as soon as I can.

Unfortunately, as I don't have a clue how to do what I am asking for, I can only represent it in a sort of pseudocode crossed with bits of code or I wouldn't be asking here in the first place.

edit: Below comment says don't do this, which I understand but I really need to be able to get those coordinates to be usable in that area of code (which is a lot longer than shown below) so if anyone has any alternative ways in which I could achieve that then please share.

It is the two clicks at two different locations to get two different coordinates to then manipulate that is really throwing me.

.....
for(int i = 0; i &lt; 15; i++) {
    wait for mouse click;
    int x = x coordinate of mouse click;
    int y = y coordinate of mouse click;
    wait for mouse click;
    int x2 = x coordinate of second mouse click;
    int y2 = y coordinate of second mouse click;
    if (closestVertex(x,y) != closestVertex(x2,y2) &amp;&amp; ! graph.isline(x,y,x2,y2)) {
        graph.insertLine(x,y,x2,y2);
    }
}
.... etc...

So far the solutions I have seen have involved looking at where the pointer is at a given moment but that is not the location only at the moment when clicked. This type of solution is illustrated below:

int x = MouseInfo.getPointerInfo().getLocation.x;
int y = MouseInfo.getPointerInfo().getLocation.y;

And also another common one that crops up is altering the MouseListener as below:

@Override
public void mouseClicked(MouseEvent e) {
    int x=e.getX();
    int y=e.getY();
    System.out.println(x+&quot;,&quot;+y);
}

But for this option, yes, you can print the coordinate, but I don't see a way that I can use the coordinate details outside of the mouseClicked function as I would like.

In terms of getting it to wait for the action to occur and not just keep going through the loop, I have a couple of ideas. I thought of adding a while loop inside and using wait() and Thread.sleep(100) but the issue is I had no idea of how to say essentially:

while(event hasn&#39;t occurred) {
    wait()
}
int x = x coordinate of mouse click;
int y = y coordinate of mouse click;
...etc

If you have any new ideas I could try I would greatly appreciate it as I have been through I don't even know how many links on google looking for a way to make this work.

答案1

得分: 1

你需要更改程序的控制流程,使代码能够响应UI事件,而不是尝试运行UI本身。尝试类似于以下的方法:

class MyStateMachine {
  int i = 0; // 这是你的循环索引
  boolean waitingForSecondClick = false; // 下一次点击是第一次还是第二次?
  int firstX; // 第一次点击的坐标
  int firstY;

  void receiveCoordinates(int x, int y) {
    if (waitingForSecondClick) { 
       if (closestvertex(....)) {
         // 做你的操作
       }
       ++i;
       waitingForSecondClick = false;
    } else {
       firstX = x;
       firstY = y;
       waitingForSecondClick = true;
    }
  }
}

因此,你的鼠标监听器变为:

@Override
public void mouseClicked(MouseEvent e) {
    stateMachine.receiveCoordinates(e.getX(), e.getY())
}

第一次你的对象接收鼠标点击时,它只会将坐标保存在它的实例变量中,并记住下一次点击是“第二次点击”。下次点击时,它会使用这两次点击来执行你的任务,递增 i 并开始寻找另一个“第一次点击”。

英文:

You need to change the control flow of your program so that your code is responding to UI events, not trying to run the UI itself. Try something like this:

class MyStateMachine {
  int i = 0; // this is your loop index
  boolean waitingForSecondClick = false; // is the next click the first or second?
  int firstX; // coordinates of the first click
  int firstY;

  void receiveCoordinates(int x, int y) {
    if (waitingForSecondClick) { 
       if (closestvertex(....)) {
         // do your stuff
       }
       ++i;
       waitingForSecondClick = false;
    } else {
       firstX = x;
       firstY = y;
       waitingForSecondClick = true;
    }
  }

So your mouse listener becomes:

@Override
public void mouseClicked(MouseEvent e) {
    stateMachine.receiveCoordinates(e.getX(), e.getY())
}

The first time your object receives a mouse click it just saves the coordinates in its instance variables and remembers that the next click is a 'second click'. Next click it uses both clicks to do your task, increments i and starts looking for another 'first click'.

huangapple
  • 本文由 发表于 2020年9月6日 05:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63758837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定