简体   繁体   English

如何从Java中的另一个类调用字符串或任何数据类型

[英]How to call a String or any datatype from another class in java

I have a LoginGUI and a MainGUI, what I want to do is return the current user as a String or the UserID as an Integer in my MainGUI. 我有一个LoginGUI和MainGUI,我想做的是在MainGUI中以字符串形式返回当前用户或以Integer形式返回UserID。 My program runs the LoginGUI and then if the UserName a password match up with the database it lets you through to the MainGUI. 我的程序运行LoginGUI,然后如果UserName密码与数据库匹配,它将允许您进入MainGUI。 I've declared a CurrUserID and CurrUser in my LoginGUI - both are public, but how can I use them in my MainGUI? 我已经在我的LoginGUI中声明了CurrUserID和CurrUser-它们都是公共的,但是如何在MainGUI中使用它们呢?

Here is the code for my LoginGUI - I'm using swing. 这是我的LoginGUI的代码-我正在使用swing。

    private void loginButActionPerformed(java.awt.event.ActionEvent evt) {                                         
    for (int i = 0; i <= size; i++) {
        if (login.getText().equals(users[i].getUser())) {
            currUser = users[i].getUserID();
            if (password.getText().equals(users[i].getPassword())) {
                try {
                    MainGUI main = new MainGUI(users);
                    main.setVisible(true);
                    this.dispose();
                } catch (SQLException ex) {
                    Logger.getLogger(LoginGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else{
                JOptionPane.showMessageDialog(null, "Incorrect Password");
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Incorrect UserName");
        }
    }
}                                        

public int getCurrUser() {
    return currUser;
}

What I want to do is return the currUser while I'm in my MainGUI. 我想做的是在MainGUI中返回currUser。 How can I? 我怎样才能?

Add a member, say String uid; 添加一个成员,例如String uid; to your MainGUI class, and create two constructors, one that accepts String and other that accepts Integer ; 到您的MainGUI类,并创建两个构造函数,一个接受String ,另一个接受Integer in 'em, set uuid to whatever is passed. 在'em'中,将uuid设置为所传递的值。 Now you'll have your user id tied to MainGUI for later use. 现在,将您的用户ID绑定到MainGUI,以供以后使用。

So, when you're done with LoginGUI, initialize the new MainGUI(passedID) . 因此,当您完成LoginGUI时,请初始化new MainGUI(passedID)

You could add a setter method in your MainGUI class and call it like so: 您可以在MainGUI类中添加一个setter方法, MainGUI调用它:

main.setCurrentUser(currUser);

For passing multiple fields from LoginGUI to MainGUI you could use a wrapper class, eg 为了将多个字段从LoginGUI传递到MainGUI ,可以使用包装器类,例如

CurrUserDetails details = new CurrUserDetails(userID, userName);
main.setCurrentUserDetails(currUser);

in order to invoke any non-static method of a class, you require an instance of the class, for instance 为了调用一个类的任何非静态方法,您需要一个该类的实例,例如

public class Foo{
    public void bar(){
        // do stuff
    }
}

Foo f = new Foo();
f.bar(); // invoking non-static method of class Foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM