2012-02-15 84 views
0

尝试调用方法getuserinput时出现错误。这是我破碎的代码中的一小部分。actionlistener无法正常工作

initialvelocitybutton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
     getuserinput(); //I am getting an error saying "The method getuserinput() is undefined for the type new ActionListener(){}" 
      } 
     }); 

static void getuserinput(){ //method to get users input 
       double initialvelocity = Double.parseDouble(
          JOptionPane.showInputDialog("please enter initial velocity")); //gets initial value of intiial velcoity 
       double angleoflaunch = Double.parseDouble(
          JOptionPane.showInputDialog("please enter angle of launch")); 
} 

回答

3

getuserInput()被宣布为static。你必须参考它使用类名称:NameOfYourClass.getuserInput();

+0

对不起,但我不知道我的班级名称是什么。我尝试了Main.getusername();我试着actionPerformed.getuserinput(); ...请你解释一下,请 – user1183685 2012-02-15 22:56:55

+0

@ user1183685这可能是[复习一些基础知识]的时候了(http://docs.oracle.com /javase/tutorial/java/concepts/class.html)。 – Jeffrey 2012-02-15 23:02:52

+0

这就是'class'关键字和源代码中的'{'符号在你粘贴的方法之上的某个地方之间的词。它可能与您的源代码文件的名称相同。关于在Java教程中声明类的更多信息:http://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html – 2012-02-15 23:05:26