如何从JTextArea创建公共字符串?

时间:2013-11-09 16:26:44

标签: java swing encryption compiler-errors

我正在创建一个从JTextArea获取输入的加密方法,我收到一条错误消息:

'参数输入的非法修饰符;只允许最终'

我浏览了很多文档网站和其他文章,但我什么也没找到。这是我近乎完整的代码:

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

    }

}

3 个答案:

答案 0 :(得分:3)

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

  • 即使您在创建时可以从JTextArea中获取String,也不会对您有所帮助。当JTextArea更改其显示文本时,该字符串不会更改,因为字符串是不变量。
  • 同样,String字段也无济于事,除非它通过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());

答案 1 :(得分:1)

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

答案 2 :(得分:0)

public String input_0 = textArea.getText();

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