如何在公共无效区域内从另一个类调用变量?

时间:2019-11-10 03:13:25

标签: java

我最近加入了Java开发,我首先制作了一个JFrame应用程序,实际上我了解其中的一些内容。 我想添加一个按钮,在该按钮中您可以更改用户和密码,您需要“新用户”,“新密码”,“重复新密码”和“旧密码”。幸运的是,我想出了如何执行“新密码和重复密码”的方法。每当我想执行“是否”检查旧密码是否使用与另一个类中声明的密码相同的密码时,都会遇到此问题,密码变量称为“ passwordField”,它位于公共无效空间内,在该变量中始终返回字面值没事。 要获取passwordField,我需要执行char [] password = passwordField.getPassword();。然后将char转换为String。这是我的大部分代码,(是的,抱歉,大多数语言是西班牙语,因为我的主要语言是西班牙语。我会将代码的一部分附加到希望“ passantiguas”等于“ passwords”的位置

            public void actionPerformed(ActionEvent e) {
                InicioMain contrasena = new InicioMain();
                char[] password = contrasena.passwordField.getPassword();
                String passwords = String.valueOf(password);
                String passnueva = nuevapasstxt.getText();
                String passrepetida = repetirpasstxt.getText();
                String passantiguas = passantigua.getText();
                //Trying to figure out what's the error (passwords returns "" literally nothing)
                System.out.println(passwords + "  //  " + passantiguas);
                //Checking if the newpassword does not equals the repeated password
                if (!passnueva.equals(passrepetida)) {
                    JOptionPane.showMessageDialog(contentPanel, "La Contrase\u00F1a Repetida(Nueva) No Coincide. ");
                    newusertext.setText(null);
                    nuevapasstxt.setText(null);
                    repetirpasstxt.setText(null);
                    passantigua.setText(null);
                //Trying to check if the Old Password does not equals "passwords"
                } else if (!passantiguas.equals(passwords)) {
                    JOptionPane.showMessageDialog(contentPanel, "La Contrase\u00F1a Antigua No Coincide. ");
                    newusertext.setText(null);
                    nuevapasstxt.setText(null);
                    repetirpasstxt.setText(null);
                    passantigua.setText(null);
                } else {
                    //Success when all is confirmed and changed
                    JOptionPane.showMessageDialog(contentPanel, "La Contrase\u00F1a Y el Nuevo Usuario han sido cambiados correctamente! ");
                    dispose();
                    //Need to add more Code here
                }

            }

        });```


    //PasswordField Declared on another class (inside a public void).
    //If you ask, "protected JPasswordField passwordField" is already located on the public class.
    ```passwordField = new JPasswordField();
        passwordField.setBounds(218, 135, 210, 39);
        frame.getContentPane().add(passwordField);```

0 个答案:

没有答案