这是MVC范式的类视图,该类由2个JDialogs
组成,单击JMenuItem
-addEvent
和editEvent
即可打开。
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("");
}
}
我有两个关于这个模块的问题:
EventController
显示了一个错误,即它不是抽象的,并且没有覆盖抽象方法actionPerformed when I consided I'd。如果我错了,请纠正我。我确实有一个名为deleteEvent
的附加JMenuItem,但我还没有接触它。在NetbeansIDE F.Y.I 上工作我想将
System.out.println("");
行替换为允许我显示视图类的对话框addDialog
的内容,但我无法访问该组件。如何做到这一点?我试过view.
,但不允许使用setVisible(true)
。
- 您的编译器非常正确:
EventController
没有声明public void actionPerformed(ActionEvent e)
方法。不过,它确实有两个内部类具有该方法,但这不算在内
您为类命名addEventListener
的方式表明,您确实想调用方法addEventListener
,而不是声明一个类,但根据您的代码,无法给出明确的建议。
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));
public class EventController {
}
而不是
public class EventController implements ActionListener {
}