java 如何等待鼠标点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12667074/
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-31 09:45:57  来源:igfitidea点击:

How to wait for a mouse click

javaswingactionlistenermouseclick-event

提问by Curious Guy 007

class GameFrameClass extends javax.swing.JFrame  {

    public void MyFunc()
    {
        UserButton.setText(Str);
        UserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UserButtonActionPerformed(evt);
            }
        });
        getContentPane().add(UserButton);
    }

    private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {

        //print some stuff after mouse click
    }
}

In someother class i define this function

在另一个类中,我定义了这个函数

void functionAdd()
{
    GameFrameClass gfc = new GameFrameClass()
    gfc.MyFunc()
    System.out.println("PRINT THIS AFTER MOUSE CLICK")  
}

If someone can look into this code. I want to wait on the mouse click . Is there a way i can print the line System.out.println("PRINT THIS AFTER MOUSE CLICK") after the mouse is being clicked . For now this happens immediately and i am not able to wait for the mouse click . Is there a way of doing it ? Apart from doing it inside the function UserButtonActionPerformed() . Please let me know .

如果有人可以查看此代码。我想等待鼠标点击。有没有办法在单击鼠标后打印 System.out.println("PRINT THIS AFTER MOUSE CLICK") 行。现在这会立即发生,我无法等待鼠标点击。有没有办法做到这一点?除了在函数 UserButtonActionPerformed() 中进行。请告诉我 。

采纳答案by Rags

You can always wait in UserButtonActionPerformed by defining it in the same class. If that is the case then you should not have the problem you are facing

你总是可以通过在同一个类中定义 UserButtonActionPerformed 来等待它。如果是这种情况,那么您不应该遇到您面临的问题

回答by MadProgrammer

This is a really "bad" way to do it...

这是一个非常“糟糕”的方式来做到这一点......

private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            System.out.println("PRINT THIS AFTER MOUSE CLICK");
            removeMouseListener(this);
        }
    });
}

A better way would be to have a flag in the actionPerformedmethod that would "enable" a mouse listener (which you added earlier). This listener would check the flag on each click and when set to trueit would flip the flag (to false) and process the event...

更好的方法是在actionPerformed方法中设置一个标志来“启用”鼠标侦听器(您之前添加的)。这个监听器会在每次点击时检查标志,当设置为true它时,它会翻转标志(to false)并处理事件......

回答by LanguagesNamedAfterCofee

It's hard to tell from the wording, but I assume he or she simply wants to execute code after the button is triggered (and not actually wait). For that, you need to add the code inside the method being invoked inside the actionlistener (in this case UserButtonActionPerformed).

从措辞上很难判断,但我认为他或她只是想在按钮被触发后执行代码(而不是实际等待)。为此,您需要在 actionlistener 中调用的方法中添加代码(在本例中为UserButtonActionPerformed)。

So:

所以:

private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {

 System.out.println(...);

}

Also, following the Java coding conventions will help people answering your questions in the future.

此外,遵循 Java 编码约定将有助于人们在未来回答您的问题。

回答by Hyman

Events are managed on a different thread which is the event dispatching thread, they are not managed by the thread that is executing your code (which presumably is the main thread).

事件在不同的线程上管理,该线程是事件调度线程,它们不受执行代码的线程(大概是主线程)的管理。

This means that you can attach listeners to GUI elements but the only thing you can do to "wait" for the click is to execute the code inside the actionPerformedcallback.

这意味着您可以将侦听器附加到 GUI 元素,但您可以“等待”单击的唯一方法是执行actionPerformed回调中的代码。

There is no way to pause the execution since the addActionListenerdoesn't do anything to effectively catch the event, it just adds the listener. Theoretically you could lock the main thread waiting to be notified by the event dispatch one but that would just be bad design.

没有办法暂停执行,因为addActionListener它没有做任何事情来有效地捕获事件,它只是添加了侦听器。从理论上讲,您可以锁定主线程等待事件调度通知,但这只是糟糕的设计。