简体   繁体   English

Java将值传递到另一个框架

[英]Java passing values to another frame

I am creating a staff management system and i would like to know how i can pass the details of the staff from the login frame to other frames when they successfully login. 我正在创建一个人员管理系统,我想知道如何在职员成功登录时将职员的详细信息从登录框架传递到其他框架。

My current method is to retrieve their data when the successfully login and pass it through the constructor. 我当前的方法是在成功登录时检索其数据,并将其通过构造函数。

if(checkPassword.equals(password)&&checkStaffId.equals(staffId)){
                    close();
                    String name = temp.getName();
                    String position = temp.getPosition();
                    String imageSrc = temp.getImageSRC();
                    String email = temp.getEmail();
                    Home page = new Home(staffId,name,position,imageSrc,email);
                    page.setVisible(true);
                    MainInterface menu = new MainInterface(staffId,name,position,imageSrc,email);
                    form b = new form(staffId,name,position,imageSrc,email);
                    Patients a = new Patients(staffId,name,position,imageSrc,email);
                    AdminMenu admin = new AdminMenu(staffId,name,position,imageSrc,email);
                    MainRpt r = new MainRpt(staffId,name,position,imageSrc,email);
                    viewSchedule s = new viewSchedule(staffId,name,position,imageSrc,email);
                }

It works but I would like to know if there is other ways of doing it. 它可行,但是我想知道是否还有其他方法可以做到。

It's okay to pass your arguments through the constructor but It's more flexible to create an Object Staff to encapsulate all the fields you need to pass from on class to another. 可以通过构造函数传递参数,但是创建对象Staff来封装从类传递到另一个域所需的所有字段,则更加灵活。

public class Staff {
    private String name;
    private String position;
    private String imageSrc;
    private String email;

    public Staff(Object temp){ // Change object here to the "temp" type
       this.name = name;
       //...
    }
}

Then change the constuctor of the receiving classes to something like 然后将接收类的构造函数更改为类似

public Home(Staff staff){

}

With this you will create Home this way : 这样,您将以以下方式创建Home

Staff staff = new Staff(temp); 
Home page = new Home(staff);

When working with frames usually you have a bigger class, a controller, that contains the views and connects them with the model (check MVC ). 通常,在使用框架时,您会拥有一个更大的类,即一个控制器,其中包含视图并将其与模型连接(请检查MVC )。

Long story short, you should (you don't have to, but in my opinion it's the best approach) do something like this: 长话短说,你应该(你不就得了,但在我看来这是最好的办法)做这样的事情:

NOTE: this has nothing to do to your code, is just made to show an example of MVC. 注意:这与您的代码无关,只是为了显示MVC的示例。

class Model {
    int firstNumber;
    int secondNumber;

    public int add () {
        return firstNumber + secondNumber;
    }
}

class View extends JFrame {

    JTextArea first;
    JTextArea second;

    JLabel result;

    // do necessary stuff to show the JFrame, and add this TextAreas and the label
}

class Controller {
    Model model;
    View view;

    // Initialize both in the constructor

    public void add() { // This can be called when you press certain button, for example
        model.firstNumber = view.first;
        model.secondNumber = view.second; // THIS IS PSEUDO CODE, you have to convert it and stuff

        view.result = model.add();
    }
}

You just should do something like this in your program, and everything will be easier. 您只需要在程序中执行类似的操作即可,一切都会变得更加容易。

If you don't want to do this, then probably the best solution for you is just what you are doing or something similar, but this is easier to modify afterwards, and easier to understand. 如果您不想这样做,那么可能最适合您的解决方案就是您正在做的事情或类似的事情,但这以后更容易修改,也更容易理解。

How about this way?. 这样呢? You do not need to pass many parameter again and again .I just suggest to you but i have not tested yet. 您不需要一次又一次地传递许多参数。我只是向您建议,但我尚未进行测试。

if(checkPassword.equals(password)&&checkStaffId.equals(staffId)){
    close();
    StaffInfo staffInfo =  new StaffInfo();
    staffInfo.setName(temp.getName());
    staffInfo.setPosition(temp.getPosition());
    staffInfo.imageSrc(temp.getImageSRC());
    staffInfo.setEmail(temp.getEmail());

    Home page = new Home(staffInfo);
    page.setVisible(true);
    MainInterface menu = new MainInterface(staffInfo);

    form b = new form(staffInfo);
    Patients a = new Patients(staffInfo);
    AdminMenu admin = new AdminMenu(staffInfo);
    MainRpt r = new MainRpt(staffInfo);
    viewSchedule s = new viewSchedule(staffInfo);
}         

public class StaffInfo {

    private String name ;
    private String position;
    private String imageSrc;
    private String email;

    // Getters and setters
}

如果需要将太多参数传递给方法(或构造函数),则最好将它们包装在新类中,然后将该类的实例传递给方法。

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

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