简体   繁体   English

GUI不在框架处显示命令

[英]GUI doesn't show the command at the frame

I programmed a little game in Java for school. 我用Java为学校编写了一个小游戏。 We have library with given functions. 我们有具有给定功能的库。 I write a code that repeats a command (while), i checked with the println that the command is running correctly, but in the GUI you can't see the result, only the last result. 我写了一个重复命令的代码(同时),我用println检查了命令是否正确运行,但是在GUI中,您看不到结果,只有最后一个结果。

Project: https://drive.google.com/open?id=0B5Ehxz_FAn6BMjVPOWY5MkgycVk&authuser=0 项目: https//drive.google.com/open?id = 0B5Ehxz_FAn6BMjVPOWY5MkgycVk&authuser = 0

Code: 码:

public void spielen () {
        if (guthaben > 0) {
            if (endlosspielAktiv) {
                while (guthaben > 0) {
                    kasten.Ringe.faerbeUm(); //Here the GUI doesn't show the results
                    ZEICHENFENSTER.gibFenster().warte(500);
                    guthabenRunter();
                }
                setEndlosspielAktiv(false);
            } else {
                kasten.Ringe.faerbeUm();
            }
        } else if (guthaben == 0) {
            setEndlosspielAktiv(false);
        }
        guthabenRunter();
    }

Your GUI is updated only after all your pending and running methods of the calling thread returned. 仅在返回调用线程的所有未决和正在运行的方法之后,才会更新GUI。 In your case the update of the GUI delays until the method spielen() returned. 在您的情况下,GUI的更新会延迟到方法spielen()返回之前。

To have an immediate result, you could use an additional Thread like this: 为了立即得到结果,您可以使用其他线程,如下所示:

public void spielen () {
        new Thread(new Runnable() {

            @Override
            public void run() {
                if (guthaben > 0) {
                    if (endlosspielAktiv) {
                        while (guthaben > 0) {
                            kasten.Ringe.faerbeUm();
                            ZEICHENFENSTER.gibFenster().warte(500);                            
                            guthabenRunter();
                        }
                        setEndlosspielAktiv(false);
                    } else {
                        kasten.Ringe.faerbeUm();
                    }
                } else if (guthaben == 0) {
                    setEndlosspielAktiv(false);
                }
                guthabenRunter();
            }
        }).start();        
    }

All code of spielen() is running asynchronous and doesn't block the method returning. spielen()所有代码都异步运行,并且不会阻止方法返回。

If you want to know more about threading, i can recommend you reading the according chapter of the (german) E-Book http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_14_003.htm 如果您想了解有关线程的更多信息,我建议您阅读(德语)电子书的相应章节,网址为http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_14_003.htm

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

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