我正在制作一个包含2个密码的配置文件加密程序。我想要一个带有按钮的GUI,用于加密,解密和退出。
我的问题在于actionPerformed方法。它需要抛出outputStream抛出的异常。这是我尝试编译时得到的错误:
:53: error: actionPerformed(ActionEvent) in ProfileEncryption_2 cannot implement actionPerformed(ActionEvent) in ActionListener
public void actionPerformed (ActionEvent e) throws FileNotFoundException//When a button is clicked
我有多种解决方案,但我不确定如何正确实施它们。我可以抓住异常并用它做点什么,但我不知道怎么做什么。我还可以检查文件是否存在,如果存在,然后输出,但我尝试的那个也不起作用。
public void actionPerformed (ActionEvent e) throws Exception //When a button is clicked
{
if (e.getSource() == encrBtn)
{
menu.setVisible(false);
createProfile();
menu.setVisible(true);
}
else
{
if (e.getSource() == decrBtn)
{
menu.setVisible(false);
viewProfile();
menu.setVisible(true);
}
else
{
if (e.getSource() == exitBtn)
{
JOptionPane.showMessageDialog(null, "Goodbye!");
System.exit(0);
}
}
}
}
//End of menu
//Start of create/view section
public static void createProfile() throws Exception //Create profile
{
String username = JOptionPane.showInputDialog("Enter your username.");
String password, confirmPass, strEncrType;
int intEncrType = 0, unlock = 0;
do
{
password = JOptionPane.showInputDialog("Enter your password.\nIt must be more than 7 characters long.");
confirmPass = JOptionPane.showInputDialog("Confirm password");
if (password.equals(confirmPass) && (password.length() >= 7))
{
JOptionPane.showMessageDialog(null, "Passwords match!");
unlock = 1;
}
else
{
if (!password.equals(confirmPass))
JOptionPane.showMessageDialog(null, "Passwords do not match!");
if (password.length() < 7)
JOptionPane.showMessageDialog(null, "Password is not long enough!");
}
}
while (unlock==0);
do
{
strEncrType = JOptionPane.showInputDialog("Choose which encryption type you would prefer:\n1. Vigenère\n2. Erénegiv mod 4");
if(!strEncrType.equals("1")&&!strEncrType.equals("2"))
JOptionPane.showMessageDialog(null, "Invalid response, try again.");
}
while (!strEncrType.equals("1")&&!strEncrType.equals("2"));
intEncrType = Integer.parseInt(strEncrType);
String name = JOptionPane.showInputDialog("Enter your real name.");
String phone = JOptionPane.showInputDialog("Enter your phone number.");
String email = JOptionPane.showInputDialog("Enter your email.");
String other = JOptionPane.showInputDialog("Enter notes/extra data you want stored.");
String data = password + "-" + username + "-tester-" + name + "-" + phone + "-" + email + "-" + other + "-" + strEncrType;
if (intEncrType ==1)
data = encrypt1(data);
else
data = encrypt2(data);
data = data + strEncrType;
OutputStream output = new FileOutputStream(username + ".txt");
byte buffer[] = data.getBytes();
output.write(buffer);
output.close();
}