I am making a vertical scroller game, and first I have the launcher.
There is a button for "load game" and "new game." those are the only two items on the screen until one is clicked. I would like to remove them on click, but when I put
remove(lGame);remove(nGame); in the
actionPerformed method of the action listener, it freezes the program on click. What do I do to fix this?
Code:
public class Launcher extends JFrame{
private JButton nGame;
private JButton lGame;
private String dir = "";
public Launcher(){
super("Space Invaders");
setLayout(new FlowLayout(FlowLayout.CENTER));
if(isWindows()){
System.out.println("Windows");
dir = "%APPDATA%\\.SInvaders\\save.sav";
}
if(isUnix() || isMac()){
System.out.println("Linux/Unix/Mac");
dir = "~/.SInvaders/save.sav";
}
nGame = new JButton("New Game");
lGame = new JButton("Load Save");
File sav = new File(dir);
add(nGame);
add(lGame);
nGame.addActionListener(new HC(false));
lGame.addActionListener(new HC(true));
if(!sav.exists()){
lGame.setEnabled(false);
}
}
//methods for OS detection
private class HC implements ActionListener{
boolean load;
public HC(boolean load){
this.load = load;
}
@Override
public void actionPerformed(ActionEvent arg0) {
remove(nGame);
remove(lGame);
}
}
}