Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取要在新方法Java中显示的变量_Java_Swing_Encryption - Fatal编程技术网

获取要在新方法Java中显示的变量

获取要在新方法Java中显示的变量,java,swing,encryption,Java,Swing,Encryption,我试图从JFrame的textarea中的用户那里获取输入,然后在另一个类中使用它,但它总是返回null。如果我硬编码变量encryptString,它将显示,但在我将其保存到Encrypt Action listener时不会显示 JFrame加密文件 public class Encrypt extends JFrame { public String encryptString ; /** * Creates new form NewJFrame1 */ public Encrypt(

我试图从JFrame的textarea中的用户那里获取输入,然后在另一个类中使用它,但它总是返回null。如果我硬编码变量encryptString,它将显示,但在我将其保存到Encrypt Action listener时不会显示

JFrame加密文件

public class Encrypt extends JFrame {
public String encryptString ;
/**
 * Creates new form NewJFrame1
 */
public Encrypt() {
    initComponents();
}


    /**
     * Run on encryption button press
     */
private void encryptionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    //windowClosed(); 
    run();
encryptString = textForEncryption.getText();

textForEncryption.setText(encryptString +"hey");


    Display display = new Display();
    display.setVisible(true); 

     //close();
}                                                
我正在尝试访问的AES文件

public class AES {

public static void run(){
    Encrypt e = new Encrypt();
    String strDataToEncrypt =  e.encryptString;
            String strCipherText;
    String strDecryptedText;
            System.out.println(strDataToEncrypt);
            System.out.println(e.encryptString);

    }

您的代码使用AES方法向后查看,创建一个加密GUI,并在用户有机会使用它之前立即从GUI中提取一个公共(可能是空的)字符串。了解AES创建的GUI可能与显示给用户的GUI不同

不,我认为GUI,或者更好的是它的控件(它的ActionListener)应该有一个非GUI加密对象,从ActionListener的actionPerformed方法中获取相关字符串,然后让encryption类完成它的工作

类似这样,在半伪代码中:

GUI类

public class Gui {

  public Gui() {
    myButton.addActionListener(new ButtonListener(this));
  }
}
控制类

public class ButtonListener implements ActionListener {
  private Gui gui;

  public ButtonListener(Gui gui)
    this.gui = gui;
  }

  public void actionPerformed(ActionEvent evt) {
    String preEncryptString = gui.getPreEncryptString(); // get from text component
    NonGuiEncryption encryption = new NonGuiEncryption(preEncryptString);

    // the code below might need to be done in a background thread 
    // depending on how long it takes to run
    String encryptString = encryption.doEncryption();

    // the code below must be run on the EDT, but after the code above completes
    gui.putEncriptString(encryyptString);
  }  
}

请看一看,以更快地获得更好的答案!这里有这么多不相关的代码..谢谢你的帮助pointer@m4773rz:如果你能问一些具体的问题,也许我能帮上忙。那么你是说我目前的Jframe(encrypt)和AES类无法做到这一点?我必须改变它们?@m4773rz:是的,我会改变它们。但要进行更详细的分析,请创建并发布您的代码。@m4773rz:您需要问自己的关键问题是:您是否理解当前代码不起作用的原因?除非你这样做,否则你不能使用它。