Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MVC项目-我可以';t更新图纸,否则我可以';我看不出来_Java_Swing_Model View Controller_Drawing_Java 7 - Fatal编程技术网

Java MVC项目-我可以';t更新图纸,否则我可以';我看不出来

Java MVC项目-我可以';t更新图纸,否则我可以';我看不出来,java,swing,model-view-controller,drawing,java-7,Java,Swing,Model View Controller,Drawing,Java 7,我有一个基于模型-视图-控制器范例的项目,我在让它正常工作方面遇到了很多麻烦 该程序有4个面板,应该允许我以各种方式修改屏幕上绘制的椭圆形。这些似乎工作得很好,经过相当多的麻烦之后,我能够让它们显示在JFrame中,其中包含了整个shebang。我已经设法让他们显示脱离提供的指示,但当我这样做,我似乎无法让椭圆形更新。然而,如果我按照信中的说明去做,我只会看到一个空框 该项目有相当具体的方向,我对此进行了一定程度的跟进,但其中一些文档并不清楚。我想我错过的一定是一些简单的东西,因为没有什么东西会

我有一个基于模型-视图-控制器范例的项目,我在让它正常工作方面遇到了很多麻烦

该程序有4个面板,应该允许我以各种方式修改屏幕上绘制的椭圆形。这些似乎工作得很好,经过相当多的麻烦之后,我能够让它们显示在JFrame中,其中包含了整个shebang。我已经设法让他们显示脱离提供的指示,但当我这样做,我似乎无法让椭圆形更新。然而,如果我按照信中的说明去做,我只会看到一个空框

该项目有相当具体的方向,我对此进行了一定程度的跟进,但其中一些文档并不清楚。我想我错过的一定是一些简单的东西,因为没有什么东西会因为没有意义而冲我跳出来。但我不得不承认,我的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));
    }
}
这是控制器类。据我所知,setboorder、setLayout和一系列的add在这里没有任何作用。我已经把它们注释掉了,但这是说明书告诉我的方法,所以要么是那里有错误,要么是我的设置有问题。然而,当我这样做的时候,我会得到一个空窗口(JFrame),但是没有一个面板显示在其中。我解决这个问题的方法是将这些add函数放在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);
    }
}
这就是事情开始变得奇怪的地方。注释掉的第一块代码与控制器类中的代码相同。我之所以把它注释掉,是因为这只是一个幸运的猜测——根据说明,它不应该是这样的。然而,这确实有助于让面板显示出来——但在那一点上,我仍然在努力让椭圆形显示出来

另一条注释行(add(newview(model));)是另一种尝试。在本例中,我将这些add函数放在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(newview(model));mvcFrame行中未注释的行

各种面板类(SolidPanel、ColorPanel等)只是扩展了一个名为ControlPanel的类,该类扩展了JPanel。这些似乎都像预期的那样工作,没有太多问题。还有一个启动GUI的驱动程序。这似乎也如预期的那样起作用

我面临的主要问题是,我无法让椭圆出现,而有一次我可以让它出现,所有改变它的选项似乎都不起作用。我觉得我已经很接近了,但我现在还不知道还有什么可以尝试


任何能提供帮助的人都将得到我最诚挚的感谢。

您的问题有点杂乱无章,而且我不知道您为什么要注释掉代码的关键部分,但第一个问题是:

//add(new View(model));
是否要将视图添加到框架中?椭圆形可以画得很好,但视图没有添加


此代码中很可能存在问题:

public class MvcFrame extends JFrame {
    ...

    public MvcFrame(Model model){
        ...
        //setLayout(new GridLayout(4,1));
        //add(new RadiusPanel(model));
        ...
    }
}
当您直接调用
add
时,它会引用超类(JFrame)。不幸的是,JFrames很狡猾,因为它们有一个保存布局的
contentPane
。更隐蔽的是:该内容窗格是一个空布局,只能通过放入自己的面板来更改

所以,你可能应该做这样的东西。即使您没有完全遵循这些方法,这些方法也会对您有很大帮助:

...
JPanel pnl = new JPanel(new GridLayout(4, 1));
this.setContentPane(pnl);
pnl.add(new RadiusPanel(model));
...
如果不想显式设置内容窗格,可以使用
this.getContentPane().add(foo)

空布局问题也可能影响椭圆形图形,因为添加JPanel时,未指定其大小,因此默认为
(0,0)



另外,不确定控制器为什么扩展JPanel。控制器应该可以使用您的视图,并且视图中应该只有swing组件。

这里有一个非常仓促的重写,“只是为了让它工作”

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

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.
    }
}
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);
    }
}
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;       
    }
}
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);
        }        
    }
}
Model.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.
    }
}
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);
    }
}
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;       
    }
}
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);
        }        
    }
}
View.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.
    }
}
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);
    }
}
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;       
    }
}
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);
        }        
    }
}

虽然我遇到了不同的问题,但我认为:

g、 clearRect(0,0,getWidth(),getHeight())


在我的项目中为我解决了所有的问题,因为它并没有抹去之前的椭圆形,所以我只能在我把它变大的情况下才能看到变化。谢谢。

控制器扩展JPanel只是因为我被告知这样做。。。知道这不是最好的原因吗>\u>至于你对内容窗格的指导,我使用的是getContentPane.add(foo),但我对add的理解是它自动获得了内容窗格。我将对此进行一次尝试-在mvcFrame将具有那些添加函数而不是控制器的上下文中,这似乎是正确的吗?这是我的怀疑