A
Amr
hi all,
im following an excercise from the book "Sams teach yourself java in
21 days"
i typed the exercise exactly as in the book but i get an error. below
are the error and the full code of the program.
thank you very much for your help.
error:
Caused by: java.lang.RuntimeException: Uncompilable source code -
DayEleven.SurveryWizard is not abstract and does not override abstract
method actionPerformed(java.awt.event.ActionEvent) in
java.awt.event.ActionListener
im doing this excercise in Netbeans and it gives an option to
'implement all abstract methods' to get rid of this error.
doing that allows the compilation, but can't when go forward with the
second interface.
package DayEleven;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
/**
*
* @author amr
*/
public class SurveryWizard extends JPanel implements ActionListener{
int currentCard=0;
CardLayout cards=new CardLayout();
SurveyPanel[] ask=new SurveyPanel[3];
public SurveryWizard(){
super();
setSize(240, 140);
//set up survery
String question1="What is your gender";
String[] response1={"female","male","not telling"};
ask[0]=new SurveyPanel(question1,response1,2);
String question2="What is your age";
String[] response2={"Under 25","25-34","35-40","over 54"};
ask[1]=new SurveyPanel(question1,response2,1);
String question3="How often do you excercise each week";
String[] responses3={"Never", "1-3 times","More than 3"};
ask[2]=new SurveyPanel(question3,responses3,1);
ask[2].setFinalQuestion(true);
for(int i=0; i<ask.length;i++){
ask.nextButton.addActionListener(this);
ask.finalButton.addActionListener(this);
add(ask,"Card",i);
}
}
public void actionPerformed(ActiveEvent evt){
currentCard++;
if(currentCard>=ask.length){
System.exit(0);
}
cards.show(this,"Card "+currentCard);
}
}
class SurveyPanel extends JPanel{
JLabel question;
JRadioButton[] response;
JButton nextButton=new JButton("Next");
JButton finalButton=new JButton("Finish");
public SurveyPanel(String ques, String[] resp, int def) {
super();
setSize(160, 110);
question =new JLabel(ques);
response=new JRadioButton[resp.length];
JPanel sub1=new JPanel();
ButtonGroup group=new ButtonGroup();
JLabel quesLabel=new JLabel(ques);
sub1.add(quesLabel);
JPanel sub2=new JPanel();
for(int i=0; i<resp.length;i++){
if(def==i){
response=new JRadioButton(resp,true);
}else{
response=new JRadioButton(resp,false);
}
group.add(response);
sub2.add(response);
}
JPanel sub3=new JPanel();
nextButton.setEnabled(true);
sub3.add(nextButton);
finalButton.setEnabled(false);
sub3.add(finalButton);
GridLayout grid=new GridLayout(3, 1);
setLayout(grid);
add(sub1);
add(sub2);
add(sub3);
}
void setFinalQuestion(boolean finalQuestion){
if(finalQuestion){
nextButton.setEnabled(false);
finalButton.setEnabled(true);
}
}
}
package DayEleven;
import javax.swing.JFrame;
/**
*
* @author amr
*/
public class SurveyFrame extends JFrame{
public SurveyFrame(){
super("Survery");
setSize(290, 140);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SurveryWizard wiz=new SurveryWizard();
add(wiz);
setVisible(true);
}
static public void main(String arg[]){
SurveyFrame surv=new SurveyFrame();
}
}
im following an excercise from the book "Sams teach yourself java in
21 days"
i typed the exercise exactly as in the book but i get an error. below
are the error and the full code of the program.
thank you very much for your help.
error:
Caused by: java.lang.RuntimeException: Uncompilable source code -
DayEleven.SurveryWizard is not abstract and does not override abstract
method actionPerformed(java.awt.event.ActionEvent) in
java.awt.event.ActionListener
im doing this excercise in Netbeans and it gives an option to
'implement all abstract methods' to get rid of this error.
doing that allows the compilation, but can't when go forward with the
second interface.
package DayEleven;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
/**
*
* @author amr
*/
public class SurveryWizard extends JPanel implements ActionListener{
int currentCard=0;
CardLayout cards=new CardLayout();
SurveyPanel[] ask=new SurveyPanel[3];
public SurveryWizard(){
super();
setSize(240, 140);
//set up survery
String question1="What is your gender";
String[] response1={"female","male","not telling"};
ask[0]=new SurveyPanel(question1,response1,2);
String question2="What is your age";
String[] response2={"Under 25","25-34","35-40","over 54"};
ask[1]=new SurveyPanel(question1,response2,1);
String question3="How often do you excercise each week";
String[] responses3={"Never", "1-3 times","More than 3"};
ask[2]=new SurveyPanel(question3,responses3,1);
ask[2].setFinalQuestion(true);
for(int i=0; i<ask.length;i++){
ask.nextButton.addActionListener(this);
ask.finalButton.addActionListener(this);
add(ask,"Card",i);
}
}
public void actionPerformed(ActiveEvent evt){
currentCard++;
if(currentCard>=ask.length){
System.exit(0);
}
cards.show(this,"Card "+currentCard);
}
}
class SurveyPanel extends JPanel{
JLabel question;
JRadioButton[] response;
JButton nextButton=new JButton("Next");
JButton finalButton=new JButton("Finish");
public SurveyPanel(String ques, String[] resp, int def) {
super();
setSize(160, 110);
question =new JLabel(ques);
response=new JRadioButton[resp.length];
JPanel sub1=new JPanel();
ButtonGroup group=new ButtonGroup();
JLabel quesLabel=new JLabel(ques);
sub1.add(quesLabel);
JPanel sub2=new JPanel();
for(int i=0; i<resp.length;i++){
if(def==i){
response=new JRadioButton(resp,true);
}else{
response=new JRadioButton(resp,false);
}
group.add(response);
sub2.add(response);
}
JPanel sub3=new JPanel();
nextButton.setEnabled(true);
sub3.add(nextButton);
finalButton.setEnabled(false);
sub3.add(finalButton);
GridLayout grid=new GridLayout(3, 1);
setLayout(grid);
add(sub1);
add(sub2);
add(sub3);
}
void setFinalQuestion(boolean finalQuestion){
if(finalQuestion){
nextButton.setEnabled(false);
finalButton.setEnabled(true);
}
}
}
package DayEleven;
import javax.swing.JFrame;
/**
*
* @author amr
*/
public class SurveyFrame extends JFrame{
public SurveyFrame(){
super("Survery");
setSize(290, 140);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SurveryWizard wiz=new SurveryWizard();
add(wiz);
setVisible(true);
}
static public void main(String arg[]){
SurveyFrame surv=new SurveyFrame();
}
}