- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 MVC 模式通过 SWT 构建一个简单的 Java 应用程序。我希望能够在后台发生某些事情时自动更新 View ,因此我尝试使用 Observer/Observable 模式,但看起来当我的 Observable 更改时我的 Observer 从未收到通知。
代码如下:
Launcher.java(主类)
public class Launcher {
public static void main(String[] args) {
Application app = new Application();
PenguinView v = new PenguinView(app);
Controller api = new Controller(app, v);
}
}
Application.java(后台应用)
public class Application {
private Penguin _myPenguin;
public Application() {
_myPenguin = new Penguin();
new Thread(_myPenguin).start();
}
public Penguin getPenguin() {
return _myPenguin;
}
}
Penguin.java(模型,我的 Observable)
public class Penguin extends Observable implements Runnable {
private String _color;
private boolean _isHappy;
private int _myRocks;
public Penguin() {
_color = "Black";
_isHappy = true;
_myRocks = 0;
}
public void paint(String color) {
_color = color;
System.out.println("My new color is " + _color);
setChanged();
notifyObservers();
}
public String getColor() {
return _color;
}
public void upset() {
_isHappy = false;
setChanged();
notifyObservers();
}
public void cheerUp() {
_isHappy = true;
setChanged();
notifyObservers();
}
public boolean isHappy() {
return _isHappy;
}
public void run() {
// Penguin starts walking and find rocks!
while(true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
iFoundRock();
}
}
private void iFoundRock() {
_myRocks++;
System.out.println("I now have " + _myRocks + " rocks");
setChanged();
notifyObservers();
}
}
PenguinView.java(SWT View ,我的观察者)
public class PenguinView implements Observer {
private Application _app;
private Display _d;
private Shell _s;
private Label _penguinColor;
private Label _penguinHumor;
private Label _penguinRocks;
private Button _upset;
private Button _cheerUp;
private Text _newColor;
private Button _paint;
public PenguinView(Application app) {
_app = app;
_d = new Display();
_s = new Shell();
RowLayout rl = new RowLayout();
rl.marginWidth = 12;
rl.marginHeight = 12;
_s.setLayout(rl);
new Label(_s, SWT.BORDER).setText("Penguin Color: ");
_penguinColor = new Label(_s, SWT.BORDER);
_penguinColor.setText(_app.getPenguin().getColor());
new Label(_s, SWT.BORDER).setText(" Humor: ");
_penguinHumor = new Label(_s, SWT.BORDER);
String humor = _app.getPenguin().isHappy() ? "Happy" : "Sad";
_penguinHumor.setText(humor);
new Label(_s, SWT.BORDER).setText(" Rocks: ");
_penguinRocks = new Label(_s, SWT.BORDER);
_penguinRocks.setText(String.valueOf(_app.getPenguin().howManyRocks()));
_upset = new Button(_s, SWT.PUSH);
_upset.setText(":(");
_upset.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event e) {
_penguinHumor.setText("Sad");
}
});
_cheerUp = new Button(_s, SWT.PUSH);
_cheerUp.setText(":)");
_cheerUp.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event e) {
_penguinHumor.setText("Happy");
}
});
_newColor = new Text(_s, SWT.BORDER);
_paint = new Button(_s, SWT.PUSH);
_paint.setText("Paint!");
_paint.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event e) {
//_penguinColor.setText(_newColor.getText());
_app.getPenguin().paint(_newColor.getText());
}
});
_s.pack();
_s.open();
while (!_d.isDisposed()) {
if (!_d.readAndDispatch()) {
_d.sleep();
}
}
}
public void update(Observable obs, Object obj) {
System.out.println("I go here!");
_penguinRocks.setText(String.valueOf(((Penguin)obs).howManyRocks()));
if(obs.equals(_app.getPenguin())) {
_penguinRocks.setText(String.valueOf(((Penguin)obs).howManyRocks()));
}
}
public void update(Observable obs) {
System.out.println("I go there!");
}
Controller.java( Controller )
public class Controller {
Application _app;
PenguinView _v;
public Controller(Application app, PenguinView v) {
_app = app;
_v = v;
// Set observer
_app.getPenguin().addObserver(_v);
}
}
输出:
I now have 1 rocks
I now have 2 rocks
My new color is Blue
I now have 3 rocks
I now have 4 rocks
你知道我做错了什么吗?
谢谢!
最佳答案
据我所知,PenguinView 构造函数中的 while 循环阻塞了您的主线程,因此 Controller 永远不会被实例化并且永远不会添加观察者。
关于Java Observer/Observable 模式不通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6537532/
一段时间后,我阅读了有关 RxJava concat 的内容,并决定测试一下我的理解力。但是我遇到了一些我不太理解的行为。 问题是,当我连接两个可观察对象时,根据我将它们传递给 Observable.
我正在使用来自数据库服务的数据实现自动完成: @Injectable() export class SchoolService { constructor(private db: AngularF
我正在尝试使用 RxJS 创建一个可观察的对象,它可以执行如图所示的操作。 获取一个值并等待一段固定的时间才能获得 下一个。 下一个将是该周期内发出的最后一个值 等等,跳过其余部分。 如果等待时间间隔
我有一个可观察对象和另一个提供的可观察对象改变 key 。我想构建一个在之间切换的可观察对象基于该键的对象中的可观察值。 示例: // Choose randomly between "up" or
我使用 protobuffers 在我的前端和我的 Dart 服务器之间进行通信。 那些对象没有实现 Observable . 我的 Dart 聚合物对象看起来像: @CustomTag('user-
在 java swing 项目中,我有一个模型类,它保存某个 JPanel 的状态。我需要使这些数据可供 View 使用。我认为有两种选择。有一个扩展 Observable 的类并将模型作为实例变量。
我想找到一种方法来检测观察者是否已完成使用我使用 Rx.Observable.create 创建的自定义可观察对象,以便自定义可观察对象可以结束它并正确地进行一些清理。 因此,我创建了一些测试代码,如
我正在尝试查询数据库。迭代结果列表,并为每一项再执行一个请求。在 rxjs 构建结束时,我有 Observable[]> 。但我需要Observable 。如何做到这一点? this.caseServ
我希望我的 api 上有一个方法返回 Observable> 但我希望该方法中的代码知道所有包含的 Observables 是否已完成,以便它可以关闭某些内容。最好的方法是什么? 更明确地说,我希望完
我有两个方法返回 Observable> firstAPI.getFirstInfo("1", "2"); secondApi.getSecondInfo(resultFirstObservable,
我有一个 Observable返回单个 Cursor实例(Observable)。我正在尝试利用 ContentObservable.fromCursor获取 onNext 中每个游标的行回调。 我想
我有两种返回 Observable 的方法: Observable firstObservable(); Observable secondObservable(String value); 对于第一
我正在尝试创建一个将用户数据作为 Observable 的函数,并使用来自第一个 observable 的数据从查询中添加/合并数据,然后将所有这些数据作为一个 observable 返回,我可以这样
我有一个 spec-compliant ECMAScript Observable ,具体来自 wonka library .我正在尝试将这种类型的 observable 转换为 rxjs 6 obs
为了简化问题,我在这里使用了数字和字符串。代码: const numbers$:Observable = of([1,2,3]); const strings: string[] = ["a","b"
对于我的 Android 应用程序,我需要一个 Observable 来聚合来自 7 个不同搜索的结果并作为一个集合发出。 对于最终发射,我选择了 ListMultimap其中 Content是搜索结
我正在使用改造 2.0.0-beta2 并且调试构建工作正常,但我在使用 Proguard 发布构建时遇到以下错误。 这是更新后的 logcat 错误。 11-17 18:23:22.751 1627
observer.throw(err) 和 observer.error(err) 有什么区别? 我正在使用 RxJS 版本“5.0.0-beta.12” var innerObservable =
我们有一种情况,对服务的方法调用返回一个 IObservable但我们的客户期望 IObservable .将 T1 转换为 T2 很简单。 Rx 中有什么允许这样做的吗? (即链接观察者) 我知道我
我陷入了如何将以下可观察类型转换/转换为我的目标类型的困境: 我有可观察的类型: Observable>> 我想将其转换为: Observable> 所以当我订阅它时,它会发出 List不是Obser
我是一名优秀的程序员,十分优秀!