-1

I'm not sure how to pass parameters/arguments into action events. This program is supposed to generate a random times table "flash card", compare it to the correct answer and return output to the console letting the user know if their input was correct. The editor is telling me that i need to make my class abstract, but, obviously that is not a solution.

public class MultiplicationGui {

public static void main(String[] args) 
{
    //new JFrame
    JFrame myJFrame = new JFrame();

    //Attributes
    myJFrame.setTitle("Multiplication Gui");
    myJFrame.setSize(240, 200);
    myJFrame.setLocation(200, 100);
    myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // make the frame visible 
    myJFrame.setVisible(true); 
}

 }//end class

class timesTableFrame extends JFrame implements ActionListener
{
JLabel jlblNote = new JLabel("This GUI gets data from a text field");
JLabel prompt = new JLabel("Please enter your answer");
JTextField jtfAnswer = new JTextField(20);

// constructor 
public timesTableFrame() 
{
    setLayout(new FlowLayout(FlowLayout.CENTER));

    add(jlblNote);
    add(prompt);
    add(jtfAnswer);

    // register TextFieldFrame (this method) as the listener for jtfName
    jtfAnswer.addActionListener(this);

} // end TextFieldFrame() constructor

public void actionPerformed(ActionEvent e, int answer) 
{
    // capture the name from the text field and reset the field
    double response=Double.parseDouble(jtfAnswer.getText());
    jtfAnswer.setText("");

    // output a message using the name to the console
    if (response == answer)
        System.out.println("Congratulations. You are correct!");
    else if (response != answer)
        System.out.println("Sorry, the correct answer is "+answer);

    // dispose of the frame (this frame) 
    this.dispose();


} // end actionPerformed(ActionEvent e)
  //********************************************************************

 class generateArray
 {

public int generateArray()

{
    int i, j;//variables to iterate over array
    int answer;//hold answer from indices into array
    int MDArray[][]=new int [13][13];//new md array for table


    //use nested for to populate array
    for(i=0; i<13; i++)
        for(j=0; j<13; j++)
    MDArray[i][j]=i*j;

    //generate two random numbers from 0-13 to be used as indices into array
    int index1=(int)(Math.random()*13);
    int index2=(int)(Math.random()*13);

    //populate answer variable and return
    answer = MDArray[index1][index2];
    return answer;
}

} }

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Michael Queue
  • 1,340
  • 3
  • 23
  • 38
  • You might want to make this `JFrame myJFrame = new JFrame();` this instead `JFrame myJFrame = new timesTableFrame();`. The former will present an empty frame. – Paul Samsotha Nov 24 '13 at 09:18
  • Where is `int answer` coming from? You may just be able to have it as a class member and use it inside the `actionPerformed` instead of taking it as a param. – Paul Samsotha Nov 24 '13 at 09:20
  • @peeskillet int answer is from class generateArray, method generateArray. Trying to keep design modular, but, i am not sure if this is the correct approach. – Michael Queue Nov 24 '13 at 09:26
  • Why don't you make `int answer` a member of the `timesTableFrame` class instead. In your current code, you would need to to do `generateArray.answer` to access it anywhere outside the class. `generateArray` is an inner class of `timesTableFrame` right? – Paul Samsotha Nov 24 '13 at 09:29
  • @peeskillet was supposed to be method of generateArray class. I took class out to make it a method of timesTableFrame, but, that doesn't solve the problem. – Michael Queue Nov 24 '13 at 09:39
  • What exactly IS the problem.. the editor is telling you you need to make a class abstract? If so, which class is it talking about? You never really stated an actual question to be answered. – Paul Samsotha Nov 24 '13 at 09:45
  • @peeskillet it is telling me that class timesTableFrame needs to be abstract. – Michael Queue Nov 24 '13 at 09:47
  • Is that the only question you have? – Paul Samsotha Nov 24 '13 at 09:47
  • @peeskillet that is the only error i am receiving in my editor. I changed the JFrame declaration as per your suggestion. I found an example that seems to be similar to what i am trying to accomplish, but, i don't really understand the code. http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/learn/CelsiusConverterProject/src/learn/CelsiusConverterGUI.java – Michael Queue Nov 24 '13 at 09:51
  • I downvoted this question because there is no explanation of how the question title relates to the question body. – Robin Green Nov 24 '13 at 09:52
  • See my answer. That will solve your problem. – Paul Samsotha Nov 24 '13 at 09:55
  • @RobinGreen I apologize. I am just unsure how to successfully pass a parameter into my actionEvent method from my generateArray method that will compare the value of user input to the correct number indexed in the array. I will try to be more explicit. – Michael Queue Nov 24 '13 at 09:59
  • You don't pass a parameter. It violates the the method signature you are trying to override – Paul Samsotha Nov 24 '13 at 10:00

1 Answers1

2

You need to overrid the actionPerformed() with the same signature as defined by ActionListener when implementing ActionListener

You have

public void actionPerformed(ActionEvent e, int answer) 

Which is an incorrect signature. Should just be this

public void actionPerformed(ActionEvent e) 

You should declare the int answer as a class member of timesTableFrame so it can be used inside the actionPerformed. Or access answer through an instantiation of generateArray.

Side note: There are many hints in our comment convo from your post. Take a look at them for option.

Edit: options

Making answer class member

class timesTableFrame ... {
    int answer;  // now answer is a class member of timesTableeFrame

    public void actionPerformed(ActionEvent e){
        // answer can be accessed from inside this method
    }

    class generateArray{
        // answer can be accessed from inside the inner class
    }
}

Using answer without making it a class member

class timesTableFrame ... {
    generateArray generatedArray = new generatedArray();

    public void actionPerformed(ActionEvent e){
        int answer = generatedArray.getAnswer();
    }

    class generateArray{
        private int answer;

        public int getAnswer(){
             return answer;
        }
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • thanks for your help. I did try to edit my code as u suggested, however, i am not sure exactly what you mean by making int answer a class member as it is part of my generate array method. I will try to figure it out. just saw your edit. thx again. – Michael Queue Nov 24 '13 at 10:03