This is my main class:

public class Table extends java.applet.Applet implements Runnable
{
    public void init()
    {
        Balla.addBall();
    }
}

This is the Balla method:

public static void addBall()throws IOException
    {
        Random generator = new Random();
        Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1);
        FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true);
        PrintWriter pw = new PrintWriter(fw,true);
        pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY);
        fw.close();
        pw.close();
    }

Now my question come from when I complie it. It tells me that I have an unreported IOException in the init method, but when I add the throws IOException on the method it tells me:
error: init() in Table cannot override init() in Applet

How can I get around this and/or how can I fix this without modifying to much of everything?

有帮助吗?

解决方案

change this

public static void addBall()throws IOException
    {
        Random generator = new Random();
        Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1);
        FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true);
        PrintWriter pw = new PrintWriter(fw,true);
        pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY);
        fw.close();
        pw.close();
    }

to

public static void addBall()
    {
        Random generator = new Random();
        Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1);
        try
{
        FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true);
        PrintWriter pw = new PrintWriter(fw,true);
        pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY);
        fw.close();
        pw.close();
    }
catch(Exception e)
{
}
}

and see if it works

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top