Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不能附加到JTextArea_Java_Swing_Jtextarea - Fatal编程技术网

Java 不能附加到JTextArea

Java 不能附加到JTextArea,java,swing,jtextarea,Java,Swing,Jtextarea,我正在尝试用Java创建一个文本聊天。我有一个服务器和一个客户端,它们使用流相互连接,并使用objectInputStream和objectOutputStream发送数据 我有客户端和服务器的GUI。 我使用intellij的GUI表单制作了这些GUI 我遇到的问题是,当我试图向服务器的GUI显示文本时。如果我从JTextField actionlistener调用relayToAll方法,那么我可以将消息附加到GUi,然后将消息发送到所有客户端并在servers GUi中打印出来 如果我尝

我正在尝试用Java创建一个文本聊天。我有一个服务器和一个客户端,它们使用流相互连接,并使用objectInputStream和objectOutputStream发送数据

我有客户端和服务器的GUI。 我使用intellij的GUI表单制作了这些GUI

我遇到的问题是,当我试图向服务器的GUI显示文本时。如果我从JTextField actionlistener调用relayToAll方法,那么我可以将消息附加到GUi,然后将消息发送到所有客户端并在servers GUi中打印出来

如果我尝试从接收输入的位置调用相同的方法,则“附加到文本”区域不起作用

有谁能告诉我为什么它没有附加

谢谢

public class ServerTest {
private JTextField textField1;
private JTextArea textArea1;
private JPanel Panel;
static private ObjectOutputStream objectOutputStream;
static private ObjectInputStream objectInputStream;
static private Socket client;
static private ArrayList<Socket> clients = new ArrayList<Socket>();
static private ArrayList<ObjectOutputStream> objectOutputStreams = new ArrayList<>();

public void relayToAll(String message){
    try {
        for(int i = 0; i < clients.size(); i++) {
            ObjectOutputStream output = objectOutputStreams.get(i);
            output.writeObject(message);
            output.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    appendTextArea(message);
}

public void appendTextArea(String text){
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            System.out.println("This should go to the Server GUI: " + text);
            textArea1.append(text + "\n");
        }
    });
}

public ServerTest() {

    textField1.addActionListener(e -> {
        System.out.println(e.getActionCommand());
        relayToAll(e.getActionCommand());
        textField1.setText("");
    });
}

public void ReadInput(ObjectInputStream input, int port){
    try {
        String oldMessage = "";
        while (true) {
            String message = (String) input.readObject();
            if (message != oldMessage){
                System.out.println(port + ": " + message);
                oldMessage = message;
                relayToAll(port + ": " + message);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}


public void IOSetup(){
    try {
        ServerSocket serverSocket = new ServerSocket( 6969 );

        ExecutorService executor = Executors.newFixedThreadPool(5);

        System.out.println("server on\n");
        for (int i = 0; i < 5; i++){

            client = serverSocket.accept();
            clients.add(client);
            System.out.println("Connection from: "+ client.getPort());

            objectOutputStream = new ObjectOutputStream(client.getOutputStream());
            objectOutputStreams.add(objectOutputStream);

            objectInputStream = new ObjectInputStream(clients.get(i).getInputStream());


            executor.submit(() -> {
                ReadInput(objectInputStream, client.getPort());
            });
        }


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

}

public static void main(String[] args) {

    JFrame frame = new JFrame("Server");
    frame.setContentPane(new ServerTest().Panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    ServerTest application = new ServerTest();


    application.IOSetup();

}
公共类服务器测试{
私有JTextField textField1;
私人JTextArea TextArea 1;
私人JPanel小组;
静态私有ObjectOutputStream ObjectOutputStream;
静态私有ObjectInputStream ObjectInputStream;
静态私有套接字客户端;
静态私有ArrayList客户端=新建ArrayList();
静态私有ArrayList objectOutputStreams=新ArrayList();
public void relayToAll(字符串消息){
试一试{
对于(int i=0;i{
System.out.println(e.getActionCommand());
relayToAll(如getActionCommand());
textField1.setText(“”);
});
}
公共无效读取输入(ObjectInputStream输入,int端口){
试一试{
字符串oldMessage=“”;
while(true){
String message=(String)input.readObject();
如果(消息!=旧消息){
System.out.println(端口+“:”+消息);
oldMessage=消息;
relayToAll(端口+”:“+消息);
}
}
}捕获(IOE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}
}
公共void IOSetup(){
试一试{
ServerSocket ServerSocket=新的ServerSocket(6969);
ExecutorService executor=Executors.newFixedThreadPool(5);
System.out.println(“服务器开\n”);
对于(int i=0;i<5;i++){
client=serverSocket.accept();
客户。添加(客户);
System.out.println(“来自:+client.getPort()的连接”);
objectOutputStream=新的objectOutputStream(client.getOutputStream());
添加(objectOutputStream);
objectInputStream=新的objectInputStream(clients.get(i).getInputStream());
执行人提交(()->{
ReadInput(objectInputStream,client.getPort());
});
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
JFrame=新的JFrame(“服务器”);
frame.setContentPane(新的ServerTest().Panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
ServerTest应用程序=新的ServerTest();
application.IOSetup();
}

实际上你犯了一个愚蠢的错误。请检查下面的(a)和(B)行:

您看到问题了吗?您正在创建两个ServerTest对象,一个将其面板变量添加到JFrame并显示,另一个设置为IO通信。ActionListener更改显示的JTextArea的状态,而IO通信更改处于e第二个ServerTest实例,未显示的实例

一个改进是只创建一个实例:

public static void main(String[] args) {

    ServerTest application = new ServerTest();  // create one instance

    JFrame frame = new JFrame("Server");
    // frame.setContentPane(new ServerTest().Panel);

    frame.setContentPane(application.Panel);     // and use in both places

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    //ServerTest application = new ServerTest();
    application.IOSetup();                 // and use in both places
}
其他问题:

  • 您在后台线程中有长时间运行的代码长时间运行和阻塞,这可能是危险的,您的GUI没有被冻结的唯一原因是因为您在主线程和Swing事件线程上(错误地)启动GUI。有关此方面的详细信息,请阅读Swing并发:
  • 您需要学习和使用。变量名都应该以小写字母开头,而类名应该以大写字母开头。学习这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码

您在哪里初始化了JTextField、JTextArea和JPanel?
serverSocket.accept();
正在阻止代码,您正在后台线程中运行此操作。这可能是问题的根源。
public static void main(String[] args) {

    ServerTest application = new ServerTest();  // create one instance

    JFrame frame = new JFrame("Server");
    // frame.setContentPane(new ServerTest().Panel);

    frame.setContentPane(application.Panel);     // and use in both places

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    //ServerTest application = new ServerTest();
    application.IOSetup();                 // and use in both places
}