java将参数传递给动作事件方法

时间:2013-11-24 09:15:18

标签: java swing parameters actionlistener actionevent

我不确定如何将参数/参数传递给动作事件。该程序应该生成随机时间表“闪存卡”,将其与正确答案进行比较并将输出返回到控制台,让用户知道他们的输入是否正确。编辑告诉我,我需要让我的课程抽象化,但是,显然这不是一个解决方案。

公共类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;
}

} }

1 个答案:

答案 0 :(得分:2)

在实施actionPerformed()时,您需要使用ActionListener定义的相同签名覆盖ActionListener

你有

public void actionPerformed(ActionEvent e, int answer) 

哪个签名不正确。应该只是这个

public void actionPerformed(ActionEvent e) 

您应该将int answer声明为timesTableFrame的类成员,以便可以在actionPerformed内使用它。或者通过answer的实例化访问generateArray

旁注:我们的评论中有许多提示来自您的帖子。看一下他们的选择。

修改:选项

制作answer班级成员

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
    }
}

使用answer而不使其成为类成员

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;
        }
    }
}