简体   繁体   English

while循环可以等待事件继续java

[英]Can a while loop wait for an event to continue java

I have a hangman engine that uses a JOptionPane to get the users guess, but now that I am implementing a GUI with a text field, I need a way to stop the engine and get the users guess from the text field - I'm trying to use MVC so at the moment the data from the text field gets sent to the main/controller class however after the data is loaded into a variable in the main class - I start having trouble integrating that data into the engine 我有一个使用JOptionPane来引起用户猜测的刽子手引擎,但是现在我正在使用文本字段实现GUI,我需要一种方法来阻止引擎并让用户从文本字段中猜出 - 我正在尝试使用MVC所以当前来自文本字段的数据被发送到主/控制器类但是在将数据加载到主类中的变量之后 - 我开始无法将这些数据集成到引擎中

I don't know if adding a wait to the engine would be the best thing but that's all I can really think of. 我不知道在发动机上添加等待是否是最好的事情,但这是我能想到的全部。

Here is my engine class: 这是我的引擎类:

public class engine {
    char [] charda = new char[20];
    char [] charwo = new char[20];
    Highscore words = new Highscore();
    main mn = new main();
    int guesses =7;
    char guess;

    public engine() {
    }

    public void enginer(){

        int count = 0;
        String word = words.getWord();

        for (int i = 0; i<word.length(); i++)
        {
            //instantiates two arrays one of dahses and one with the word
            charwo[count] = word.charAt(i);
            charda[count]= '_';
            count++;
        }

        for (int l=0; l<count; l++)
        {
            System.out.print(" "+charda[l]);
        }

        while (guesses != 0 && !Arrays.equals(charwo, charda))
        {
            System.out.println("");
            guess = JOptionPane.showInputDialog("enter a Guess").charAt(0);

            if (word.toUpperCase().contains(String.valueOf(guess).toUpperCase()))
            {
                for (int k = 0; k<word.length(); k++)
                {
                    if (String.valueOf(guess)
                            .toUpperCase()
                            .equals( String.valueOf(charwo[k]).toUpperCase() ))
                    {
                        charda[k]=charwo[k];

                        for(int l=0; l<count; l++)
                        { // prints dashes here to avoid a letter being 
                          // chopped off by the program stopping in the middle
                            System.out.print(" "+charda[l]);

                        }
                    }
                }
            }
            else
            {
                guesses = guesses-1;
                System.out.println("guesses left "+guesses);
                //
                //Re-displays dashes 
                for (int l=0; l<count; l++)
                {
                    System.out.print(" "+charda[l]);
                }
            }

            if (Arrays.equals(charwo, charda))
            {
                System.out.println("");
                System.out.println("You are Winner");

            }
        }
    }
}

Here is the part of my main class that handles the button click 这是我的主类中处理按钮单击的部分

public void buttonClicked ()
{
    gameplay gp = new gameplay(); 
    engine en  = new engine();

    en.guess = gp.getInput; // this is where I try send the input from the text field to the engine
    en.enginer(); 

    System.out.println("button clicked"); 
}

I'm really lost at the moment so even a nod in the right direction would be really helpful :) 我现在真的迷失了,所以即使是朝着正确的方向点头也会非常有帮助:)

Generally waiting can be implemented using wait/notify built-in java mechanism. 通常可以使用wait/notify内置java机制来实现wait/notify

Although you can find a bunch of tutorials that explain this issue here is a very brief explanation. 虽然你可以找到一堆解释这个问题的教程,但这是一个非常简短的解释。

Each class in java automatically extends java.lang.Object that defines methods wait() and notify() . java中的每个类都自动扩展定义方法wait()notify() java.lang.Object Wait suspends current thread until notify() is called on the same object. 等待挂起当前线程,直到在同一对象上调用notify() The object that is used to invoke wait() and notify() is called monitor . 用于调用wait()notify()对象称为monitor Following rules of java thread management both method must be invoked from synchronized block, ie 遵循java线程管理的规则,必须从synchronized块调用这两个方法,即

synchronized(obj) {
    obj.wait();
}

Now the answer to your question is simple. 现在你的问题的答案很简单。 Define monitor object that is visible from both points (I mean code that should wait and code that should trigger the first thread to continue). 定义从两个点可见的监视器对象(我的意思是应该等待的代码和应该触发第一个线程继续的代码)。

Implement waiting as: 实施等待:

synchronized(obj) {
    obj.wait();
}

When use clicks button (so you want the wait to exit) you should invoke code: 当使用单击按钮(因此您希望等待退出)时,您应该调用代码:

synchronized(obj) {
    obj.notify();
}

That's it. 而已。 Pay attention that there is version of wait with timeout and there is notifyAll() too. 注意有超时的wait版本,也有notifyAll() And IMHO take some time to learn java threads, synchronization etc. It is fun. 恕我直言需要一些时间来学习java线程,同步等等。这很有趣。 Good luck. 祝好运。

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

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