[英]How to implement MouseListener into the controller in Java
我一直在使用Model View Control设计开发Tic tac toe程序。 我试图将鼠标侦听器添加到控制器中的视图。 做这个的最好方式是什么?
我的控制器看起来像这样。
public class TicTacToeViewController implements MouseListener{
TicTacToeView view;
TicTacToeModel model;
Color oColor=Color.BLUE, xColor=Color.RED;
public TicTacToeViewController(TicTacToeView view, TicTacToeModel model) {
this.model = model;
this.view = view;
// this.view.addMouseListener(new TicTacToeViewController(view, model));
}
public void mouseClicked(MouseEvent e) {
int xpos=e.getX()*3/view.getWidth();
int ypos=e.getY()*3/view.getHeight();
//System.out.println("Play "+xpos+","+ypos);
play(xpos,ypos);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
基本上,我无法在我的井字游戏程序的单元格中单击。 我需要帮助。
谢谢。
在这种情况下,要假定视图类中有addMouseListener方法,则通过传入“ this”来将此控制器的MouseListener添加到视图中。
this.view.addMouseListener(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.