2017-03-04 20 views
0

這是我在修改幾個小時後想出的。可悲的是,它給我留下了一個空洞的高分場面。用一個高分例子來練習IO

​​

這就是.txt文件的樣子。

2500,Peter 
2400,Elisabeth 
2200,Josje 
1900,Sebastiaan 
1500,Petra 
1500,Jozef 
1500,Dave 
1400,Karen 
1200,Kristel 
1000,Jules 

高分由\ n分隔。分數和名稱用「,」分隔。

public class HighScoresView extends GridPane { 
private Label naamKop; 
private Label scoreKop; 

private Label[] namen; 
private Label[] scores; 

public HighScoresView() { 
    initialiseNodes(); 
    layoutNodes(); 
} 

private void initialiseNodes() { 
    naamKop = new Label("Naam"); 
    scoreKop = new Label("Score"); 
    namen = new Label[HighScores.AANTAL_HIGHSCORES]; 
    scores = new Label[HighScores.AANTAL_HIGHSCORES]; 
    for (int i = 0; i < HighScores.AANTAL_HIGHSCORES; i++) { 
     namen[i] = new Label(""); 
     scores[i] = new Label(""); 
    } 
} 

private void layoutNodes() { 
    setGridLinesVisible(true); 

    naamKop.setPadding(new Insets(2, 10, 8, 10)); 
    naamKop.setPrefWidth(120); 
    scoreKop.setPadding(new Insets(2, 10, 8, 10)); 
    scoreKop.setPrefWidth(120); 

    add(naamKop, 0, 0); 
    add(scoreKop, 1, 0); 

    for (int i = 0; i < HighScores.AANTAL_HIGHSCORES; i++) { 
     add(namen[i], 0, (i + 1)); 
     add(scores[i], 1, (i + 1)); 
     namen[i].setPadding(new Insets(5, 0, 5, 10)); 
     scores[i].setPadding(new Insets(5, 0, 5, 10)); 
    } 
} 

Label[] getNaamLabels() { 
    return namen; 
} 

Label[] getScoreLabels() { 
    return scores; 
} 

public void setNamen(Label[] namen) { 
    this.namen = namen; 
} 

public void setScores(Label[] scores) { 
    this.scores = scores; 
} 


} 
+0

Soo,你在哪裏設置場景內容? – n247s

+0

view.setNamen(namen); view.setScores(scores); – m4t5k4

+1

很抱歉,我可能不太清楚。你能發佈HighScore視圖類嗎?此外,你甚至使用了什麼前端api?現在它的巫術找出爲什麼場景(內容)不能正常工作...... – n247s

回答

0

通過更改以下內容來實現updateView()工作。 (演示者類)

private void updateView() { 
    String[] namen = new String[10]; 
    String[] scores = new String[10]; 
    String[] strings; 
    try { 
     Scanner scanner = new Scanner(new File(HighScoresModel.getBESTANDSNAAM())); 
     scanner.useDelimiter(System.lineSeparator()); 
     for (int i = 0;i<10;i++){ 
      strings = scanner.nextLine().split(","); 
      namen[i] = strings[1]; 
      scores[i] = strings[0]; 
     } 
     view.setNaamText(namen); 
     view.setScoresText(scores); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

} 

添加了不同的setters,雖然作業表示該類已完成。 (查看課程)

public void setNaamText(String[] namen) { 
    for (int i=0;i<namen.length;i++) { 
     this.namen[i].setText(namen[i]); 
    } 
} 

public void setScoresText(String[] scores) { 
    for (int i=0;i<scores.length;i++) { 
     this.scores[i].setText(scores[i]); 
    } 
}