JLabel 文本未显示,JFrame 在等待时冻结

问题描述

我正在编写一个石头剪刀布 GUI 应用程序,在我的一个 JPanel 中,我有一个 JLabel。这个 JLabel 应该说 Rock 一秒钟,然后是 Paper 一秒钟,然后是 Scissors 一秒钟。到目前为止,它只在最后显示了 Shoot 的 JLabel 文本。我似乎无法弄清楚为什么。有人可以帮忙吗?

代码

import javax.swing.*;
import java.awt.*;

class RockPaperScissoRSShoot {
    public JFrame shootFrame;
    JPanel shootPanel;
    JLabel shootLabel;
    int flagThing = 0;

    public RockPaperScissoRSShoot(Window gameWindow) {
        shootFrame = new JFrame();
        shootFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        shootFrame.setSize(250,150);

        shootPanel = new JPanel(new GridLayout(1,1));
        shootFrame.add(shootPanel);

        shootLabel = new JLabel();
        shootPanel.add(shootLabel);

        shootFrame.setVisible(true);

        shootFrame.setTitle("Rock!"); // the title changes
        shootLabel.setText("Rock!"); // but the JLabel text does not

        waitForMilSecs(1000);

        shootFrame.setTitle("Paper!");
        shootLabel.setText("Paper!");

        waitForMilSecs(1000);

        shootFrame.setTitle("Scissors!");
        shootLabel.setText("Scissors!");

        waitForMilSecs(1000);

        shootFrame.setTitle("Shoot!");
        shootLabel.setText("Shoot!"); //Except here,right here,the text shows up
    }

    public static void waitForMilSecs(long ms) {
        long checkMS = System.currentTimeMillis() + ms;
        while (System.currentTimeMillis() <= checkMS) {
            System.out.println("Not yet!");
        }
        System.out.println("Now!");
    }
}

编辑:我尝试添加一个 SwingWorker 来处理单独线程中的等待。我仍然得到一个冻结的 GUI。代码

import java.awt.*;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
//import javax.swing.event.ChangeEvent;
import javax.swing.SwingWorker.StateValue;

class RockPaperScissoRSShoot {
    public JFrame shootFrame;
    JPanel shootPanel;
    JLabel shootLabel;
    int flagThing = 0;

    public RockPaperScissoRSShoot(Window gameWindow) {
        shootFrame = new JFrame();
        shootFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        shootFrame.setSize(250,1));
        shootFrame.add(shootPanel);

        shootLabel = new JLabel();
        shootPanel.add(shootLabel);

        shootFrame.setVisible(true);

        changeText(false,"Rock!",shootLabel,shootFrame,1);
        changeText(true,"Paper!","Scissors","Shoot!",1);
    }

    public static void changeText(boolean wait,String text,JLabel shootLabel,JFrame shootFrame,long secondsToWait) {
        while (wait) {
            wait = waiter(secondsToWait * 1000);
        }
        shootFrame.setTitle(text);
        shootLabel.setText(text);
    }

    private static boolean waiter(long ms) {
        boolean keepGoing = false;
        SwingWorker sw1 = new SwingWorker() {

            @Override
            protected Boolean doInBackground() throws Exception {
                Thread.sleep(ms);
                // String res = "Finished Execution";
                return true;
            }

            @Override
            protected void done() {
                try {
                    System.out.println(get().toString());
                } catch (InterruptedException e) {
                    e.printstacktrace();
                } catch (ExecutionException e) {
                    // Auto-generated catch block
                    e.printstacktrace();
                }
            }
        };

        // executes the swingworker on worker thread
        sw1.execute();

        while (sw1.getState() != StateValue.DONE) {
            System.out.println("Not done (debug)");
        }
        System.out.println("Done!");
        return false;
    }

    public static void waitForMilSecs(long ms) {
        long checkMS = System.currentTimeMillis() + ms;
        while (System.currentTimeMillis() <= checkMS) {
            System.out.println("Not yet!");
        }
        System.out.println("Now!");
    }

    public static void wait(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            System.out.println("Error waiting,cannot wait lol");
        }
    }
}

谁能向我解释为什么我的 GUI 仍然冻结?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)