用高分榜样练习IO

时间:2017-03-04 02:50:59

标签: java io mvp

这是我在修补几个小时后想出来的。可悲的是,它留给我一个空洞的高分场景。

public HighScoresPresenter(HighScoresModel model, HighScoresView view) {
    this.model = model;
    this.view = view;
    addEventHandlers();
    updateView();
}

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

}

这就是.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;
}


}

1 个答案:

答案 0 :(得分: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();
    }

}

添加了不同的setter,虽然作业说这个类已经完成了。 (查看课程)

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]);
    }
}