gpt4 book ai didi

java - 尝试从 JComponent 调用函数时传递按钮时出现 MVC 函数错误

转载 作者:行者123 更新时间:2023-11-29 03:03:39 25 4
gpt4 key购买 nike

我不确定我是否在做一个正确的 MVC,但我无法从我的 View 中执行 Clear()

主要

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);
}
}

Controller

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");
}
}

J组件

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();
}
}

如果我从我的 View 中使用事件监听器并创建一个 drawPad 并直接调用 clear(),我就能够执行。但是,当我调用 clear form 模型和 Controller 时,它会抛出错误。

如果我做错了什么,有什么想法吗?也许我遗漏了什么?

最佳答案

绕过尝试将纯 MVC 范例包装在非纯 MVC 范例之上的问题以及您的 DrawPad 实际上应该有自己的 MVC 包装的问题,您需要提供一个Observer Pattern这允许 Controller 监视 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);
}
}
}

然后,当您创建Controller 时,您将向模型注册一个ModelListener...

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");
}
}

关于java - 尝试从 JComponent 调用函数时传递按钮时出现 MVC 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33359089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com