2012-02-15 83 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