2016-04-15 56 views
2

我正在創建一個員工管理系統,我想知道如何在成功登錄時將工作人員的詳細信息從登錄框架傳遞到其他框架。Java將值傳遞到另一個框架

我當前的方法是在成功登錄並通過構造函數傳遞它時檢索它們的數據。

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); 
       } 

它的工作原理,但我想知道是否有其他方式來做到這一點。

回答

2

可以通過構造函數傳遞參數,但創建對象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; 
     //... 
    } 
} 

然後接收類的constuctor更改爲類似

public Home(Staff staff){ 

} 

有了這個,你將創建Home這樣:

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

如果您需要將太多參數傳遞給方法(或構造函數),最好將它們包裝在一個新類中,並將該類的實例傳遞給該方法。

1

使用框架時,通常您有更大的類,包含視圖並將它們與模型連接的控制器(檢查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(); 
    } 
} 

你應該在你的程序中做這樣的事情,一切都會更容易。

如果你不想這樣做,那麼最好的解決方案可能就是你正在做的或類似的事情,但是這樣做更容易修改,而且更容易理解。

1

怎麼樣這樣?你不需要一次又一次地傳遞許多參數。我只是建議你,但我還沒有測試過。

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 
} 
相關問題