试图在我的java程序中实现CardLayout

时间:2017-05-01 23:32:26

标签: java client-server names cardlayout prepend

我是java的新手,我在尝试实现使用卡片布局从用户获取名称和在我的Java客户端/服务器程序中将名称前置名称添加到消息时遇到了问题。

仅供参考:括号全部位于正确的位置,只需看一下就可以了。

这是我的ChatHandler代码:

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatHandler extends Thread{
Socket s;
BufferedReader br;
PrintWriter pw;
String temp;
ArrayList <ChatHandler>handlers;

public ChatHandler(Socket s, ArrayList <ChatHandler>handlers){
    this.s = s;
    this.handlers = handlers;
}


public void run(){
    try{
        handlers.add(this);
        br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        pw = new PrintWriter(s.getOutputStream(), true);
        temp = "";
        while((temp = br.readLine()) != null){
            for (ChatHandler ch : handlers){
                ch.pw.println(temp); 
            }
            System.out.println(temp);
        }
    }catch(IOException ioe){
        System.out.println(ioe.getMessage());      
    }finally{
        handlers.remove(this);
    }
}
}

这是我的ChatFrame代码:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatFrame extends Frame{
public ChatFrame(){
    setSize(500,500);
    setTitle("Echo Client");
    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent we){
            System.exit(0);
        }
    });
    ChatPanel cp = new ChatPanel();
    add(cp, BorderLayout.CENTER);
    setVisible(true);   
}
public static void main(String[] args){
    ChatFrame cf = new ChatFrame();
}
}
class ChatPanel extends Panel implements ActionListener, Runnable{
TextField tf;
TextArea ta;
Socket s;
PrintWriter send;
BufferedReader rec;
Thread t;
Button connect, disconnect;

public ChatPanel(){
    setLayout(new BorderLayout());
    tf = new TextField();
    tf.addActionListener(this);
    add(tf, BorderLayout.NORTH);
    ta = new TextArea();
    add(ta, BorderLayout.CENTER);
    connect = new Button("Connect");
    connect.addActionListener(this);
    disconnect = new Button("Disconnect");
    disconnect.addActionListener(this);
    disconnect.setEnabled(false);
    Panel p = new Panel();
    p.add(connect);
    p.add(disconnect);
    add(p, BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==tf){
        String temp = tf.getText();
        send.println(temp);
        tf.setText("");
    }else if(ae.getSource() == connect){
        try{
            s = new Socket("localhost", 3000); 
            send = new PrintWriter(s.getOutputStream(), true);
            rec = new BufferedReader(new InputStreamReader(
                                        s.getInputStream()));

        }catch(UnknownHostException uhe){
            System.out.println(uhe.getMessage());
        }catch(IOException ioe){
            System.out.println(ioe.getMessage());
        }
        t = new Thread(this, "whatever");
        t.start();
        connect.setEnabled(false);
        disconnect.setEnabled(true);

    }else if(ae.getSource() == disconnect){
        send.println("BYE");
        send = null;
        rec = null;
        s = null;
        t = null;

        disconnect.setEnabled(false);
        connect.setEnabled(true);
    }
}
public void run(){
    String temp = "";
    try{
        while(((temp = rec.readLine()) != null) && (t != null)){
            ta.append(temp + "\n");
        }
    }catch(IOException ioe){
        System.out.println(ioe.getMessage());
    }

    }

}

这是ChatServer代码:

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer{
Socket s;
ArrayList <ChatHandler>handlers;
public ChatServer(){
    try{
        ServerSocket ss = new ServerSocket(3000);
        handlers = new ArrayList<ChatHandler>();
        for(;;){
            s = ss.accept();
            new ChatHandler(s, handlers).start();
        }
    }catch(IOException ioe){
        System.out.println(ioe.getMessage());
    }
}
public static void main(String[] args){
    ChatServer tes = new ChatServer(); 
}
}

0 个答案:

没有答案