gpt4 book ai didi

java - 从聊天窗口读取输入

转载 作者:行者123 更新时间:2023-12-02 04:44:44 24 4
gpt4 key购买 nike

因此,我们的老师尝试帮助并为我们的聊天服务器/客户端作业编写了 GUI,但我不知道如何从 GUI 读取输入。我输入的任何内容都会保留在那里,直到我输入内容为止。我不知道如何阅读它。我一天中的大部分时间都停留在这部分上,我要放弃了。我将发布类(class),我不需要代码中提出的问题的答案,我只需要能够读取我在文本框中输入的任何内容!剩下的我可以想办法!我以为连接部分是最难的,没想到输入会成为问题:/

服务器类

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{

private static final int PORT = 8889;
private static ServerSocket server;
public static int connections;

public static void main(String[] args) throws IOException
{
server = new ServerSocket(PORT);

System.out.println("Waiting for clients to connect...");

Socket clientConnection;
while (true)
{
if (connections >=5)
{
clientConnection = server.accept();
System.out.println("No more connections can be made at this time.You will be disconnected.");
server.close();
}

else {
clientConnection = server.accept();
//CommandHandler handler = new CommandHandler(clientConnection);
System.out.println("Client connected.");
connections += 1;
System.out.println("There are " + connections + "user(s) at this time");
//Thread t = new Thread(handler);
//t.start();
}
}
}
}

客户端类

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ChatClient
{
public static final String HOST= "localhost";
public static final int PORT = 8889;

private ChatWindow window;
private Socket connection;


public ChatClient()
{
this.window = new ChatWindow(this);

try {
Socket serverConnection = null;
try {
serverConnection = new Socket(HOST, PORT);
ObjectInputStream reader = new ObjectInputStream(
serverConnection.getInputStream());
PrintWriter writer = new PrintWriter(
serverConnection.getOutputStream());

} finally {
serverConnection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}




public void sendMessage(String message)
{
//TODO - USE THE OUTPUT STREAM TO WRITE THE MESSAGE TO THE SERVER
// THIS METHOD IS INVOKED BY THE window WHEN THE USER ENTERS
// TEXT TO SEND TO THE CHAT SERVER


}


private void start()
{
//TODO - ADD CODE THAT WILL CONTINUOUSLY READ
// MESSAGES FROM THE SERVER
// USE THE window.printMessage() TO DISPLAY
// THE MESSAGE IN THE CHAT WINDOW
}




public static void main(String[] args)
{
ChatClient chatClient = new ChatClient();
chatClient.start();

}

}

聊天窗口类

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;

@SuppressWarnings("serial")
public class ChatWindow extends JFrame
{

private ChatClient client;

private JTextArea messageArea = new JTextArea(10, 32);
private JTextField inputArea = new JTextField(32);

public ChatWindow(ChatClient theClient)
{
this.client = theClient;

//when the user closes the window, make sure the user is logged out.
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try {
client.sendMessage("LOGOUT");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

// GUI COMPONENTS ------------------------------------------

// MESSAGE AREA: All messages from all clients are displayed here
messageArea.setEditable(false);
messageArea.setBackground(Color.LIGHT_GRAY);
messageArea.setLineWrap(true);

// make sure the scroll bar stays are the bottom
DefaultCaret caret = (DefaultCaret) messageArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

// INPUT AREA: Input area where client enters text message
// The message is sent when the user presses enter
inputArea.addActionListener(new InputAreaActionListener());

Container content = getContentPane();
content.add(new JScrollPane(messageArea), BorderLayout.CENTER);
content.add(inputArea, BorderLayout.SOUTH);

setTitle("Chat Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
inputArea.requestFocusInWindow();
setVisible(true);
}

/**
*
* This method adds the message to the end of the message area followed by a
* new line character.
*
* @param message
*/
public void printMessage(String message)
{
messageArea.append(String.format("%s\n", message));
try
{
messageArea.setCaretPosition(messageArea.getLineStartOffset(messageArea.getLineCount() - 1));
}
catch (BadLocationException e)
{
e.printStackTrace();
}
}


private class InputAreaActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();

// ask the chat client to send the message to the server
try {
client.sendMessage(command);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

inputArea.setText("");
inputArea.requestFocusInWindow();
}
}

}

我有一种感觉,这很简单,也许我只是看得太难或太久了。我们很乐意接受任何帮助或批评。除了聊天窗口类之外,任何类都可以修改:/

最佳答案

您必须实现 sendMessage 函数,就像 TODO 消息所说,您可以使用类 OutputStream,在本例中,它是 PrintWriter 实例,如下所示:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);

这样您就可以通过客户端发送消息了。

关于java - 从聊天窗口读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com