资源描述:
《java聊天代码》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、服务器代码:WindowServer.javaimportjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.IOException;imp
2、ortjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JList;importjavax.swing.JOptionPane;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.border.TitledBorder
3、;publicclassWindowServer{publicstaticJFramewindow;publicstaticJTextAreatextMessage;//聊天记录publicstaticJListuser;//用户列表publicstaticintports;JButtonstart,send,exit;JTextFieldportServer,message,name;//主函数入口publicstaticvoidmain(String[]args){newWin
4、dowServer();}//初始化窗体publicWindowServer(){init();}//初始化内容publicvoidinit(){//采用绝对布局window=newJFrame("服务端");window.setLayout(null);window.setBounds(200,200,500,400);window.setResizable(false);//不可改变大小JLabellabel1=newJLabel("端口号:");label1.setBounds(10,8,5
5、0,30);window.add(label1);portServer=newJTextField();portServer.setBounds(60,8,100,30);portServer.setText("30000");window.add(portServer);JLabelnames=newJLabel("用户名:");names.setBounds(180,8,55,30);window.add(names);name=newJTextField();name.setBounds(2
6、30,8,60,30);name.setText("服务端");window.add(name);start=newJButton("启动");start.setBounds(300,8,80,30);window.add(start);exit=newJButton("关闭");exit.setBounds(390,8,80,30);window.add(exit);JLabellabel2=newJLabel("用户列表");label2.setBounds(40,40,80,30);wind
7、ow.add(label2);user=newJList();JScrollPanescrollPane=newJScrollPane(user);//添加滚动条scrollPane.setBounds(10,70,120,220);window.add(scrollPane);textMessage=newJTextArea();textMessage.setBounds(135,70,340,220);textMessage.setBorder(newTitledBorder(
8、"聊天记录"));//设置标题textMessage.setEditable(false);//不可编辑//文本内容换行的两个需要配合着用textMessage.setLineWrap(true);//设置文本内容自动换行,在超出文本区域时,可能会切断单词textMessage.setWrapStyleWord(true);//设置以自动换行,以单词为整体,保证单词不会被切断JScrollPanescrollPane1=newJScrollPane(textMessage);//设