【发布时间】:2016-05-04 10:29:09
【问题描述】:
我进行了搜索,但没有找到类似问题的任何解决方案。我对 Observer/Observable 模式有疑问。 Observable 在名为“firstmap”的类中实现,这些类是:
class ObservableValue extends Observable{
private String n;
public ObservableValue(String n)
{
this.n = n;
}
public void setValue(String n)
{
this.n = n;
setChanged();
notifyObservers();
}
public String getValue()
{
return n;
}
}
public class TextObserver implements Observer
{
private ObservableValue ov = null;
private multiwindow window1;
public TextObserver(ObservableValue ov, multiwindow window1)
{
this.ov = ov;
this.window1=window1;
}
public void update(Observable obs, Object obj)
{
if (obs == ov)
{
System.out.println(ov.getValue());
String[] tmp = ov.getValue().split(",");
window1.destMessage(tmp[0], tmp[1]);
}
}
}
问题是只有当窗口“firstmap”在前台时才会执行更新。否则,在我将窗口置于前台之前,它不会更新。有没有办法让它在后台运行时更新? 谢谢。
编辑:我没有提到“firstmap”是一个 JFrame。
edit2:更多细节:
“firstmap”是显示物体运动的地图。当一个对象到达特定位置时,我希望一条消息出现在另一个 JFrame(“多窗口”)中。现在的代码方式,只有当“firstmap”在前台时才会出现消息。
在“firstmap”类中有
ObservableValue ov = new ObservableValue("");
TextObserver to = new TextObserver(ov, window1);
public firstmap() {
this.setTitle("Map");
ov.addObserver(to);
window1.setVisible(true);
canvas = new DrawCanvas();
this.setContentPane(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.pack();
this.setSize(width, height);
canvas.setPreferredSize(new Dimension(width, height));
this.setAlwaysOnTop(true);
timer = new Timer(DELAY, this);
timer.start();
this.setVisible(true);
}
...
for (int i = 0; i < window1.getaircraftlist().size(); i++) {
if (window1.getaircraftlist().get(i).getReached()==false) {
g.drawImage(window1.getaircraftlist().get(i).getImage(), window1.getaircraftlist().get(i).getX(),
window1.getaircraftlist().get(i).getY(), this);
}
else {
if (((window1.getaircraftlist().get(i).getDestFixPointName()=="IAF")||(window1.getaircraftlist().get(i).getDestFixPointName()=="AIRPORT")) && k[i]==0){
g.drawImage(window1.getaircraftlist().get(i).getImage(), window1.getaircraftlist().get(i).getX(),
window1.getaircraftlist().get(i).getY(), this);
ov.setValue(window1.getaircraftlist().get(i).name+","+window1.getaircraftlist().get(i).getDestFixPointName());
k[i]=1;
}
else if (((window1.getaircraftlist().get(i).getDestFixPointName()=="IAF")||(window1.getaircraftlist().get(i).getDestFixPointName()=="AIRPORT")) & k[i]==1){
g.drawImage(window1.getaircraftlist().get(i).getImage(), window1.getaircraftlist().get(i).getX(),
window1.getaircraftlist().get(i).getY(), this);
}
else
{
ov.setValue(window1.getaircraftlist().get(i).name+","+window1.getaircraftlist().get(i).getDestFixPointName());
window1.getaircraftlist().remove(i);}
}
}
【问题讨论】:
标签: java observer-pattern observable