Hey, just trying to get these buttons to work by calling the methods "correctAnswer" and "wrongAnswer" at the bottom into earlier code. I had it working earlier but it was "styled badly" so I'm trying to clean it up. This is the code I have so far.
public class Level1 extends Screen {
private String q = "Name the BAND";
private String b = "";
private AudioClip seize;
private 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")
};
private int a=1;
public Level1(final AppPanel panel) {
super(panel);
for (int i =0; i<buttons.length;i++){
panel.add(buttons[i]);
if(i==a){
buttons[i].addActionListener(new ActionListener(){
//this is where I want "correctAnswer"
});
}
else {buttons[i].addActionListener(new ActionListener(){
//this is where I want "wrongAnswer"
});
}
}
}
public void correctAnswer(ActionEvent e){
q = "Correct!";
}
public void wrongAnswer(ActionEvent e){
main.lives--;
if(main.lives<=0){
panel.lastScreen();
main.lives=3;
}
q = "Wrong";
b = ":-(";
}
Earlier I was calling those methods inside the for loop and it worked, but was told to clean that up, so any suggestions, however simple they may be, are very welcome!
Thank you.
public class Level1 extends Screen {
private String q = "Name the BAND";
private String b = "";
private AudioClip seize;
private 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")
};
private int a=1;
public Level1(final AppPanel panel) {
super(panel);
for (int i =0; i<buttons.length;i++){
panel.add(buttons[i]);
if(i==a){
buttons[i].addActionListener(new ActionListener(){
//this is where I want "correctAnswer"
});
}
else {buttons[i].addActionListener(new ActionListener(){
//this is where I want "wrongAnswer"
});
}
}
}
public void correctAnswer(ActionEvent e){
q = "Correct!";
}
public void wrongAnswer(ActionEvent e){
main.lives--;
if(main.lives<=0){
panel.lastScreen();
main.lives=3;
}
q = "Wrong";
b = ":-(";
}
Earlier I was calling those methods inside the for loop and it worked, but was told to clean that up, so any suggestions, however simple they may be, are very welcome!
Thank you.