简体   繁体   English

如何等待鼠标点击

[英]How to wait for a mouse click

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 . 有没有一种方法可以在单击鼠标后打印System.out.println(“在鼠标单击后打印此行”)行。 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() . 除了在UserButtonActionPerformed()函数中执行此操作外。 Please let me know . 请告诉我 。

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 actionPerformed method that would "enable" a mouse listener (which you added earlier). 更好的方法是在actionPerformed方法中具有一个标志,以“启用”鼠标侦听器(您之前已添加)。 This listener would check the flag on each click and when set to true it would flip the flag (to false ) and process the event... 该侦听器将在每次单击时检查标志,并将其设置为true ,它将翻转标志(为false )并处理事件...

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编码约定将有助于人们将来回答您的问题。

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 actionPerformed callback. 这意味着您可以将侦听器附加到GUI元素,但是唯一可以“等待”单击的操作是在actionPerformed回调中执行代码。

There is no way to pause the execution since the addActionListener doesn't do anything to effectively catch the event, it just adds the listener. 由于addActionListener并没有做任何有效捕获事件的操作,它只是添加侦听器,因此无法暂停执行。 Theoretically you could lock the main thread waiting to be notified by the event dispatch one but that would just be bad design. 从理论上讲,您可以锁定等待事件分发之一通知的主线程,但这只是一个糟糕的设计。

You can always wait in UserButtonActionPerformed by defining it in the same class. 您始终可以通过在同一类中定义UserButtonActionPerformed来等待。 If that is the case then you should not have the problem you are facing 如果是这种情况,那么您就不会遇到问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM