Java 刷新所有帧中的用户列表

Java 刷新所有帧中的用户列表,java,swing,Java,Swing,我试着聊天。我的问题是,当其他用户输入时,如何刷新用户列表,以便用户列表在所有打开的帧中都可见 import java.io.*; public class MulticastChat implements Runnable, WindowListener, ActionListener { public InetAddress groupp; public int port; public MulticastChat (InetAddress group, int

我试着聊天。我的问题是,当其他用户输入时,如何刷新用户列表,以便用户列表在所有打开的帧中都可见

import java.io.*;

public class MulticastChat implements Runnable, WindowListener, ActionListener {
    public InetAddress groupp;
    public int port;

    public MulticastChat (InetAddress group, int port) throws IOException {
    this.groupp = group;
    this.port = port;
    start();
    }
    public String [] user;
    public Frame frame;
    public TextArea output;
    public TextField input;
    public Button button;
    public Panel panel;
    public Panel p;
    public List list=new List(8);
    public TextField tf;

    public Thread listener;



    protected MulticastSocket socket;
    protected DatagramPacket outgoing, incoming;

    protected void initNet () throws IOException {
    socket = new MulticastSocket (port);
    socket.setTimeToLive (5);
    socket.joinGroup (groupp);
    outgoing = new DatagramPacket (new byte[1], 1, groupp, port);
    incoming = new DatagramPacket (new byte[65508], 65508);

    }
    public synchronized void start () throws IOException {
    if (listener == null) {
        initAWT ();
        initNet ();
        listener = new Thread (this);
        listener.start ();
        frame.setVisible (true);

        list.add(frame.getTitle());
    }
    }

    public void initAWT () {
    frame = new Frame
    ();
    frame.addWindowListener (this);

    tf=new TextField(10);
    output = new TextArea ();
    output.setEditable (false);
    button=new Button("Login");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("Login")) {
            try {
            if(!tf.getText().isEmpty()){

                byte[] utf = (tf.getText()+" Joined").getBytes ("UTF8");
                frame.setTitle(tf.getText());
                outgoing.setData (utf);
                outgoing.setLength (utf.length);
                socket.send (outgoing);
                input.setText ("");
                list.addItem(tf.getText());
                tf.setVisible(false);
                button.setVisible(false);
            }else{
                JOptionPane.showMessageDialog(frame, "Enter login name");
            } 
            } catch (IOException ex) {
                handleIOException (ex);
            }
        }
        }
    });
    panel=new Panel(new BorderLayout());
    p=new Panel(new BorderLayout());
    input = new TextField ();
    input.addActionListener (this);
    frame.setLayout (new BorderLayout ());
    panel.add(tf,"South");
    panel.add(list,"North");
    panel.add(button,"Center");
    frame.add(panel,"East");
    p.add (output, "Center");
    p.add (input, "South");
    frame.add(p,"West");
    frame.pack ();

    }


    public synchronized void stop () throws IOException {
    String s=frame.getTitle();
    frame.dispose();

    byte[] utf = (s+"Leaved").getBytes ("UTF8");
        outgoing.setData (utf);
        outgoing.setLength (utf.length);
        socket.send (outgoing);
    if (listener != null) {
        listener.interrupt ();
        listener = null;
        try {
        socket.leaveGroup (groupp);
        } finally {
        socket.close ();
        }
    }
    }

    public void windowOpened (WindowEvent event) {
    input.requestFocus ();
    }

    public void windowClosing (WindowEvent event) {
    try {

        stop ();

    } catch (IOException ex) {
        ex.printStackTrace ();
    }
    }

    public void windowClosed (WindowEvent event) {
    }
    public void windowIconified (WindowEvent event) {}
    public void windowDeiconified (WindowEvent event) {}
    public void windowActivated (WindowEvent event) {}
    public void windowDeactivated (WindowEvent event) {}

    public void actionPerformed (ActionEvent event) {
    try {
        byte[] utf = event.getActionCommand ().getBytes ("UTF8");
        outgoing.setData (utf);
        outgoing.setLength (utf.length);
        socket.send (outgoing);
        input.setText ("");


    } catch (IOException ex) {
        handleIOException (ex);
    }
    }

    protected synchronized void handleIOException (IOException ex) {
    if (listener != null) {
        output.append (ex + "\n");
        input.setVisible (false);
        frame.validate ();
        if (listener != Thread.currentThread ())
        listener.interrupt ();
        listener = null;
        try {
        socket.leaveGroup (groupp);
        } catch (IOException ignored) {
        }
        socket.close ();
    }
    }

    public void run () {

    try {

        while (!Thread.interrupted ()) {
        incoming.setLength (incoming.getData ().length);
        socket.receive (incoming);
        String message = new String
        (incoming.getData (), 0, incoming.getLength (), "UTF8");
        output.append (incoming.getAddress().toString()+frame.getTitle()+message + "\n");
        }
    } catch (IOException ex) {
        handleIOException (ex);
    }

    }
    public void refresh(){

    }

    public static void main (String[] args) throws IOException {

    InetAddress group;

        group = InetAddress.getByName("235.235.235.235");

    int port = 2999;
    new Thread(new MulticastChat(group, port));
    }
}
  • 按特定时间间隔更新列表。(轮询机制)
  • 从客户端发送一些数据(散列),并在服务器上检查是否有需要更新的返回数据包和一些指令,以便客户端刷新列表
      • 按特定时间间隔更新列表。(轮询机制)
      • 从客户端发送一些数据(散列),并在服务器上检查是否有需要更新的返回数据包和一些指令,以便客户端刷新列表

      我知道这个答案没有那么多描述性,但请尝试创建一个事件处理程序,以控制新用户何时进入聊天室。就像:

      public void newUserIn (UserEvent event) {}
      

      或者…

      我知道这个答案没有那么多描述性,但是试着让它创建一个事件处理程序,只是为了控制新用户何时进入聊天室。就像:

      public void newUserIn (UserEvent event) {}
      

      或者…

      首先,我建议您将代码分成若干类,以便更易于阅读和维护。为您的网络IO创建一个类,另一个类为窗口类。用监听器接口将两者链接起来

      其次,您需要创建一条多播消息,指示用户已加入。这可能类似于“[NEW-USER:xxxxxx]”

      然后,您需要检查客户端中的传入消息,以识别“新用户”消息。然后可以在用户界面中显示该用户


      您还应该防止用户欺骗您的新用户消息。如果他们键入了与新用户模式匹配的内容,请将其转义,以免被误解。

      首先,我建议您将代码分成若干类,以便更易于阅读和维护。为您的网络IO创建一个类,另一个类为窗口类。用监听器接口将两者链接起来

      其次,您需要创建一条多播消息,指示用户已加入。这可能类似于“[NEW-USER:xxxxxx]”

      然后,您需要检查客户端中的传入消息,以识别“新用户”消息。然后可以在用户界面中显示该用户

      您还应该防止用户欺骗您的新用户消息。如果他们键入了与新用户模式匹配的内容,请将其转义,以免误解