我正在做一個遊戲,我需要每3秒更新一次JProgressBar。爲此,我使用了一個while循環。問題是我的程序凍結了while循環的原因(我在其他問題中讀過它,它們並沒有幫助我解決這個問題)。我不知道如何解決它。這裏是我的代碼:Java:雖然循環凍結程序
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){}
}
}
你能幫我一下嗎?
雖然循環執行該程序應該怎麼做其他同時嗎? –
你需要在這裏刪除所有不相關的代碼。 – Carcigenicate
看起來你可能會在主應用程序線程中「休眠」。 – Carcigenicate