这是一款基于Java开发的多功能在线聊天室软件,除了提供即时文字交流外,还特别集成了文件传输功能,极大地方便了用户之间的资料共享与互动。
以下是经过处理后的代码文本:
```java
Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
public ChatClient() {
super(登录聊天室);
pnlLogin = new JPanel();
this.getContentPane().add(pnlLogin);
lblServer = new JLabel(服务器:);
lblPort = new JLabel(端口:);
lblName = new JLabel(用户名:);
lblPassword = new JLabel(口令:);
tfServer = new JTextField(15);
tfServer.setText(127.0.0.1);
tfPort = new JTextField(6);
tfPort.setText(8000);
tfName = new JTextField(20);
pwd = new JPasswordField(20);
btnLogin = new JButton(登录);
btnRegister = new JButton(注册);
btnExit=new JButton(退出);
pnlLogin.setLayout(null);
pnlLogin.setBackground(new Color(205,112,159));
lblServer.setBounds(40,35,50,30);
tfServer.setBounds(90,35,102,25);
lblPort.setBounds(195,35,35,30);
tfPort.setBounds(230,35,55,25);
lblName.setBounds(40,70,50,30);
tfName.setBounds(90,70,195,25);
lblPassword.setBounds(40,100,50,30);
pwd.setBounds(90,100,195,25);
btnLogin.setBounds(30,160,70,25);
btnRegister.setBounds(130,160,70,25);
btnExit.setBounds(230,160,70,25);
pnlLogin.add(lblServer);
pnlLogin.add(tfServer);
pnlLogin.add(lblPort);
pnlLogin.add(tfPort);
pnlLogin.add(lblName);
pnlLogin.add(tfName);
pnlLogin.add(lblPassword);
pnlLogin.add(pwd);
pnlLogin.add(btnLogin);
pnlLogin.add(btnRegister);
pnlLogin.add.btnExit;
setResizable(false);
setSize(320,260);
setVisible(true);
setLocation((ss.width-getWidth())/2,(ss.height-getHeight())/2);
btnLogin.addActionListener(this);
btnRegister.addActionListener(this);
btnExit.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source.equals(btnLogin)){
if(tfName.getText().equals() || pwd.getPassword().equals())
JOptionPane.showMessageDialog(null, 用户名或密码不能为空);
else{
strServerIp=tfServer.getText();
login(); }
}
if(source.equals(btnRegister)){
strServerIp = tfServer.getText();
this.dispose();
new Register(strServerIp,8000);
}
if (source == btnExit) {
System.exit(0);
}
}
public void login() {
User data = new User();
data.name=tfName.getText();
data.password= new String(pwd.getPassword());
try{
String str = InetAddress.getLocalHost().toString();
data.ip= +str.substring(str.lastIndexOf(/), str.length());
}catch (UnknownHostException ex) {
Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
}
try{
Socket sock= new Socket(strServerIp ,8000);
ObjectOutputStream os =new ObjectOutputStream(sock.getOutputStream());
os.writeObject((User)data);
BufferedReader br=new BufferedReader(new InputStreamReader(sock.getInputStream()));
String status =br.readLine();
if(status.equals(登陆成功)){
new ChatRoom((String) data.name, strServerIp);
this.dispose();
//关闭流对象
os.close();
br.close();
sock.close();
} else{
JOptionPane.showMessageDialog(null,status);
os.close();
br.close();
sock.close();
}
}
catch (ConnectException e1){
JOptionPane.showMessageDialog(null, 连接到指定服务器失败!);
}
catch(InvalidClassException e2) {
JOptionPane.showMessageDialog(null,类错误!);
}
catch(NotSerializableException e3){
JOptionPane.showMessageDialog(null, 对象未序列化!);
}
catch(IOException e4){