按钮传递尝试从JComponent调用函数时MVC函数错误 [英] MVC function error when button passed trying to call a function from JComponent
问题描述
我不确定我是否正在进行正确的MVC,但我无法从我的视图中执行 Clear()
。
I am not sure if I am doing a proper MVC, but I am unable to execute Clear()
from my view.
Main
public class Main{
public static void main(String[] args) {
Model model = new Model();
final View view = new View();
Controller controller = new Controller(view, model);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
view.showView();
}
});
}
}
模型
public class Model {
DrawPad drawPad = new DrawPad();
Model() {
}
public void Clear() {
drawPad.clear();
System.out.print("HELP");
}
}
查看
public class View extends JFrame {
JButton clearButton = new JButton("Clear");
DrawPad drawPad = new DrawPad();
Model model;
public void showView() {
JFrame frame = new JFrame();
drawPad.DrawPad();
frame.add(drawPad, BorderLayout.CENTER);
frame.add(clearButton, BorderLayout.SOUTH);
frame.setSize(280, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void addButtonActionListener(ActionListener clr) {
clearButton.addActionListener(clr);
}
}
控制器
public class Controller implements ActionListener {
View view;
Model model;
Controller(View v, Model m) {
this.view = v;
this.model = m;
view.addButtonActionListener(this);
}
public void actionPerformed(ActionEvent e) {
model.Clear();
System.out.print("H");
}
}
JComponent
JComponent
class DrawPad extends JComponent {
Image image;
public Graphics2D graphics2D;
int currentX, currentY, oldX, oldY;
public void DrawPad() {
setDoubleBuffered(false);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
if (graphics2D != null)
graphics2D.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
public void paintComponent(Graphics g) {
if (image == null) {
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D) image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
public void clear() {
System.out.println("Test");
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}
}
如果我从我的视图中使用事件监听器而我创建一个 drawPad
并直接调用 clear()
,我能够执行。但是,当我调用clear form形式的模型和控制器时,它会抛出一个错误。
If I use an event listener from my view and I create a drawPad
and call clear()
directly, I am able to executed. However when I called clear form a model and controller it throws an error.
如果我做错了什么想法?也许我错过了什么?
Any ideas if I am doing anything wrong? Perhaps I am missing something?
推荐答案
试图将纯MVC范式包装在非纯MVC上范例以及你的 DrawPad
实际上应该拥有它自己的MVC,你需要提供一个观察者模式,它允许控制器
监视中可能发生的变化型号
。
Stepping around the issues of trying to wrap a pure MVC paradigm ontop of a non-pure MVC paradigm and the fact that your DrawPad
should actually have it's own MVC wrapped around it, you need to provide a Observer Pattern which allows the Controller
to monitor for changes which might occur in the Model
.
例如......
public interface ModelListener {
public void modelCleared(Model model);
}
public class Model {
private List<ModelListener> listeners;
Model() {
listeners = new ArrayList<>(25);
}
public void addModelListener(ModelListener listener) {
listeners.add(listener);
}
public void Clear() {
System.out.print("HELP");
for (ModelListener listener : listeners) {
listener.modelCleared(this);
}
}
}
然后,当你创建你的控制器
,您将向模型注册 ModelListener
...
Then, when you create your Controller
, you would register a ModelListener
to the model...
public class Controller implements ActionListener {
View view;
Model model;
Controller(View v, Model m) {
this.view = v;
this.model = m;
model.addModelListener(new ModelListener() {
@Override
public void modelCleared(Model model) {
view.clear();
}
});
view.addButtonActionListener(this);
}
public void actionPerformed(ActionEvent e) {
model.Clear();
System.out.print("H");
}
}
这篇关于按钮传递尝试从JComponent调用函数时MVC函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!