I’m switching to Java from Python and I’m having a little difficulty with my project. I’m writing a GUI in Swing for some ciphers that I teach. I have two packages in my project – cryptolib and cryptogui. Cryptolib contains all the different ciphers as classes and cryptogui is my GUI.
All of my ciphers are sublcasses of a Cipher class that I defined. Currently, I’m having difficulty using the following class.
package cryptolib;
public class SubstitutionCipher extends Cipher{
... implementation here ...
}
In my GUI class, I define a menu item to switch to the Substitution cipher using an anonymous class.
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());
}
});
}
The problem I’m running into is that while “private Cipher cipher;” works, the SubstitutionCipher code in the ActionListener gives me the error
The method SubstitutionCipher(String) is undefined for the type new ActionListener(){}
The classes I imported from Swing (java.awt.CardLayout, for example) work perfectly. I know it’s probably something fundamental that I’ve missed, but I’ve searched and can’t seem to find the issue.
should probably be
Notice the
newkeyword.