Java将值传递到另一个帧
我正在创建一个员工管理系统,我想知道当员工成功登录时,如何将他们的详细信息从登录框架传递到其他框架 我当前的方法是在用户成功登录时检索他们的数据,并将其传递给构造函数Java将值传递到另一个帧,java,oop,Java,Oop,我正在创建一个员工管理系统,我想知道当员工成功登录时,如何将他们的详细信息从登录框架传递到其他框架 我当前的方法是在用户成功登录时检索他们的数据,并将其传递给构造函数 if(checkPassword.equals(password)&&checkStaffId.equals(staffId)){ close(); String name = temp.getName();
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);
}
这是可行的,但我想知道是否有其他方法可以做到这一点 如果需要向方法(或构造函数)传递太多参数,最好将它们包装在一个新类中,并将该类的实例传递给该方法。处理框架时,通常会使用一个更大的类,一个控制器,它包含视图并将它们与模型连接(选中) 长话短说,你应该(你没有必须,但在我看来,这是最好的方法)这样做: 注意:这与您的代码无关,只是为了展示一个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();
}
}
你只需要在你的程序中做一些类似的事情,一切就会变得简单
如果您不想这样做,那么最好的解决方案可能就是您正在做的事情或类似的事情,但这更容易在以后修改,而且更容易理解。可以通过构造函数传递参数,但创建对象
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;
//...
}
}
然后将接收类的构造函数更改为
public Home(Staff staff){
}
使用此选项,您将通过以下方式创建主页
:
Staff staff = new Staff(temp);
Home page = new Home(staff);
这边走怎么样?。您不需要一次又一次地通过许多参数。我只是建议您,但我还没有测试
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
}