Java 消息仅在1/2的时间内显示给客户端?

Java 消息仅在1/2的时间内显示给客户端?,java,swing,client-server,client,jtextpane,Java,Swing,Client Server,Client,Jtextpane,当我向服务器发送一条消息时,它会100%地检索它,但我的客户机只会每隔一条消息显示一次。这对我来说真的毫无意义。。我曾经使用JTextAreas,但我决定切换到JTextPanes,是的,进展不太顺利 在我的ClientFrame.java中,我有displayMessage(stringmessage),这是在我的客户机的run()中调用的。因为我以前没有发布足够的代码,所以我会发布我所有的。。使用的主要类有ClientFrame、Client、Server(用于sendGlobalMessa

当我向服务器发送一条消息时,它会100%地检索它,但我的客户机只会每隔一条消息显示一次。这对我来说真的毫无意义。。我曾经使用JTextAreas,但我决定切换到JTextPanes,是的,进展不太顺利

在我的ClientFrame.java中,我有
displayMessage(stringmessage)
,这是在我的客户机的
run()
中调用的。因为我以前没有发布足够的代码,所以我会发布我所有的。。使用的主要类有ClientFrame、Client、Server(用于
sendGlobalMessage(字符串消息)
)和User(用于我的服务器线程)

Client.java

import java.io.IOException;

import javax.swing.text.BadLocationException;


public class Client extends Stream implements Runnable {
    public boolean running = false;
    private Thread clientThread;

    ClientFrame frame;
    public Client() {
        super("localhost", 43594);

        frame = new ClientFrame(500, 500);
        start();
    }

    public synchronized void start() {
        if(running) return;
        running = true;

        clientThread = new Thread(this);
        clientThread.start();
    }
    public synchronized void stop() {
        if(!running) return;
        running = false;

        clientThread.interrupt();
        try {
            clientThread.join();
        } catch (InterruptedException e) {e.printStackTrace();}
    }

    public static void sendMessage(String message) {
        try{
            out.writeObject(message);
            out.flush();
        }catch(IOException e) { }
    }

    public void run() {
        try{
        setupStream();

        while(running) {
            if(recieveData() != null) {
                frame.displayMessage(recieveData());
            }
        }
        }catch(IOException | BadLocationException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try{
            out.close();
            in.close();
            socket.close();
            clientThread.join();
            }catch(IOException | InterruptedException e) { e.printStackTrace(); }
        }
    }
    public static void main(String[] args) {
        new Client();
    }
}
客户端的Stream.java

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public class Stream {

    Socket socket;
    static ObjectOutputStream out;
    static ObjectInputStream in;
    String data;

    public Stream(String host, int port) {
        try {
            socket = new Socket(host, port);
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }

    protected void setupStream() throws IOException {
        out = new ObjectOutputStream(socket.getOutputStream());
        out.flush();
        in = new ObjectInputStream(socket.getInputStream());
    }

    public synchronized String recieveData() throws IOException, ClassNotFoundException {
        return (String) in.readObject();
    }

}
ClientFrame.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;


public class ClientFrame extends Frame {

    private JPanel mainpanel;

    public ClientFrame(int width, int height) {
        super(width, height);

        mainpanel = new JPanel();
        addContentToPanel();
        setListeners();

        add(mainpanel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void addContentToPanel() {
        mainpanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        setGbc(0, 0, width, height-460, gbc);
        mainpanel.add(topPanel(), gbc);
        setGbc(0, 1, 0, 0, gbc);
        mainpanel.add(chatPanel(), gbc);

    }


    JTextField userfield;
    JPasswordField passfield;
    JPanel topPanel() {
        JPanel panel = new JPanel();
            panel.setLayout(null);

            JLabel label = new JLabel("Username: ");
                label.setBounds(15, 1, 65, 30);
            userfield = new JTextField();
                userfield.setBounds(85, 7, 160, 20);
                panel.add(label);
                panel.add(userfield);

            label = new JLabel("Password: ");
                label.setBounds(255, 1, 65, 30);
            passfield = new JPasswordField();
                passfield.setBounds(325, 7, 160, 20);
                panel.add(label);
                panel.add(passfield);

        return panel;
    }


    JTextPane toptextpane;
    JTextPane bottomtextpane;
    JScrollPane topscrollpane;
    JScrollPane bottomscrollpane;
    JPanel chatPanel() {
        JPanel chatpanel = new JPanel();
            chatpanel.setPreferredSize(new Dimension(width, height - 40));
            chatpanel.setLayout(null);

            toptextpane = new JTextPane();
                toptextpane.setEditable(false);
            topscrollpane = new JScrollPane(toptextpane);
                topscrollpane.setBounds(5, 0, width - 10, height - 200);
                chatpanel.add(topscrollpane);

            bottomtextpane = new JTextPane();
            bottomscrollpane = new JScrollPane(bottomtextpane);
                bottomscrollpane.setBounds(5, 305, width - 10, height - 350);
                chatpanel.add(bottomscrollpane);


        return chatpanel;   
    }

    Document doc;
    public void displayMessage(String message) throws BadLocationException {
        Document doc = toptextpane.getDocument();
        try
        {
            System.out.println("works");
            doc.insertString(0, message +"\n", null );
        }
        catch(Exception e) { System.out.println(e); }

    }

    private void setListeners() {
        bottomtextpane.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                    Client.sendMessage(bottomtextpane.getText());
                    bottomtextpane.setText("");
                    e.consume();
                }
            }
        });
    }
}
Server.java

package Server;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Server {
    public static int maxConnections = 10;

    private ServerSocket serverSocket;

    static User[] user = new User[maxConnections];

    public static void sendGlobalMessage(String message) {
        for(int i = 0; i < 9; i++) {
            if(user[i] != null) {
                try {
                    user[i].out.writeObject(message);
                    user[i].out.flush();
                    System.out.println(message);
                }catch(IOException e) { }
            }
        }
    }

    public static HashMap<String, User> users = new HashMap<String, User>();


    Thread acceptConnections = new Thread(new Runnable() {
        public void run() {
            while(User.users <= maxConnections) {
                try{
                    System.out.println("[System] Waiting for connections.. ("+User.users+" users online)");

                    for(int i = 0; i < 9; i++) {
                        if(user[i] == null) {
                            user[i] = new User(serverSocket.accept());
                            user[i].ID = i;
                            System.out.println("--- Someone has joined! ---");
                            user[i].start();
                            User.users++;
                            break ;
                        } 
                    }
                }catch(IOException e) { }
            }
        }
    });
    Thread scannerThread;
    Scanner scanner;

    public Server() { 
        scannerThread = new Thread(new Runnable() {
            public void run() {
                scanner = new Scanner(System.in);
                String input;
                while((input = scanner.nextLine()) != null) {
                    switch(input.toLowerCase()) {
                    case "checkuser":

                    }
                }
            }
        });

        try{
            scannerThread.start();
            serverSocket = new ServerSocket(43594);
        }catch(IOException e) { }

    }

    public static void main(String args[]) throws Exception {
        Server server = new Server();

        server.acceptConnections.start();
    }
}
包服务器;
导入java.io.IOException;
导入java.net.ServerSocket;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Scanner;
公共类服务器{
公共静态int maxConnections=10;
私有服务器套接字服务器套接字;
静态用户[]用户=新用户[maxConnections];
公共静态void sendGlobalMessage(字符串消息){
对于(int i=0;i<9;i++){
if(用户[i]!=null){
试一试{
用户[i].out.writeObject(消息);
用户[i].out.flush();
System.out.println(消息);
}捕获(IOE){}
}
}
}
public static HashMap users=new HashMap();
Thread acceptConnections=新线程(new Runnable(){
公开募捐{

虽然(User.users首先,您正在EDT之外对UI进行更改,这本身是危险的,但可能不是当前的问题。还有一些好信息需要了解。同步是否会使这更安全?不,您需要使用
SwingUtilities.invokeLater
将更新调用放入EDT或
SwingWor的上下文中ker
。有关详细信息,请查看
package Server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class User extends Thread {
    public static int users = 0;
    public int ID;
    public String username;
    boolean online = false;

    public static ArrayList<String> usernames = new ArrayList<String>();

    Socket socket;

    ObjectOutputStream out;
    ObjectInputStream in;
    String input;

    public User(Socket socket) {
        this.socket = socket;

    }

    public String decode(String input) {
        String[] split = input.split(" ");

        if(input.startsWith("::")) {
            try {
                switch(split[0].substring(2, split[0].length()).toLowerCase()) {
                case "setname": 
                case "changename":
                case "newname":
                    if(usernames.contains(split[1].toLowerCase())) {
                        out.writeUTF("This name is already taken! Please choose a different one.");
                        out.flush();
                        return null;
                    }
                    if(username == null) {
                        username = split[1].substring(0, 1).toUpperCase() + split[1].substring(1, split[1].length());
                        Server.users.put(split[1].toLowerCase(), Server.user[ID]);
                        usernames.add(split[1].toLowerCase());
                    } else {
                        usernames.remove(username.toLowerCase());
                        username = split[1].substring(0, 1).toUpperCase() + split[1].substring(1, split[1].length());
                        usernames.add(split[1].toLowerCase());
                    }
                        return null;
                case "rank+":
                    return null;
                case "[sm]=":
                    return null;
                }
            }catch(IOException e) { }
        }
        return input;
    }

    String timeStamp;
    public void run() {
        try {

            out = new ObjectOutputStream(socket.getOutputStream());
            out.flush();
            in = new ObjectInputStream(socket.getInputStream());

            while((input = (String) in.readObject()) != null) {
                input = decode(input);

                if(input != null) {
                    if(username != null) {
                        timeStamp = new SimpleDateFormat("[h:mm:ss] ").format(Calendar.getInstance().getTime());
                        Server.sendGlobalMessage(timeStamp + username +": "+input);
                    } else {
                        timeStamp = new SimpleDateFormat("[h:mm:ss] ").format(Calendar.getInstance().getTime());
                        Server.sendGlobalMessage(timeStamp + "Guest "+ID+": "+input);
                    }
                }
            }

        }catch(IOException | ClassNotFoundException e) { try{
            out.close();
            in.close();
            socket.close();
        } catch(IOException e2) { e.printStackTrace(); } } finally { 

        }

    }

}