所以我们的老师试图为我们的聊天服务器/客户端分配提供帮助和编写GUI,但我无法弄清楚如何从GUI读取输入。无论我输入什么,只要坐在那里直到我输入一些东西。我无法弄清楚如何阅读它。在这一天的大部分时间里,我一直被困在这一部分而且我放弃了。我会发布课程,而且我不想在代码中询问问题的答案,我只需要能够在文本框中输入任何能够阅读的内容!我可以把剩下的东西拿出来!我认为连接部分是最难的,我没想到输入是一个问题:/
服务器类
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();
}
}
}
我觉得这很简单,也许我只是在看这个太难或太长。很高兴收到任何帮助或批评。可以为聊天窗口类修改任何类,除外:/
答案 0 :(得分:2)
你必须实现sendMessage函数,就像TODO消息说你可以使用类OutputStream
,在这种情况下,是PrintWriter
实例,如下所示:
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
通过这种方式,您将实现通过客户端发送消息。