Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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和MouseStener实现observer模式_Java_Oop_Mouselistener - Fatal编程技术网

用Java和MouseStener实现observer模式

用Java和MouseStener实现observer模式,java,oop,mouselistener,Java,Oop,Mouselistener,我有三个类:Main、DrawingPanel和ToolboxPanel。ToolboxPanel包含所有“我的”按钮,包括“撤消”按钮。DrawingPanel是我绘制对象的地方。我希望在屏幕上绘制对象时启用“撤消”按钮,在屏幕上没有其他对象时禁用该按钮。Main创建DrawingPanel和ToolboxPanel的实例。如果我使用静态方法并调用Main.setUndoStatusfalse,我可以让我的undo按钮正常工作;从绘图面板。setUndoStatus然后调用toolboxPan

我有三个类:Main、DrawingPanel和ToolboxPanel。ToolboxPanel包含所有“我的”按钮,包括“撤消”按钮。DrawingPanel是我绘制对象的地方。我希望在屏幕上绘制对象时启用“撤消”按钮,在屏幕上没有其他对象时禁用该按钮。Main创建DrawingPanel和ToolboxPanel的实例。如果我使用静态方法并调用Main.setUndoStatusfalse,我可以让我的undo按钮正常工作;从绘图面板。setUndoStatus然后调用toolboxPanel中的setter。然而,我一直在阅读关于观察者模式和听众的文章,我认为我可能没有以最佳实践的方式来做这件事

我应该如何正确地使用观察者模式和/或鼠标侦听器来执行此操作?或者任何更好的方法

这里有一些代码与我正在做的有些相似

public class Main
{
    DrawingPanel drawingPanel;
    ToolboxPanel toolboxPanel;

    public Main()
    {
        drawingPanel = new DrawingPanel();
        toolboxPanel = new ToolboxPanel(drawingPanel);
    }
}

//A static method here to setUndoStatus, but I feel like I shouldn't have it
public static void setUndoStatus(boolean b)
{
    {
    toolboxPanel.setUndoStatus(b);
    }
}


public class ToolboxPanel
{
    JButton undoButton;
    public ToolboxPanel(DrawingPanel drawingPanel)
    {
        undoButton = new JButton("Undo");
        undoButton.setEnabled(false);
        undoButton.addActionListener
        (
            new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    drawingPanel.undo();
                    undoButton.setEnabled(drawingPanel.getUndoStatus());
                }
            }
        );
    }

    public void setUndoStatus(boolean status)
    {
        undoButton.setEnabled(status);
    }
}

public class DrawingPanel
{
    public DrawingPanel()
    {
        addMouseListener(new MouseAdapter() 
        {

            public void mouseReleased(MouseEvent e)
            {
                //Some code here that's unrelated
                if(objectsExist == true) //If something gets drawn, whatever
                {
                    Main.setUndoStatus(true); //Don't like this
                }
            }
        });
    }
}

我自己,我会从我的模型类开始,一个完全脱离GUI和事件侦听器的类,而不是我的控件或视图类。我不知道你现在不考虑模型是否是本末倒置。它应该保存哪些数据?你需要它有什么样的行为?它应该有什么类型的侦听器?我不是专业人士,但我经常给我的模型提供一个SwingPropertyChangeListener,其中包含多个String propertyName常量,这些常量在模型接口中定义为枚举或常量。Java有一个内置的观察者/可观察模式。看,它并不适用于所有场景,但应该会给你一个想法。也要牢记气垫船的建议。你可能想研究一种类似MVC的模式,你是对的,Birdie,避免使用静态字段和方法来保存对象状态和行为代码。这样会导致无法稳定、无法识别的代码。当您在drawingPanel上绘制控制器时,我的drawingPanel实际上既是一个视图又是一个控制器,并且还可以看到以前绘制的对象视图,这有关系吗?我想我会用Main作为我的模型。我会看一看,看看我是否能解决这个问题。对我来说,最重要的是将程序的逻辑部分与所有其他部分分开,让它们远离用户交互,远离事件代码,使其尽可能不受用户界面的影响,并使其干净、合乎逻辑,使其美观。