尝试使用Java类将数据从一个jFrame传输到另一个jFrame。

huangapple go评论103阅读模式
英文:

Trying to transfer data from one jFrame to another jFrame with the help of a Java Class

问题

我正在尝试将在一个jFrame中获取的电子邮件和密码数据传输到另一个jFrame,使用一个单独的Java类。所有文件都在同一个包中。非常感谢任何帮助,这是我的第一个问题,所以不知道应该提到什么具体细节,如果需要,请告诉我。

LoginForm.java(第一个jFrame)

数据操作部分 在这里,String mail, pass; 包含了电子邮件和密码数据。

if (op == true) {
    JOptionPane.showMessageDialog(null, "登录成功!", "WebApp数据库登录",
            INFORMATION_MESSAGE);
    DataTransfer dt = new DataTransfer(); // 设置DataTransfer类的实例
    dt.setEmail(mail);
    dt.setPassword(pass);
    InfoDisplay id = new InfoDisplay();
    id.setVisible(true);
}

DataTransfer.java

public class DataTransfer {
    private String loginEmail;
    private String loginPassword;

    public String getEmail() {
        return loginEmail;
    }

    public String getPassword() {
        return loginPassword;
    }

    public void setEmail(String email) {
        loginEmail = email;
        System.out.println("存储的电子邮件:" + loginEmail);
    }

    public void setPassword(String password) {
        loginPassword = password;
        System.out.println("存储的密码:" + loginPassword);
    }
}

InfoDisplay.java(第二个jFrame)

//构造函数

public InfoDisplay() {
    initComponents();
    //显示当前日期和时间
    Calendar cl = Calendar.getInstance();
    String datetime = cl.getTime().toString();
    jLabel5.setText(datetime);
    //从DataTransfer类获取存储的电子邮件和密码
    DataTransfer dt = new DataTransfer();
    String finalEmail = dt.getEmail();
    String finalPass = dt.getPassword();
    System.out.println("电子邮件:" + finalEmail);
    System.out.println("密码:" + finalPass);
}

我已经使用了打印命令来检查它们是否被正确赋值。我只从LoginForm.java运行程序,数据被存储在DataTransfer.java中,但在InfoDisplay()中显示的是Email: NULLPassword: NULL的值。

英文:

I am trying to transfer a email and password data taken in one jFrame to another using a separate java Class. All are in same Package. Any help is much appreciated, this is my first question so don't know the proper specifics to mention so let me know if you require.

LoginForm.java (1st jFrame)

Part where the data manipulation is present here String mail,pass; have email and password data

    if(op==true) {
                 JOptionPane.showMessageDialog(null, "Login Successful!", "WebApp Database Login",
                 INFORMATION_MESSAGE);
                 DataTransfer dt=new DataTransfer();      // Setting instance of DataTransfer class 
                                                            //to pass on Mail and Password details
                 dt.setEmail(mail);
                 dt.setPassword(pass);
                 InfoDisplay id=new InfoDisplay();
                 id.setVisible(true);
                 }

DataTransfer.java

    public class DataTransfer {
        private String loginEmail;
        private String loginPassword;

        public String getEmail() {
            return loginEmail;
        }
    
        public String getPassword() {
            return loginPassword;
        }
    
        public void setEmail(String email) {
            loginEmail=email;
            System.out.println("Stored Mail: "+loginEmail);
        }
    
        public void setPassword(String password) {
            loginPassword=password;
            System.out.println("Stored Pass: "+loginPassword);
        } }

InfoDisplay.java (2nd jFrame)

//Constructor

    public InfoDisplay() {
        initComponents();
        //Displaying Current Date and Time
        Calendar cl=Calendar.getInstance();
        String datetime=cl.getTime().toString();
        jLabel5.setText(datetime);
        //Getting the Stored details of Mail and Password from DataTransfer class
        DataTransfer dt=new DataTransfer();
        String finalEmail=dt.getEmail();
        String finalPass=dt.getPassword();
        System.out.println("Email: "+finalMail);
        System.out.println("Password: "+finalPass); }

I have used Print commands to just check whether they are assigned properly or not. I am running the program from only LoginForm.java, and the data gets stored in DataTransfer.java but it shows Email: NULL and Password: NULL values in InfoDisplay()

答案1

得分: 1

我用以下代码使其工作起来:

LoginForm.java

public class LoginForm {
    public static void main(String[] args) {
        DataTransfer dataTransfer = new DataTransfer();
        dataTransfer.setEmail(mail);
        dataTransfer.setPassword(pass);
        InfoDisplay infoDisplay = new InfoDisplay(dataTransfer);
    }
}

InfoDisplay.java

public class InfoDisplay {
    public InfoDisplay(DataTransfer dt) {
        // 显示当前日期和时间
        // 从DataTransfer类获取存储的邮件和密码详细信息
        String finalEmail = dt.getEmail();
        String finalPass = dt.getPassword();
        System.out.println("Email: " + finalEmail);
        System.out.println("Password: " + finalPass);
    }
}

DataTransfer.java

public class DataTransfer {
    private String loginEmail;
    private String loginPassword;

    public String getEmail() {
        return loginEmail;
    }

    public String getPassword() {
        return loginPassword;
    }

    public void setEmail(String email) {
        loginEmail = email;
        System.out.println("Stored Mail: " + loginEmail);
    }

    public void setPassword(String password) {
        loginPassword = password;
        System.out.println("Stored Pass: " + loginPassword);
    }
}
英文:

I got it working with this....

LoginForm.java

public class LoginForm {
    public static void main(String[] args) {
        DataTransfer dataTransfer = new DataTransfer();
        dataTransfer.setEmail(mail);
        dataTransfer.setPassword(pass);
        InfoDdisplay infoDdisplay = new InfoDdisplay(dataTransfer);
    }
}

InfoDisplay.java

public class InfoDisplay {
    public InfoDisplay(DataTransfer dt) {
        //Displaying Current Date and Time
        //Getting the Stored details of Mail and Password from DataTransfer class
        String finalEmail = dt.getEmail();
        String finalPass = dt.getPassword();
        System.out.println("Email: " + finalEmail);
        System.out.println("Password: " + finalPass);
    }
}

DataTransfer.java

public class DataTransfer {
    private String loginEmail;
    private String loginPassword;

    public String getEmail() {
        return loginEmail;
    }

    public String getPassword() {
        return loginPassword;
    }

    public void setEmail(String email) {
        loginEmail=email;
        System.out.println("Stored Mail: "+loginEmail);
    }

    public void setPassword(String password) {
        loginPassword=password;
        System.out.println("Stored Pass: "+loginPassword);
    } 
}

huangapple
  • 本文由 发表于 2020年8月30日 16:07:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63655316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定