This site requires JavaScript, please enable it in your browser!
Greenfoot back
Alex12
Alex12 wrote ...

2017/4/5

System.exit(0);

Alex12 Alex12

2017/4/5

#
Hey lads, got a problem here.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class quitbutton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class quitbutton extends Actor
{
    /**
     * Act - do whatever the quitbutton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.mouseClicked(this)){ 
            System.exit(10);
        }
    }    
}
Greenfoot shuts down if I execute this code but restarts after a couple of seconds. Does anyone know the reason for that? -Alex12
Nosson1459 Nosson1459

2017/4/5

#
That happens to me also when I use it for a JFrame project. It probably happens because that's not the way Greenfoot is really meant to be closed, it's just a force type of a thing, not done by your computer's Operating System.
Alex12 Alex12

2017/4/5

#
Thank you for the reply. Seems like I just have to keep it like that.
Nosson1459 Nosson1459

2017/4/5

#
Alex12 wrote...
Thank you for the reply. Seems like I just have to keep it like that.
Keep it like what, restarting, or not using it at all? There isn't any point in an exit button if Greenfoot reopens right away. But I don't think Greenfoot was made in a way that requires any in-programming closing.
danpost danpost

2017/4/5

#
Alex12 wrote...
< Code Omitted > Greenfoot shuts down if I execute this code but restarts after a couple of seconds. Does anyone know the reason for that? -Alex12
You can use:
Greenfoot.stop();
to end the simulation. However, I believe you are trying to close the program totally by using the 'System.exit' command. I have not tested it, but, I think that this particular command will work as you had anticipated if the project was converted into a stand-alone application (an executable jar file), as the code is part of your project and not part of the greenfoot application that is the IDE of your project.
You need to login to post a reply.