我想在我的程序來實現MVC增加聽衆JPanels,這是目前我的控制器類:MVC,幷包含在其他其它的組件
public class Controller {
private DatabaseModel model;
private View view;
public Controller(View view, DatabaseModel model){
this.model = model;
this.view = view;
this.view.getMainPanel().getCandidateForm().
addSubmitListener(new CandidateListener());
}
class CandidateListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("ACTION");
}
}
}
,因爲我認爲包括一些私人子JPanels - MainFrame和CandidateForm - 後者是按鈕的位置,在第10行,我將候選窗體面板傳遞迴控制器,然後將其稱爲'addSubmitListener()'方法,該方法將自定義偵聽器添加到按鈕。
這看起來像是添加偵聽器的正確方法嗎?我應該如何在Controller中定義CandidateListener?或者可能在子JPanel中創建一個匿名類?
有關MVC和聽衆的最佳實踐的任何其他建議也將不勝感激!
[在Java開發中使用Swing正確實現MVC模式]可能的副本(http://stackoverflow.com/questions/25502552/correctly-implementing-the-mvc-pattern-in-gui-development-using -swing-in-java) –