从另一个类获取主函数的String

时间:2018-04-05 18:28:09

标签: java

我有这个主要功能(Class:DialogBoxForIt.java):

public void main(final SimpleCallback<HashMap<SPARAM, Object>> sr) {
    if(mainwindow == null){
        JFrame jf = new JFrame();
        jf.setTitle(_T.PasswordEncrypt_title.val());
        jf.setResizable(false);
        jf.setIconImages(ResourceCenter.icons);
        jf.getContentPane().setLayout(new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS)); 
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        thiswindow = jf;
    }
    else{
        JDialog jd = new JDialog(mainwindow);
        jd.setModal(true);
        jd.setTitle(_T.PasswordEncrypt_title.val());
        jd.setResizable(false);
        jd.setIconImages(ResourceCenter.icons);
        jd.getContentPane().setLayout(new BoxLayout(jd.getContentPane(), BoxLayout.Y_AXIS)); 
        jd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        thiswindow = jd;
    }

    JPanel line1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JPanel line2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JPanel lineok = new JPanel(new GridBagLayout());
    JPanel line3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    final JPanel lineopt = new JPanel(new FlowLayout(FlowLayout.RIGHT));

    final JPasswordField tf = new JPasswordField();
    tf.setPreferredSize(new Dimension(200, 25));

    final JPasswordField tf2 = new JPasswordField();
    tf2.setPreferredSize(new Dimension(200, 25));

    final JTextField jtf = new JTextField(""+CryptoCodes.STANDARD_PBKDF2_ITERATIONS); 

    JButton b = new JButton("OK");

    ActionListener act = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            char[] s1 = tf.getPassword();
            char[] s2 = tf2.getPassword();

            if(s1 == null || s2 == null || s1.length == 0 || s2.length == 0)
                return;

            int minlen = CryptoCodes.STANDARD_PBKDF2_PWLEN;

            tf.setText(DUMMYTEXT);
            tf2.setText(DUMMYTEXT);

            tf.setText("");
            tf2.setText("");

            if(Arrays.equals(s1, s2) && s1.length >= minlen){
                thiswindow.dispose();
                CryptoUtils.kill(s2);

                char[] passw = PasswordGenerator.GenerateRandomString(18,25,3,1,1,1).toCharArray();
                String passwds = String.valueOf(passw);
                try {
                    passwds = new String(passw);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }



                myParams.put(SPARAM.password, passwds.toCharArray());

                System.out.println("let test -- "+String.valueOf((char[])myParams.get(SPARAM.password))) ;


                if(lineopt.isVisible()){
                    String tmp = jtf.getText();
                    if(FilterNumbers.validate(tmp)) myParams.put(SPARAM.itcount, tmp);
                }
                sr.callbackValue(this, myParams);
            }
            else{
                CryptoUtils.kill(s1);
                CryptoUtils.kill(s2);
                tf.requestFocus();

                if(s1.length < minlen)
                    JOptionPane.showMessageDialog(thiswindow, _T.PasswordEncrypt_len.msg(minlen));
                else
                    JOptionPane.showMessageDialog(thiswindow, _T.PasswordEncrypt_nomatch);
            }
        }
    };

    b.addActionListener(act);
    tf.addActionListener(act);
    tf2.addActionListener(act);
    jtf.addActionListener(act);

    JLabel jl = new JLabel(_T.PasswordEncrypt_label + ": ");
    line1.add(jl);
    line1.add(tf);
    thiswindow.add(line1);

    jl = new JLabel(_T.PasswordEncrypt_retype + ": ");
    line2.add(jl);
    line2.add(tf2);
    thiswindow.add(line2);

    b.setPreferredSize(new Dimension(200, 35));
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = gbc.weighty = 1.0;
    lineok.add(b, gbc);
    thiswindow.add(lineok);

    javax.swing.ToolTipManager.sharedInstance().setDismissDelay(7000);

    ((PlainDocument) jtf.getDocument()).setDocumentFilter(new FilterNumbers());
    jtf.setPreferredSize(new Dimension(200, 25));
    jtf.setToolTipText(_T.PasswordEncrypt_itcountdescr.val());
    jl = new JLabel(_T.PasswordEncrypt_itcount + ": ");
    jl.setToolTipText(_T.PasswordEncrypt_itcountdescr.val());
    lineopt.add(jl);
    lineopt.add(jtf);

    final JButton jb = new JButton(new ImageIcon(ResourceCenter.optsup));
    line3.add(jb);
    thiswindow.add(line3);

    thiswindow.add(lineopt);
    lineopt.setVisible(false);

    jb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(lineopt.isVisible()){
                lineopt.setVisible(false);
                jb.setIcon(new ImageIcon(ResourceCenter.optsup));
            }
            else{
                lineopt.setVisible(true);
                jb.setIcon(new ImageIcon(ResourceCenter.optsdown));
            }

            thiswindow.pack();
        }
    });

    thiswindow.pack();

    thiswindow.setLocationRelativeTo(mainwindow);
    thiswindow.setVisible(true);

    thiswindow.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosed(WindowEvent e) {
            try {
                tf.setText(DUMMYTEXT);
                tf2.setText(DUMMYTEXT);

                tf.setText("");
                tf2.setText("");
            } catch (Exception ex) {}
        }
    });

    if(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)){
        JOptionPane.showMessageDialog(thiswindow, _T.Password_caps);
    }
}

我从另一个类中调用这个main函数(另一个类名:AppTest.java):

new DialogBoxForIt(currentSource).main(this);

我想从这个类中获取main函数的passwds字符串。

请问怎么做?

为了实现这一目标,我必须进行哪些更改?

您必须知道passwds String包含随机生成的密码,并且必须转移到另一个类以供其他用途。

我尝试添加公共字符串(public String passwpub = ""),然后在函数(passwpub = passwds;)中添加,但无法从其他类中获取它。

如果您有任何想法将受到高度赞赏。

谢谢!

0 个答案:

没有答案