我正在做一个游戏,我需要每3秒更新一次JProgressBar。为此,我使用while循环。问题是我的程序由于while循环而冻结(我在其他问题中读过它,它们没有帮助我解决此问题)。我不知道该怎么解决。这是我的代码:
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){} } }
你能帮我吗?
您应该在自己的线程中运行循环:
new Thread( new Runnable() { @Override public void run() { resourceLoader (null); }}).start();
顺便说一句:如果您没有在方法中使用“ String [] args”,则没有理由在方法中声明它。