public class AcademicController {
private AcademicModel model;
private RegistrationView view;
private MainMenu mView = new MainMenu();
byte [] person_image=null;
ImageIcon format = null;
String filename = null;
public AcademicController(AcademicModel model, RegistrationView view){
this.model = model;
this.view = view;
}
public class AcademicController() {
}
public final void setUpListeners() {
view.registerButton.addActionListener(registerListener);
view.browseButton.addActionListener(browseListener);
mView.myProfileTab.addChangeListener(new tabListener());
}
private ActionListener registerListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
try{
int id = Integer.parseInt(view.idTxt.getText());
String title = view.titleCombo.getSelectedItem().toString();
String fn = view.forenameTxt.getText();
String sn = view.surnameTxt.getText();
String role = view.roleCombo.getSelectedItem().toString();
String rm = view.roomTxt.getText();
String page = view.pageTxt.getText();
String hrs = view.hoursTxt.getText();
String phn = view.phoneTxt.getText();
String mobile = view.mobileTxt.getText();
String email = view.emailTxt.getText();
String pw = view.cPassTxt.getText();
model.registerUser(id, title, fn, sn, role, rm, page, hrs, phn, mobile, email, pw); //Model Change your state
view.showSuccess("You have registered successfully");
view.setVisible(false);
LoginForm lf = new LoginForm();
lf.setVisible(true);
}
catch(Exception ex) { view.showError(ex); }
}
};
private ActionListener browseListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
String filename = model.profImage();
view.imageTxt.setText(filename);
}
};
public class tabListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
int index = sourceTabbedPane.getSelectedIndex();
if (index == 0) {
mView.myProfile();
}
else if (index==1) {
System.out.println("State 2");
}
}
}
}
作为MVC约定的VIEW的单独的类MainMenu
由名为JTabbedPane
的{{1}}组成。如何检测索引中的更改,以便AcademicController知道这一点并相应地更改模型。上面你可以看到我已经尝试用类myProfileTab
进行设置。换句话说,我如何使用Controller类注册面板本身,以便在此处检测它。
这是另一个调用setUpListener
的类tabListener