我有一个基于模型- View - Controller 范例的项目,但在让它正常工作方面我遇到了很多麻烦。
该程序有 4 个面板,可以让我以各种方式修改在屏幕上绘制的椭圆。这些似乎工作正常,在相当大的麻烦之后,我能够让它们显示在包含整个 shebang 的 JFrame 中。我已经设法通过打破提供的说明来显示它们,但是当我这样做时,我似乎无法更新椭圆形。但是,如果我按照信中的说明进行操作,我只会看到一个空框。
该项目有非常具体的方向,我在一定程度上遵循了这些方向,但有些文档不清楚。我认为我所缺少的一定是一些简单的东西,因为没有什么是毫无意义的。我不得不承认,尽管我的 Java 经验有限,但我在 GUI 设计/范例方面的经验更是如此。
无论如何,我一直在广泛地搜索网络和这个网站,试图找出问题所在,但这是一个有点具体的例子,老实说,我对此了解不多,无法概括我得到的任何答案在网上找到并弄清楚缺少什么。我研究这段代码已经太久了,所以我真的希望有人能帮助我。
public class Model {
private Controller controller;
private View view;
private MvcFrame mvcFrame;
private int radius = 44;
private Color color = Color.BLUE;
private boolean solid = true;
//bunch of mutators and accessors for the above variables
public Model() {
controller = new Controller(this);
view = new View(this);
mvcFrame = new MvcFrame(this);
}
}
这是模型类。这似乎相当简单。我认为我对这里发生的事情的理解是可靠的,而且似乎没有任何错误。主要用于上下文。
public class Controller extends JPanel{
private Model model;
public Controller(Model model) {
this.model = model;
setBorder(BorderFactory.createLineBorder(Color.GREEN));
setLayout(new GridLayout(4,1));
add(new RadiusPanel(model));
add(new ColorPanel(model));
add(new SolidPanel(model));
add(new TitlePanel(model));
}
}
这是 Controller 类。据我所知,setBorder、setLayout 和一系列添加在这里什么都不做。我让他们注释掉了,但这是说明告诉我做事的方式,所以要么那里有错误,要么我的设置有问题。但是,当我这样做时,我会得到一个空窗口 (JFrame),但其中不会显示任何面板。我为解决这个问题所做的是将这些添加函数放在 mvcFrame 类中:
public class MvcFrame extends JFrame {
private Model model;
public MvcFrame(Model model){
this.model = model;
//setLayout(new GridLayout(4,1));
//add(new RadiusPanel(model));
//add(new ColorPanel(model));
//add(new SolidPanel(model));
//add(new TitlePanel(model));
//add(new View(model));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(800,600);
setVisible(true);
}
}
事情开始变得奇怪了。第一个注释掉的代码块与 Controller 类中的代码相同。我将其注释掉的原因是因为那只是一个幸运的猜测——根据说明,它不应该是那样的。然而,这确实可以让面板显示出来——但那时我还在费力地试图让椭圆形显示出来。
另一条注释行 ( add(new View(model)); ) 是使事情正常进行的不同尝试。在这种情况下,我将这些添加函数放在 View 类中(请参阅下面注释掉的代码)。这实际上可以同时显示椭圆形和面板,但该方法不允许我更新椭圆形。另外,虽然我刚刚显示了椭圆形,但我似乎无法弄清楚到底是什么导致了这种情况,而且我似乎无法让它恢复原状。
public class View extends JPanel{
private Model model;
public View(Model model) {
this.model = model;
//setLayout(new GridLayout(4,1));
//add(new RadiusPanel(model));
//add(new ColorPanel(model));
//add(new SolidPanel(model));
//add(new TitlePanel(model));
repaint();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
//center of view panel, in pixels:
int xCenter = getWidth()/2;
int yCenter = getHeight()/2;
int radius = model.getRadius();
int xStart = xCenter - radius;
int yStart = yCenter - radius;
int xWidth = 2 * radius;
int yHeight = 2 * radius;
g.setColor(model.getColor());
g.clearRect(0, 0, getWidth(), getHeight());
if (model.isSolid()){
g.fillOval(xStart, yStart, xWidth, yHeight);
} else {
g.drawOval(xStart, yStart, xWidth, yHeight);
}
}
}
有点像以前的想法 - 注释掉的代码是我添加的东西,试图让事情正常进行,但不是基于提供的指示。在那些东西没有被注释的情况下,我有 add(new View(model)); mvcFrame 行中的行也未注释。
各种面板类(SolidPanel、ColorPanel 等)只是扩展了一个名为 ControlPanel 的类,后者扩展了 JPanel。这些似乎都按预期工作,没有太大问题。还有一个启动 GUI 的驱动程序。这似乎也按预期工作。
我遇到的主要问题是我无法让椭圆形显示出来,有一次我可以让它显示出来,但似乎没有任何更改它的选项有效。我觉得我已经很接近了,但我现在对尝试其他事情一筹莫展。
任何能提供帮助的人都会有我最诚挚的谢意。
最佳答案
这里是对这个东西的非常仓促的重写,“只是让它工作”。
main.java
public class main {
public static void main(String[] args) {
// The JFrame could be created here, since it lasts the life
// of the program.
//...then, later, the model.
Model mdl = new Model();
// ...and then move on to applying the view and control to the frame.
}
}
Controller.java
// Nothing interesting here, added for consistency.
public class Controller {
private final Model model;
public Controller(Model model) {
// The frame is shown automatically in the model here.
this.model = model;
// The frame's setVisible is a control issue, should be called
// from in here, not automatically in the model.
}
}
MvcFrame.java
import javax.swing.JFrame;
public class MvcFrame extends JFrame {
private final Model model;
public MvcFrame(Model model){
this.model = model;
// Anytime you add anything to a JFrame, use the content pane.
this.getContentPane().add(model.getView());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// This line centers the frame on your screen.
setLocationRelativeTo(null);
setSize(800,600);
// The frame won't paint until it's visible.
// This means if you check dimensions, everything will be 0 x 0
// until this line is called.
setVisible(true);
}
}
模型.java
import java.awt.Color;
public class Model {
private final Controller controller; // Not used yet
private final View view;
private final MvcFrame mvcFrame; // Not used yet
// Mutators and accessors needed for these guys (set/get)
private final int radius = 44;
private final Color color = Color.BLUE;
private final boolean solid = true;
public Model() {
controller = new Controller(this);
view = new View(this);
mvcFrame = new MvcFrame(this);
}
public View getView() {
return view;
}
public int getRadius() {
return radius;
}
public Color getColor() {
return color;
}
public boolean isSolid() {
return solid;
}
}
View.java
import java.awt.Graphics;
import javax.swing.JPanel;
public class View extends JPanel{
private final Model model;
public View(Model model) {
this.model = model;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
//center of view panel, in pixels:
int xCenter = getWidth()/2;
int yCenter = getHeight()/2;
int radius = model.getRadius();
int xStart = xCenter - radius;
int yStart = yCenter - radius;
int xWidth = 2 * radius;
int yHeight = 2 * radius;
g.setColor(model.getColor());
g.clearRect(0, 0, getWidth(), getHeight());
if (model.isSolid()){
g.fillOval(xStart, yStart, xWidth, yHeight);
} else {
g.drawOval(xStart, yStart, xWidth, yHeight);
}
}
}
关于Java MVC 项目——要么我不能更新绘图,要么我看不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13737051/