使用Observer模式移动标签(JavaFx) [英] Using Observer pattern to move labels (JavaFx)

查看:199
本文介绍了使用Observer模式移动标签(JavaFx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个聊天程序,我的聊天人员是一个标签。当用户点击 anchorpane 时,标签可以在屏幕上移动现在这里有两个senerios:

I am creating a chat program where my chat persons are a label. the labels can be moved around on the screen when the user clicks on the anchorpane now there are two senerios here:


  1. 聊天员必须在本地移动。

  1. The chatperson has to locally move.

客户端必须将此移动发送给所有其他连接的客户端。

the client has to send this movement to all the other connected clients.

第二种情况很容易,如果对象的工作原理应该是我们当前的 ChatPerson 对象如下所示:

The 2nd case is easy enough if the objects works as they should currently my ChatPerson object looks like this:

 package GUI;


public class ChatPerson {

    private String username;
    private int x;
    private int y;
    // Brugerens ID i databasen
    private int id;

    public ChatPerson(String name){
        this.username = name;
    }

    public String getUserName(){
        return username;
    }
    public void setX(int x){
        this.x = x;
    }
    public void setY(int y){
        this.y = y;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id = id;
    }



}

我的问题是我将如何实现这种行为。我已经查找了Observer模式,但我发现在这种情况下如何使它工作很难?

My question is how would i implement this sort of behavior. ive looked up the Observer pattern but i am finding it hard on how to put it to work in this case?

另外,JavaFx是否有某种实现方式我可以在这里使用?我看过Observablelist,但是我真的不知道如何帮助我?

Also, do JavaFx have some sort of implementation that i could use here? Ive looked at Observablelist but i cant really get my mind through how that would help me?

推荐答案

你可以使用观察者模式这个案例。我假设你在每个客户端的某个地方都有一个连接人员列表。如果这是一些,通知其他人移动事件应该很简单。简单地使ChatPerson可观察如此

You can use the observer pattern in this case. I assume you have a list of connected people somewhere on each client. if this is some it should be quite simple to notify the other people of the move event. Simply make the ChatPerson observable like so

public class ChatPerson {
     //your props here :P...
    private final List<MoveListener> listeners = new ArrayList<MoveListener>();

    private void notifyListeners(MoveEvent e){
        for(MoveListener l : listeners){
             l.onMoveEvent(e);
        }
    }
    public void addMoveListener(MoveListener l){
        this.listeners.add(l);
    }
    public void removeMoveListener(MoveListener l){
        this.listeners.remove(l);
    }

    //i would create a move method but you can do this on setX() and setY()
    public void move(int x,int y){
        this.x=x;
        this.y=y;
        this.notifyListeners(new MoveEvent(this,x,y));
    }
    //your other method...
}

现在用于MoveListener接口。

Now for the MoveListener Interface.

public interface MoveListener{
    public void onMoveEvent(MoveEvent e);
}

和MoveEvent。

And the MoveEvent.

public class MoveEvent{
    public final ChatPerson source;//i could be more generic but you get the ideea
    public final int currentX;
    public final int currentY;
    public MoveEvent(ChatPerson source, int x,int y){
        this.source = source;
        this.currentX = x;
        this.currentY = y;
    }
    //you can make the fields private and getters ofc :P
}

现在每当ChatPerson移动时,它都会以一种漂亮而通用的方式广播它的位置,它会响应这个事件向每个监听器发送它的内容。

在容器类中(具有连接人员列表的那个)只需实现一个MoveListener并将其添加到当前的ChatPerson。

在此实现中,您可以遍历连接人员列表并通过线路发送当前位置 可以这么说。如果没有关于你的应用程序如何实现的更多细节我真的不能给出更好的答案。

希望它有所帮助。

Now whenever the ChatPerson moves, it broadcast its position in a nice and generic way, its up to each listener to its stuff in response to this event.
In the container class (the one with the list of connected people) simply implement a MoveListener and add it to the current ChatPerson.
In this implementation you can iterate over the list of connected people and send the current position "over the wire" so to speak. Without more detail on how your app is implemented I can't really give a better answer.
Hope it this helps.

这篇关于使用Observer模式移动标签(JavaFx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆