Modify the MyFrame class from the code above to extend View, with the following UML specification:In the constructor of MyFrame, the actionPerformed method of the ActionListener of the “Reset” button must call the resetClicked method of the controller c, and the actionPerformed method of the ActionListener of the “Undo” button must call the undoClicked method of the controller c. The controller c will then itself implement the actual meaning of the button clicks. Also move the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) method call from the constructor of MyFrame to the constructor of View. The update method must simply call Swing’s repaint method: when the model changes (because a new point has been added, for example), the model will notify all its listeners, which will call the update method of MyFrame, which will ask Swing to repaint everything; Swing will then automatically call the paintComponent method of the MyPanel (see below) which will then redraw everything (including the new point).写一个java文件
时间: 2023-12-03 10:44:52 浏览: 146
下面是一个符合要求的Java文件,其中包含了MyFrame和View两个类的实现:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends View implements ActionListener {
private JButton resetButton;
private JButton undoButton;
private Controller c;
private MyPanel myPanel;
public MyFrame(Controller c) {
super();
this.c = c;
this.resetButton = new JButton("Reset");
this.undoButton = new JButton("Undo");
this.resetButton.addActionListener(this);
this.undoButton.addActionListener(this);
this.myPanel = new MyPanel(c.getModel());
this.getContentPane().add(myPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(resetButton);
buttonPanel.add(undoButton);
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetButton) {
c.resetClicked();
} else if (e.getSource() == undoButton) {
c.undoClicked();
}
}
}
class View extends JFrame {
public View() {
super();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(new Dimension(400, 400));
this.setVisible(true);
}
public void update() {
this.repaint();
}
}
class MyPanel extends JPanel {
private Model model;
public MyPanel(Model model) {
super();
this.model = model;
this.setBackground(Color.WHITE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
ArrayList<Point> points = model.getPoints();
for (int i = 0; i < points.size(); i++) {
Point p = points.get(i);
g.fillOval(p.x - 5, p.y - 5, 10, 10);
}
}
}
```
其中,View类是一个抽象类,它包含了窗口的基本属性和方法,而MyFrame类则是继承自View类的具体窗口实现,它负责显示画板和按钮,并且处理按钮的点击事件。MyPanel类则是一个画板,负责绘制所有的点。
在构造函数中,MyFrame类会创建一个Controller对象c,并将其保存在成员变量中。同时,它会创建两个按钮resetButton和undoButton,分别用于重置和撤销操作,然后将这两个按钮添加到窗口底部的面板中。此外,它还会创建一个MyPanel对象,并将其添加到窗口的中央区域。
在MyFrame类中,实现了ActionListener接口,并覆盖了actionPerformed方法,以处理按钮的点击事件。当resetButton被点击时,它会调用Controller对象c的resetClicked方法;当undoButton被点击时,它会调用Controller对象c的undoClicked方法。
在View类中,实现了update方法,它会在模型发生变化时被调用,以便更新窗口的显示内容。在本例中,update方法只是简单地调用了Swing的repaint方法,以触发窗口的重绘操作。
在MyPanel类中,实现了paintComponent方法,它会在窗口需要重绘时被调用,以绘制所有的点。具体来说,它会从模型中获取所有的点,并将它们绘制成实心圆。
阅读全文