toString不在JApplet

时间:2017-06-02 02:07:35

标签: java arrays tostring

这是我的小程序。

JApplet

public void paint(Graphics g){
    super.paint(g);

    Font font = new Font("monospaced", Font.BOLD, 12);
    g.setFont(font);

    g.setColor(Color.YELLOW);
    g.fillRect(0, 105, 500, 500);
    g.setColor(Color.BLACK);

    g.drawString("hello", 120, 300);

    for(int c = 0; c < 1;){

        for(int b = 250; c < 9; b = b + 10){
            g.drawString(roster[c].toString(), 10, b);
            c++;
        }

    }
    g.drawString(roster[0].toString(), 0, 250);



}
    public Student[] setup(){
    count = 10;
    roster = new Student[count];
    int x = 0; int e = 0;
    int[] gtest = new int[5];

    for(e = 0; e < 5; e++){
         gtest[e] = 0;
    }


    for(x = 0; x < 31; x++){
      roster[x] = new Student("", 0, gtest);
    }

    int[] help = new int[5];
    help[0] = 84; help[1] = 85; help[2] = 86; help[3] = 87; help[4] = 88;

    roster[0] = new Student("Pocket, beam", 2017, new int[] {84, 85, 84, 87, 88, 88});
    roster[1] = new Student("Zuckerberg, Marcus", 2017, help);
    roster[2] = new Student("Timelapse, Random", 2013, help);
    roster[3] = new Student("Timelapse, Random2", 2013, help);
    roster[4] = new Student("Timelapse, Random3", 2016, help);
    roster[5] = new Student("god, not", 2016, help);
    roster[6] = new Student("Webster, Jacques", 2016, help);
    roster[7] = new Student("East, Kanayo", 2016, help);
    roster[8] = new Student("Blue, Canoe", 2017, help);
    roster[9] = new Student("West, East", 2017, help);

    return(roster);

}

我也粘贴了特定的代码,.toString在Graphics方法中。

这是构造函数类中的toString方法。

public String toString(){
    DecimalFormat tens = new DecimalFormat("0.00");
    String ans = String.format("%-20s%-8s %-5s%-5s%-5s%-5s%-5s%-10s", name, year,
    grades[0], grades[1], grades[2], grades[3], grades[4], tens.format(gpa));
    return(ans);

}

所以我认为问题在于我如何调用.toString,我的Google搜索显示我应该使用java.util.Arrays?我试过了,我无法弄清楚出了什么问题。我知道它有一个简单的语法错误,但我不知道它是什么。

如果名单编号打印在小程序上会发生什么,但所有值都是0.00或0或各自格式的0。

repaint()方法

中也会调用actionperformed

1 个答案:

答案 0 :(得分:1)

setup函数中,获取此行:

for(x = 0; x < 31; x++){
  roster[x] = new Student("", 0, gtest);
}

导致ArrayIndexOutOfBoundsException,因为roster只有10个元素。

然后,将文字绘画外观更改为:

// Ger rid of the outer loop on c
// for (int c = 0; c < 1) {
// Change your initial b value from 250 to 115. 250 is too low for your window
for(int b = 115, c = 0; c < 9; b = b + 10){
    g.drawString(roster[c].toString(), 10, b);
    c++;
}
// }