1

这是 MVC 范例的类视图,该类由 2 组成,单击-和JDialogs时将打开。JMenuItemaddEventeditEvent

 public class EventView extends javax.swing.JFrame {



 private javax.swing.JDialog addDialog;
private javax.swing.JDialog editDialog;

private EventModel model;

     /** Constructor */
    public EventView(EventModel model) {
        initComponents();
        this.model = model;
        updateEventTable();       
    }

   public void addEventListener(ActionListener al) {
        addEventButton.addActionListener(al);
    }

  /* public void clearListener(ActionListener cl) {
        clearEventButton.addActionListener(cl);
    }*/

   public void addDialog(ActionListener ae) {
        addEvent.addActionListener(ae);

    }
   public void editDialog(ActionListener ee) {
        editEvent.addActionListener(ee);
    }

} 

控制器类处理用户与监听器的交互。

public class EventController implements ActionListener {

//... The Controller needs to interact with both the Model and View.
private EventModel model;
private EventView view;


/** Constructor */
public EventController(EventModel model, EventView v){

    model = new EventModel();
    view = v;
    //... Add listeners to the view.
    view.addEventListener(new addEventListener());
    //view.clearListener(new clearEventListener());
    view.addDialog(new addDialogListener());
    view.editDialog(new editDialogListener());
}

class addEventListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String name = "";
        String date;
        String start="";
        String end="";
        String venue="";
        String details="";
        String opportunities="";
        String moreOppor="";

        try {
            name = view.getEventName();
            date = view.eventDate().toString();
            start = view.startTime();
            end = view.endTime();
            venue = view.locationWhere();
            details = view.getDetails();
            opportunities = view.getOpportunities();
            moreOppor = view.getMore();
            model.addEvent(name,date,start,venue,details,opportunities,moreOppor,end);    
            view.showSuccess("Event Added!");

        } catch (Exception ex) {
            view.showError(ex);
        }
    }
}


class addDialogListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
       System.out.println("");

    }
}
class editDialogListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
         System.out.println("");
    }
}

我有两个关于这个模块的问题:

  1. EventController显示一个错误,它不是抽象的,并且当我相信我这样做时不会覆盖抽象方法 actionPerformed。如我错了请纠正我。我确实有一个额外的 JMenuItem 被调用deleteEvent,但我还没有触及它。在 NetbeansIDE 上工作仅供参考

  2. 我想System.out.println("");用可以让我显示addDialog视图类的对话框但我无法访问该组件的东西替换这些行。这个怎么做?我已经尝试过view.,但它没有显示允许setVisible(true).

4

3 回答 3

3
  1. 您的编译器非常正确: EventController没有声明public void actionPerformed(ActionEvent e)方法。但是,它确实有两个具有该方法的内部类,但这不算数。

您命名类的方式addEventListener表明您确实打算调用该方法addEventListener而不是声明一个类,但是根据您的代码无法给出明确的建议。

于 2013-01-18T11:32:21.913 回答
0
public class EventController {

}

代替

public class EventController implements ActionListener {

}
于 2013-01-18T11:53:43.207 回答
0

1)您忘记在 EventController 类中实现 actionPerformed() 方法。

2) 您应该将视图设置为 addDialogListener 和 editDialogListener 的参数。就像是 :

public class addDialogListener implements ActionListener {

    private EventView view;

    public addDialogListener(EventView view){
       this.view = view;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       view.doWhatever();
    }
}

接着 :

view.addDialog(new addDialogListener(view));
于 2013-01-18T11:34:07.653 回答