简体   繁体   English

Java:while 循环冻结程序

[英]Java: while loop freezes program

I'm making a game and i need to update the JProgressBar every 3 seconds.我正在制作一个游戏,我需要每 3 秒更新一次 JProgressBar。 To do that i use a while loop.为此,我使用了一个 while 循环。 The problem is that my program freezes becuse of the while loop (i read it in other questions, they didn't help me to solve this).问题是我的程序由于 while 循环而冻结(我在其他问题中阅读了它,他们没有帮助我解决这个问题)。 I don't know how to solve it.我不知道如何解决。 Here is my code:这是我的代码:

public static void city (String[] args){
        //loading some of the saveData (not all of it is made yet)
    try{
        File saveDataFile = new File("save.dat");
        Scanner saveDataSc = new Scanner(saveDataFile);

        int power = saveDataSc.nextInt();
        int people = saveDataSc.nextInt();
        int food = saveDataSc.nextInt();
        int wood = saveDataSc.nextInt();
        int iron = saveDataSc.nextInt();
        int stone = saveDataSc.nextInt();

        saveDataSc.close();

    } catch (FileNotFoundException ex){}

    frame.remove(introImage4);
    frame.remove(startingScreen);
    frame.add(background1);

        //background properties
    background1.setLayout(null);

        //sideBar properties
    sideBar.setLayout(null);
    sideBar.setBounds(700,0,200,600);

        //button properties
    AltarListB.setBounds(50,20,100,100);
    InventortyB.setBounds(25,515,150,20);
    ArmyB.setBounds(25,545,150,20);
    MessagesB.setBounds(25,485,150,20);
    MissionsB.setBounds(25,455,150,20);
    MapB.setBounds(117,170,65,30);
    FieldB.setBounds(18,170,65,30);
    TownB.setBounds(68,135,65,30);

        //image properties
    underline1.setBounds(0,205,200,31);
    underline2.setBounds(0,426,200,24);

        //prograssbar properties
    powerProg.setMinimum(0);
    peopleProg.setMinimum(0);
    woodProg.setMinimum(0);
    foodProg.setMinimum(0);
    ironProg.setMinimum(0);
    stoneProg.setMinimum(0);

    powerProg.setMaximum(500);
    peopleProg.setMaximum(500);
    woodProg.setMaximum(500);
    foodProg.setMaximum(500);
    ironProg.setMaximum(500);
    stoneProg.setMaximum(500);


    powerProg.setStringPainted(true);
    peopleProg.setStringPainted(true);
    woodProg.setStringPainted(true);
    foodProg.setStringPainted(true);
    ironProg.setStringPainted(true);
    stoneProg.setStringPainted(true);

    powerProg.setValue(power);
    peopleProg.setValue(people);
    woodProg.setValue(wood);
    foodProg.setValue(food);
    ironProg.setValue(iron);
    stoneProg.setValue(stone);

    powerProg.setString("Power: " + power + "/" + "maxPower");
    peopleProg.setString("People: " + people + "/" + "maxPeople");
    woodProg.setString("Wood: " + wood + "/" + "maxWood");
    foodProg.setString("Food: " + food + "/" + "maxFood");
    ironProg.setString("Iron: " + iron + "/" + "maxIron");
    stoneProg.setString("Stone: " + stone + "/" + "maxStone");

    powerProg.setBounds(14,241,170,20);
    peopleProg.setBounds(14,273,170,20);
    woodProg.setBounds(14,305,170,20);
    foodProg.setBounds(14,337,170,20);
    ironProg.setBounds(14,369,170,20);
    stoneProg.setBounds(14,401,170,20);

        //adding stuff
    sideBar.add(AltarListB);
    sideBar.add(InventortyB);
    sideBar.add(ArmyB);
    sideBar.add(MessagesB);
    sideBar.add(MissionsB);
    sideBar.add(MapB);
    sideBar.add(FieldB);
    sideBar.add(TownB);
    sideBar.add(underline1);
    sideBar.add(underline2);
    sideBar.add(powerProg);
    sideBar.add(peopleProg);
    sideBar.add(woodProg);
    sideBar.add(foodProg);
    sideBar.add(ironProg);
    sideBar.add(stoneProg);

    background1.add(sideBar);


    background1.revalidate();
    background1.repaint();
    frame.revalidate();
    frame.repaint();
    resourceLoader(new String[] {"a","b","c"});
}   

public static void resourceLoader (String[] args){
    while(true){
        try{
            File saveDataProgFile = new File("save.dat");
            Scanner saveDataProgSc = new Scanner(saveDataProgFile);

            power = saveDataProgSc.nextInt();
            people = saveDataProgSc.nextInt();
            food = saveDataProgSc.nextInt();
            wood = saveDataProgSc.nextInt();
            iron = saveDataProgSc.nextInt();
            stone = saveDataProgSc.nextInt();

            saveDataProgSc.close();

            powerProg.setValue(power);
            peopleProg.setValue(people);
            woodProg.setValue(wood);
            foodProg.setValue(food);
            ironProg.setValue(iron);
            stoneProg.setValue(stone);

            background1.revalidate();
            background1.repaint();
            frame.revalidate();
            frame.repaint();

            saveDataProgSc.reset();

            try{
                Thread.sleep(3000);
            } catch(InterruptedException e){
                Thread.currentThread().interrupt();
            }
        } catch (FileNotFoundException ex){}
    }
}

Can you please help me with this?你能帮我解决这个问题吗?

You should run your loop in an own Thread:您应该在自己的线程中运行循环:

        new Thread( new Runnable() {
            @Override
            public void run()
            {
                resourceLoader (null);
            }}).start();

BTW: If you do not use the "String[] args" in the method there is no reason to have it declared in the method.顺便说一句:如果您不在方法中使用“String[] args”,则没有理由在方法中声明它。

You will need to use threads for that.您将需要为此使用线程。 But be careful, and don't try to update the GUI component (JProgressBar) from a thread that does not own the progress bar.但要小心,不要尝试从不拥有进度条的线程更新 GUI 组件 (JProgressBar)。

You should use SwingUtilities.invokeLater to do that.您应该使用 SwingUtilities.invokeLater 来做到这一点。

I'm not sure what you're trying to do with this program, but there you might want to investigate using a listener instead of rolling your own regular time interval.我不确定你想用这个程序做什么,但你可能想使用侦听器进行调查,而不是滚动你自己的固定时间间隔。 Also, if your while condition is simply true, it's probably a smell that there is a simpler way to implement the solution - messing with threads is playing with fire.此外,如果您的 while 条件完全正确,则可能有一种更简单的方法来实现解决方案的味道 - 弄乱线程就是在玩火。

https://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html https://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html

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

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