gpt4 book ai didi

java - 如何从 JTextArea 创建公共(public)字符串?

转载 作者:行者123 更新时间:2023-11-29 03:28:12 24 4
gpt4 key购买 nike

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

'参数输入非法修饰符;只允许使用 final'

我浏览了许多文档网站和其他文章,但一无所获。这是我接近完成的代码:

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

}

}

最佳答案

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

  • 即使您可以在创建时从 JTextArea 中取出 String,也无济于事。当 JTextArea 更改其显示文本时,该字符串不会更改,因为字符串是不变量。
  • 同样,字符串字段也无济于事,除非它通过 DocumentListener 或类似的东西动态绑定(bind)到 JTextArea。
  • 改为将 JTextArea 设为私有(private)字段并在需要时提取其字符串。
  • 如果其他类需要该文本,则创建一个公共(public)方法 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());

关于java - 如何从 JTextArea 创建公共(public)字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19878248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com