私はまだMVCを使用するのが初めてで、ActionListenerを使用するとプログラムの実行に問題が発生し、その場合NullPointerExceptionが出力として表示されます。
目的は、ユーザーをセカンドメニューからメインメニューに戻す戻るボタンを用意することです。
コントローラーでこの行をコメントアウトすると、エラーはなくなります。 this.theView.addComBackButtonListener(new CompanyBackBtnListener());
間違っている場合は修正してください。ただし、CompanyBackBtnListener があったとしても見つからないようです。
- すでにタイプミスをチェックしてください。
コーディングを絞り込んでみました。私はまだJavaを学んでいるので、何か解決策があれば教えてください。ありがとうございました。
景色
public class HRMSViewGUI extends JFrame {
//Declarations here
private JButton exitCusBtn;
// Main Menu
public HRMSViewGUI()
{
this.setTitle("Human Resource Management System");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(600,225);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.add(HRMSMenu);
// Main Menu GUI/Buttons place here.
// This contains the Main Menu buttons and GUI only.
// Second Menu
public void comMenu(){
comFrame = new JFrame();
comFrame.setTitle("Company Menu - HRMS SYSTEM");
comFrame.setResizable(false);
comFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
comFrame.setSize(600,225);
comFrame.setLocationRelativeTo(null);
comFrame.setVisible(true);
// Swings Codings... Buttons.
// This is the Back/Return button.
GridBagConstraints cusConc = new GridBagConstraints();
exitCusBtn = new JButton("Exit to Menu");
exitCusBtn.setToolTipText("Go back to the Main Menu.");
cusConc.gridx = 3;
cusConc.gridy = 8;
cusConc.fill = GridBagConstraints.HORIZONTAL;
cusConc.insets = new Insets(5,0,0,0);
cusContainer.add(exitCusBtn,cusConc);
}
void addComBackButtonListener(ActionListener listenerforComBackButton){
exitComBtn.addActionListener(listenerforComBackButton);
}
}
コントローラー
public class HRMSControlGUI {
private HRMSViewGUI theView;
private HRMSModel theModel;
public HRMSControlGUI(HRMSViewGUI theView, HRMSModel theModel){
this.theView = theView;
this.theModel = theModel;
//set Listener for the Controller to detect the ActionListener of the View.
this.theView.addComBackButtonListener(new CompanyBackBtnListener());
}
private class CompanyBackBtnListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// Enable Main Menu
theView.setEnabled(true);
}
主要
public class HRMSapp {
public static void main(String[] args) {
HRMSViewGUI theView = new HRMSViewGUI();
HRMSModel theModel = new HRMSModel();
HRMSControlGUI theController = new HRMSControlGUI(theView, theModel);
theView.setVisible(true);
}