Hey, so I need to remove my previously added OptionButtons and replace them with my new ones. The new ones do paint themselves, but they show up behind the first set of buttons, so I need to remove them or hide them somehow. Is there just a simple remove method for buttons that I can use? Here's my code:
public class Question1 extends Screen {
private String q = "Name the BAND";
private String b = "";
private AudioClip seize;
private int a=1;
public Question1(final AppPanel panel) {
super(panel);
}
{
OptionButton[] buttons = {
new OptionButton(50,200,200,50, "Aerosmith"),
new OptionButton(450, 200,200,50, "A7X"),
new OptionButton(50,300,200,50, "Guns n' Roses"),
new OptionButton(450,300,200,50, "AC/DC")
};
for (int i = 0; i<buttons.length;i++){
panel.add(buttons[i]);
if(i==a){
buttons[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
correctAnswer(event);
panel.repaint();
q = "Name the SONG";
//This is where I want to delete the previous buttons, and replace them with the buttons I have directly below this.
//These buttons do show up, but behind the first added buttons.
OptionButton[] buttons = {
new OptionButton(50,200,200,50, "Civil War"),
new OptionButton(450, 200,200,50, "Walk This Way"),
new OptionButton(50,300,200,50, "Afterlife"),
new OptionButton(450,300,200,50, "Back in Black")
};
for(int e=0; e<buttons.length; e++){
panel.add(buttons[e]);
if(e==a){
buttons[e].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
correctAnswer(event);
panel.nextScreen();
}
});
}
else {buttons[e].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
wrongAnswer(event);
}
});
};
}
}
});}
else {buttons[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
wrongAnswer(event);
}
});
}
};
}
public void correctAnswer(ActionEvent e){
q = "Correct!";
b = "";
}
public void wrongAnswer(ActionEvent e){
main.lives--;
if(main.lives<=0){
panel.screen=0;
main.lives=3;
}
q = "Wrong";
b = ":-(";
}
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, control.AppMain.WIDTH, control.AppMain.HEIGHT);
Font font = new Font("Arial", Font.PLAIN, 50);
g.setColor(Color.BLACK);
g.setFont(font);
FontMetrics fm=g.getFontMetrics(font);
g.drawString(q, ((control.AppMain.WIDTH/2)-(fm.stringWidth(q)/2)), 50);
g.drawString(b, ((control.AppMain.WIDTH/2)-(fm.stringWidth(q)/5)), 100);
}
}