2012-07-25 58 views
0

我正在從Python切換到Java,並且對我的項目有點困難。我正在Swing爲我教授的一些密碼寫GUI。我的項目中有兩個軟件包 - cryptolib和cryptogui。 Cryptolib包含所有不同的密碼作爲類,cryptogui是我的GUI。不能在匿名類中使用導入的類

我所有的密碼都是我定義的Cipher類的sublcasses。目前,我在使用以下課程時遇到困難。

package cryptolib; 
public class SubstitutionCipher extends Cipher{ 
... implementation here ... 
} 

在我的GUI類中,我定義了一個菜單項以使用匿名類切換到替換密碼。

package cryptogui; 
import cryptolib.*; 
public class CryptoSwing extends JFrame { 
    private Cipher cipher; 
    public CryptoSwing() { 
     JMenuItem mntmSubstitution = new JMenuItem("Substitution"); 
     mntmSubstitution.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       cipher = SubstitutionCipher(txtSubKeyword.getText()); 
      } 
     }); 
} 

我遇到的問題是,雖然「私人密碼密碼;」工作,在的ActionListener的SubstitutionCipher代碼給我的錯誤

The method SubstitutionCipher(String) is undefined for the type new ActionListener(){} 

我從鞦韆(java.awt.CardLayout中,例如)進口的類很好地工作。我知道這可能是我錯過的根本原因,但我已經搜索,似乎無法找到問題。

回答

1
cipher = SubstitutionCipher(txtSubKeyword.getText()); 

大概應該是

cipher = new SubstitutionCipher(txtSubKeyword.getText()); 

通知的new關鍵字。

+0

我不敢相信我搞砸了。我得到了一切正確的東西,那就是我錯過了什麼?! – MathFreak 2012-07-25 17:32:38