我一直试图让我现有的聊天课让多个用户看到这些消息,但我似乎不知道该怎么做。
public class user
{
JFrame chatWindow = null;
JButton sendButton = null;
JTextField message = null;
JTextArea chatArea = null;
JPanel chatArea_Container = null;
JPanel sendButton_Container = null;
JScrollPane Scroll = null;
Socket socket = null;
BufferedReader reader = null;
PrintWriter writer = null;
JButton login;
JTextField username;
JTextField password;
JPanel login_Container = null;
public user(){
userGUI();
}
public void userGUI(){
chatWindow= new JFrame ("Client");
username = new JTextField ("");
password = new JTextField ("");
login = new JButton ("login");
sendButton = new JButton ("Send");
message = new JTextField (4);
chatArea = new JTextArea (10,12);
Scroll = new JScrollPane(chatArea);
chatArea_Container = new JPanel();
chatArea_Container.setLayout (new GridLayout (1,1));
chatArea_Container.add(Scroll);
login_Container = new JPanel();
login_Container.setLayout(new GridLayout(1,3));
login_Container.add(username);
login_Container.add(password);
login_Container.add(login);
sendButton_Container = new JPanel();
sendButton_Container.setLayout(new GridLayout(1,2));
sendButton_Container.add(message);
sendButton_Container.add(sendButton);
chatWindow.setLayout(new BorderLayout());
chatWindow.add(chatArea_Container,BorderLayout.CENTER);
chatWindow.add(sendButton_Container,BorderLayout.SOUTH);
chatWindow.add(login_Container,BorderLayout.NORTH);
chatWindow.setSize(600, 350);
chatWindow.setVisible(true);
chatWindow.setResizable(false);
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread main = new Thread(new Runnable(){
public void run(){
try{
socket = new Socket ("localhost", 9000);
read();
write();
}
catch(Exception ex){
ex.printStackTrace();
}
}
});
main.start();
}
public void read(){
Thread read_Thread = new Thread (new Runnable(){
public void run(){
try{
reader = new BufferedReader (new InputStreamReader (socket.getInputStream()));
while (true){
String messageReceived = reader.readLine();
chatArea.append("Server says: "+messageReceived+"\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
});
read_Thread.start();
}
public void write(){
Thread writeThread = new Thread (new Runnable(){
public void run(){
try{
writer = new PrintWriter(socket.getOutputStream(),true);
sendButton.addActionListener(new ActionListener (){
public void actionPerformed (ActionEvent e){
String sending_message = message.getText();
writer.println(sending_message);
message.setText("");
}
});
}catch (Exception ex){
ex.printStackTrace();
}
}
});
writeThread.start();
}
public static void main(String[] args) {
new user();
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
}
我的服务器类与此类似,但从socket更改为serversocket。
{
JFrame chatWindow = null;
JButton sendButton = null;
JTextField message = null;
JTextArea chatArea = null;
JPanel chatArea_Container = null;
JPanel sendButton_Container = null;
JScrollPane Scroll = null;
ServerSocket server = null;
Socket socket = null;
BufferedReader reader = null;
PrintWriter writer = null;
JButton login;
JTextField username;
JTextField password;
JPanel login_Container = null;
public server(){
serverGUI();
}
public void serverGUI(){
chatWindow= new JFrame ("Server");
username = new JTextField ("");
password = new JTextField ("");
login = new JButton ("login");
login.addActionListener(this);
sendButton = new JButton ("Send");
message = new JTextField (4);
chatArea = new JTextArea (10,12);
Scroll = new JScrollPane(chatArea);
chatArea_Container = new JPanel();
chatArea_Container.setLayout (new GridLayout (1,1));
chatArea_Container.add(Scroll);
login_Container = new JPanel();
login_Container.setLayout(new GridLayout(1,3));
login_Container.add(username);
login_Container.add(password);
login_Container.add(login);
sendButton_Container = new JPanel();
sendButton_Container.setLayout(new GridLayout(1,2));
sendButton_Container.add(message);
sendButton_Container.add(sendButton);
chatWindow.setLayout(new BorderLayout());
chatWindow.add(chatArea_Container,BorderLayout.CENTER);
chatWindow.add(sendButton_Container,BorderLayout.SOUTH);
chatWindow.add(login_Container,BorderLayout.NORTH);
chatWindow.setSize(600, 350);
chatWindow.setVisible(true);
chatWindow.setResizable(false);
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread main = new Thread (new Runnable(){
public void run(){
try{
server = new ServerSocket(9000);
while(true){
socket = server.accept();
read();
write();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
});
main.start();
}
public void read(){
Thread read_Thread = new Thread (new Runnable(){
public void run(){
try{
reader = new BufferedReader (new InputStreamReader (socket.getInputStream()));
while (true){
String messageReceived = reader.readLine();
chatArea.append("User says: "+messageReceived+"\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
});
read_Thread.start();
}
public void write(){
Thread writeThread = new Thread (new Runnable(){
public void run(){
try{
writer = new PrintWriter(socket.getOutputStream(),true);
sendButton.addActionListener(new ActionListener (){
public void actionPerformed (ActionEvent e){
String sending_message = message.getText();
writer.println(sending_message);
message.setText("");
}
});
}catch (Exception ex){
ex.printStackTrace();
}
}
});
writeThread.start();
}
public static void main(String[] args) {
new server();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
我需要添加一种方法来让多个用户看到这些消息,但我不知道如何做到这一点。
你会如何解决这个问题?我在看这个:multiuser chat application on adhoc network in java
我是否基本上需要修改服务器类以便没有读写方法并只打开两个用户窗口?