cannot implement actionPerformed(ActionEvent) in A

2019-08-15 18:57发布

问题:

I am making a profile encryption program with 2 ciphers. I want to have a GUI with buttons for enciphering, deciphering and exiting.

My problem is with the actionPerformed method. It needs to throw the exception that the outputStream throws. This is the error I get when I try to complie it:

: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

I have though of multiple solutions, but am not sure how to implement them properly. I could catch the exception and do something with it, but I am not sure how and what. I could also check if the file exists, and if so, then output, but what I tried for that didn't work either.

   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();
   }