2017-03-04 42 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]); 
    } 
}