在封闭范围内定义的局部变量textfeild_1必须是最终的还是有效的最终?

时间:2018-03-26 07:50:20

标签: java swing

  

线程中的异常" AWT-EventQueue-0" java.lang.Error:未解决   编译问题:
  局部变量textField_1定义在   封闭范围必须是最终的或有效的最终的   在   swing.a1 $ 4.actionPerformed(a1.java:179)

我收到此错误,我无法将变量设置为final,因为它会破坏gui代码textField_1 = new JTextField();该怎么做?

public class a1 extends JFrame  {

     JPanel contentPane;
      JTextField textField_1;
      JTextField textField_2;
      JTextField key_text;



    public static void encDec(String key,int cipherMode,File in,File out ) 
             throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException,
              NoSuchPaddingException, IOException, NullPointerException
             {
            FileInputStream fis= new FileInputStream(in);
             FileOutputStream fos = new FileOutputStream(out);

             DESKeySpec dks = new DESKeySpec(key.getBytes());

             SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
             SecretKey sk = skf.generateSecret(dks);
             Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

             if(cipherMode==Cipher.ENCRYPT_MODE) {
                  cipher.init(Cipher.ENCRYPT_MODE, sk,SecureRandom.getInstance("SHA1PRNG"));
                   CipherInputStream cis= new CipherInputStream(fis,cipher);
                 write(cis,fos);
             }else if(cipherMode==Cipher.DECRYPT_MODE) {
                 cipher.init(Cipher.DECRYPT_MODE, sk,SecureRandom.getInstance("SHA1PRNG"));
               CipherOutputStream cos= new CipherOutputStream(fos,cipher);
             write(fis,cos);
             }
             }
             private static void write(InputStream in, OutputStream out) throws IOException {
                 byte[] buffer= new byte[64];
                 int nobr;
                 while((nobr=in.read(buffer)) != -1) {
                     out.write(buffer,0,nobr);
                 }
                 out.close();
                 in.close();

                 }








    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    a1 frame = new a1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public a1() {

        JTextField textField_1= new JTextField();
        JTextField textField_2= new JTextField();


        File in = new File(textField_1.getText());  
          File out = new File(textField_2.getText());




        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 452, 224);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);



        JButton btnEncrypt = new JButton("Encrypt");
        btnEncrypt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try { 
                    encDec("12345678",Cipher.ENCRYPT_MODE,in,out);
                    System.out.println("27la mesa");
                } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
                        | IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();}
                }
            });
        btnEncrypt.setBounds(107, 151, 89, 23);
        contentPane.add(btnEncrypt);

        JButton btnDecrypt = new JButton("Decrypt");
        btnDecrypt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try { 
                    encDec("12345678",Cipher.DECRYPT_MODE,in,out);
                    System.out.println("27la mesa");
                } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
                        | IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                }

        });
        btnDecrypt.setBounds(229, 151, 89, 23);
        contentPane.add(btnDecrypt);

        textField_1 = new JTextField();
        textField_1.setBounds(79, 52, 216, 20);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(79, 84, 216, 20);
        contentPane.add(textField_2);

        key_text = new JTextField();
        key_text.setColumns(10);
        key_text.setBounds(79, 21, 216, 20);
        contentPane.add(key_text);
            new JFileChooser();

        JButton button = new JButton("select");
        final JFileChooser fc1 = new JFileChooser();
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                fc1.setCurrentDirectory(new java.io.File("user.home"));
                fc1.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                if (fc1.showOpenDialog(button) == JFileChooser.APPROVE_OPTION) {
                    textField_1.setText(fc1.getSelectedFile().toString());





                }
            }
        });


        button.setBounds(309, 52, 89, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("select");
        final JFileChooser fc2 = new JFileChooser();
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                fc2.setCurrentDirectory(new java.io.File("user.home"));
                fc2.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                if (fc1.showOpenDialog(button) == JFileChooser.APPROVE_OPTION) {
                    textField_2.setText(fc1.getSelectedFile().toString());






                }
            }
        });



        button_1.setBounds(309, 83, 89, 23);
        contentPane.add(button_1);

        JLabel lblKey = new JLabel("key:");
        lblKey.setBounds(23, 24, 46, 14);
        contentPane.add(lblKey);

        JLabel lblIn = new JLabel("in:");
        lblIn.setBounds(23, 55, 46, 14);
        contentPane.add(lblIn);

        JLabel lblOut = new JLabel("out:");
        lblOut.setBounds(23, 87, 46, 14);
        contentPane.add(lblOut);




}
}

1 个答案:

答案 0 :(得分:0)

在这些方面:

JTextField textField_1= new JTextField();
JTextField textField_2= new JTextField();

您正在创建新的本地对象,这些对象与您之前声明的对象不同。您应该将这些更改为:

textField_1= new JTextField();
textField_2= new JTextField();