1

I'm creating an encryption method that takes input from a JTextArea, and I'm getting an error saying:

'Illegal modifier for parameter input; only final is permitted'

I have gone through many documentation websites and other articles, and I have found nothing. Here is my near-complete code:

Package lake. RAMBIT7;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

public class RAMBIT7 implements ActionListener {

    private JFrame frame;

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

    /**
     * Create the application.
     */
    public RAMBIT7() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setSize(800, 600); //1024x768, 800x600
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("RAMBIT7 Encryption Software 1.0.0");
        frame.setResizable(false);

        /**
         * 'Encrypt' and 'Decrypt' buttons
         */

        JButton encrypt = new JButton("Encrypt");
        encrypt.addActionListener(this);
        JButton decrypt = new JButton("Decrypt");
        decrypt.addActionListener(this);

        /**
         * JMenuBar
         */

        JMenuBar bar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenu help = new JMenu("Help");
        JMenuItem about = new JMenuItem("About");
        JMenuItem license = new JMenuItem("License");
        JMenuItem close = new JMenuItem("Exit");
        file.add(close);
        help.add(about);
        help.add(license);
        bar.add(file);
        bar.add(help);
        about.addActionListener(this);
        license.addActionListener(this);
        close.addActionListener(this);
        frame.setJMenuBar(bar);

        /**
         * Text and input related stuff
         */
        frame.getContentPane().setLayout(new MigLayout("", "[69px][71px,grow][]", "[23px][35.00][200px][][grow][]"));
        frame.getContentPane().add(encrypt, "cell 0 0,alignx left,aligny top");
        frame.getContentPane().add(decrypt, "cell 2 0,alignx right,aligny top");
        JLabel lblCopyTextIn = new JLabel("Copy Text in here.");//JLabel
        frame.getContentPane().add(lblCopyTextIn, "cell 1 1");
        JScrollPane scrollPane = new JScrollPane();
        frame.getContentPane().add(scrollPane, "cell 0 2 3 1,grow");
        JTextArea textArea = new JTextArea();//JTextArea
        scrollPane.setViewportView(textArea);
        textArea.setLineWrap(true);
        JLabel lblOutputTextIn = new JLabel("Output text in RAMBIT7 encryption");//JLabel
        frame.getContentPane().add(lblOutputTextIn, "cell 1 3");
        JScrollPane scrollPane_1 = new JScrollPane();
        frame.getContentPane().add(scrollPane_1, "cell 0 4 3 1,grow");
        JTextArea textArea_1 = new JTextArea();//JTextArea_1
        scrollPane_1.setViewportView(textArea_1);
        textArea_1.setEditable(false);
        textArea_1.setLineWrap(true);
        JLabel lblRambitEncryptionMethod = new JLabel("RAMBIT7 Encryption Method"); //JLabel
        frame.getContentPane().add(lblRambitEncryptionMethod, "cell 1 5");

        public String input_0 = textArea.getText();//Error here


    }



    @Override
    public void actionPerformed(ActionEvent e) {
        String a = e.getActionCommand();
        if(a.equalsIgnoreCase("encrypt")) {
            System.out.println("Begin RAMBIT7 encryption.");
            encryptRAMBIT7(input);
        } else if(a.equalsIgnoreCase("decrypt")) {
            System.out.println("Begin RAMBIT7 decryption.");
            decryptRAMBIT7(input);
        } else if(a.equalsIgnoreCase("about")) {
            System.out.println("Opening Program Specs...");
            JOptionPane.showMessageDialog(frame, "RAMBIT7 v1.0.0");
            System.out.println("Program Specs Closed.");
        } else if(a.equalsIgnoreCase("license")) {
            System.out.println("Opening License...");
            JOptionPane.showMessageDialog(frame, "You may not sell this program or say that any part of the code is yours.");
            System.out.println("License closed.");
        } else if(a.equalsIgnoreCase("exit")) {
            System.out.println("Why, oh WHY CRUEL WORLD does that person have to close me?! I'm\na living thing too! Or maybe I'm an emotionless pig! NOOOOOOOO!");
            System.exit(3);
        }

    }

}
4

3 回答 3

3

您需要重新考虑一下您的结构:

  • 即使您可以在创建时将字符串从 JTextArea 中取出,它也对您一点帮助也没有。当 JTextArea 更改其显示文本时,该字符串不会更改,因为字符串是不变量。
  • 同样,字符串字段也无济于事,除非它通过 DocumentListener 或类似的东西动态绑定到 JTextArea。
  • 而是将 JTextArea 设为私有字段并在需要时提取其字符串。
  • 如果其他类需要该文本,则创建一个公共方法public String getTextAreaText()并在该方法中返回 JTextArea 保存的文本。

public class RAMBIT7 implements ActionListener {

    private JFrame frame;
    private JTextArea textarea = new JTextArea();

    // ....

   private void initialize() {

        // .....

        // JTextArea textArea = new JTextArea();//JTextArea
        scrollPane.setViewportView(textArea);
        textArea.setLineWrap(true);

和其他地方:

    if(a.equalsIgnoreCase("encrypt")) {
        System.out.println("Begin RAMBIT7 encryption.");
        encryptRAMBIT7(textarea.getText());
于 2013-11-09T16:33:15.617 回答
1

您不能public在方法中使用关键字。可见性修饰符用于确定每个对象的哪些成员可以被其他对象或类从外部访问。您在方法中声明的东西不是类的成员,而是只存在于该方法中的东西,因此可见性修饰符在那里没有意义。

于 2013-11-09T16:33:25.820 回答
0
public String input_0 = textArea.getText();

在函数内声明的任何变量都是该函数的本地变量。外界不知道该变量,但不知道函数本身。public因此,访问修饰符privateprotected不适用于局部实例变量,例如input_0这里是局部的initialize()。同样,这段代码在initialize()函数内部,它负责初始化你的 GUI 组件,创建它们并将它们添加到容器中,用户输入尚未发生,因此读取文本内容的textArea意义不大。

于 2013-11-09T16:38:15.133 回答