没有调用Observer update()方法

时间:2017-03-23 19:21:07

标签: java methods observable notify

在我的Observable类中,我有4个使用setChanged()和notifyObservers()的方法,但是Observer类中的update()方法没有运行。

这是更新方法:

@Override
public void update(Observable o, Object arg) {

    model = (Model) o;
    System.out.println("updated");

    if (!( Integer.parseInt(fromDropDownMenu.getSelectedItem().toString()) >= Integer.parseInt(toDropDownMenu.getSelectedItem().toString()) )){
        switch (model.getMethodNumber()){
        case 1:
            System.out.println("LEFT HAS BEEN PRESSED");
            if (currentCenterPanelIndex == 0){
                System.out.println("Can't go left.");
            } else {
                currentCenterPanel = listOfPanels[currentCenterPanelIndex--];
            }
            break;
        case 2:
            System.out.println("RIGHT HAS BEEN PRESSED");
            if (currentCenterPanelIndex == 3){
                System.out.println("Can't go right.");
            } else {
                currentCenterPanel = listOfPanels[currentCenterPanelIndex++];
            }
            break;
        }
    }

}

这是在Observable类中:

private int methodNum;

public void comboBoxUpdate(){
    setChanged();
    notifyObservers();
}

public void setMethodNum(int x){
    this.methodNum = x;
    setChanged();
    notifyObservers();
}

public int getMethodNumber(){
    return this.methodNum;
}

public void moveLeft(){
    System.out.println("LEFT");
    setMethodNum(1);
    setChanged();
    notifyObservers();
}

public void moveRight(){
    System.out.println("RIGHT");
    setMethodNum(2);
    setChanged();
    notifyObservers();
}

0 个答案:

没有答案