(九)网络聊天程序

博客展示了一个带界面的聊天程序demo,属于信息技术领域,为相关开发提供示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

demo:带界面的聊天程序

public class ChatFrame extends JFrame implements ActionListener {
    JTextField tf;
    JTextArea ta;
    JScrollPane sp;
    JButton send;
    JPanel P;
    int port;
    String s="";
    String myID;
    Date date;
    ServerSocket server;
    Socket mySocket;
    BufferedReader is;
    PrintWriter os;
    String line;
    public ChatFrame(String ID, String remotelID, String IP, int port, boolean isServer, String remoteID){
        super(ID);
        myID=ID;
        this.port=port;
        ta=new JTextArea();
        ta.setEditable(false);
        sp=new JScrollPane(ta);
        this.setSize(330,400);
        this.setResizable(false);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch (Exception e){
            System.out.println("UI error");

        }
        this.getContentPane().add(sp,"Center");
        P=new JPanel();
        this.getContentPane().add(P,"South");
        send = new JButton("发送");
        tf=new JTextField(20);
        P.add(tf);
        P.add(send);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        send.addActionListener(this);
        tf.addActionListener(this);
        if (isServer){
            try {
                server=null;
                try {
                    server=new ServerSocket(port);
                }catch (Exception e){
                    System.out.println("can not listen to:"+e);
                }
                mySocket=null;
                try {
                    mySocket=server.accept();
                }catch (Exception e){
                    System.out.println("Error."+e);
                }
                is=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
                os=new PrintWriter(mySocket.getOutputStream());
            }catch (Exception e){
                System.out.println("Error:in server client socket"+e);
            }
        }//end of if
        else{
            try {
                mySocket=new Socket(IP,port);
                os=new PrintWriter(mySocket.getOutputStream());
                is=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
            }catch (Exception e){
                System.out.println("Error:in client socket"+e);
            }
            while (true){
                try {
                    line=is.readLine();
                    date=new Date();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String currentTime=formatter.format(date);
                    s+=currentTime+""+remoteID+"说:\n"+line+"\n";
                    ta.setText(s);
                }catch (Exception e){
                    System.out.println("Error:in receive remote information"+e);
                }
            }
        }
    }

    public ChatFrame(String cat, String dog, String s, int i, boolean b) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime=formatter.format(date);
        s+=currentTime+""+myID+"说:\n"+tf.getText()+"\n";
        ta.setText(s);
        os.print(tf.getText());
        os.flush();
        tf.setText("");
        tf.requestFocus();
    }
}
public class ChatServerFrame {
    public static void main(String[] args) {
        ChatFrame cserver = new ChatFrame("Cat", "Dog", "127.0.0.1", 2019, true);
    }
}
public class ChatClientFrame {
    public static void main(String[] args) {
        ChatFrame cclient = new ChatFrame("Dog", "Cat", "127.0.0.1", 2019, false);
    }
}

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值