How to wait for a mouse click

拜拜、爱过 提交于 2019-12-11 00:32:15

问题


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 .


回答1:


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




回答2:


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). 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...




回答3:


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).

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.




回答4:


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.

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. Theoretically you could lock the main thread waiting to be notified by the event dispatch one but that would just be bad design.



来源:https://stackoverflow.com/questions/12667074/how-to-wait-for-a-mouse-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!