Java: while loop freezes program

风流意气都作罢 提交于 2019-12-04 06:59:59

问题


I'm making a game and i need to update the JProgressBar every 3 seconds. To do that i use a while loop. 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). 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?


回答1:


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.




回答2:


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.

You should use SwingUtilities.invokeLater to do that.




回答3:


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.

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



来源:https://stackoverflow.com/questions/40793559/java-while-loop-freezes-program

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