⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javachatcode.txt

📁 小小的java聊天室源代码 希望对初学者有帮助!
💻 TXT
📖 第 1 页 / 共 5 页
字号:
以下是登陆类login.java:
package chatroom;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.util.*;
import javax.swing.*;
public class Login extends JFrame {
 JLabel lblUsername, lblPassword;
 JComboBox comboUsername;
  JPasswordField txtPassword;
  JButton jbnButton1, jbnButton2,jbnButton3;
  BufferedReader read1;
  PrintWriter write1;
 JLabel lblname,lblwelcome,lb_ServerName,lb_port;
 JTextField jt_servername,jt_port;
 String s32;
 TreeSet tr1;
 Socket clientSocket;
 BufferedReader sin;
 PrintWriter sout;
 liaotianshi lt1;
 String ServerIp="localhost";
 int ServerPort=10001;
 boolean loginSucc;
 String nickname;
 public Login(String title) {
  super(title);
  tr1=new TreeSet();
  String s="";
  loginSucc=false;
  try{
   comboUsername = new JComboBox();
   if ((new File("user.txt")).exists()){
     read1 = new BufferedReader(new FileReader("user.txt"));
    while ((s = read1.readLine()) != null)
    {
     comboUsername.addItem(s);
     comboUsername.setSelectedIndex(0);
     tr1.add(s);
    }
    read1.close();
   }
    
  lblname= new JLabel("好朋友聊天室");
  lblname.setFont(new Font("宋体",Font.PLAIN,20));
  lblname.setForeground(Color.RED);
  lblwelcome= new JLabel("欢迎您!");
  
  lblUsername = new JLabel("用户名:");
  lblPassword = new JLabel("密  码:");
  txtPassword = new JPasswordField(10);
  lb_ServerName=new JLabel("服务器名称/IP");
  lb_port=new JLabel("端口号");
  jt_servername = new JTextField(15);
  jt_servername.setText("localhost");
  jt_port = new JTextField(15);
  jt_port.setText("10001");
  
  comboUsername.setEditable(true);
  jbnButton1 = new JButton("登录(Y)");
  jbnButton2 = new JButton("取消(X)");
  jbnButton3 = new JButton("注册(Z)");
  jbnButton3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    //在这里添加注册模块的打开代码.
    reg_user reg1=new reg_user(ServerIp,ServerPort);
    
   }
  });
  jbnButton1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
            try {
             String user,pass;
             ServerIp=jt_servername.getText();
             ServerPort=Integer.valueOf(jt_port.getText());
             user=(String)comboUsername.getSelectedItem();
             pass=txtPassword.getText();
             if (connectserver(user,pass)==1) {
              //将登陆成功的用户记入用户列表.
              tr1.add(user);
              write1 =new PrintWriter( new BufferedWriter(new FileWriter("user.txt",false)));
              Iterator it1;
              it1=tr1.iterator();
              while (it1.hasNext()){
               write1.println(it1.next());
              }
                 write1.close();
                 //打开聊天室界面并隐藏登陆界面.
                 lt1 = new liaotianshi(jt_port.getText()+"聊天室",user,sin,sout,"Normal",ServerIp,ServerPort);
                 lt1.pack();
                  setVisible(false);
                  lt1.setVisible(true);
                  lt1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  loginSucc=true;
                  System.out.println("登录成功!");
             }
             else {
              loginSucc=false;
             }
            }
           catch (IOException e2){
            //System.out.println(e2.toString());
           }
   }
  });
  jbnButton2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    //退出
    if (loginSucc) {
     sout.println("/logout");
     sout.flush();
     try{
      sout.close();
      clientSocket.close();
      
     }catch (IOException dfe){
      
     }

    }
    System.exit(0);
   }

  });
  
  jbnButton1.setMnemonic(KeyEvent.VK_Y);
  jbnButton2.setMnemonic(KeyEvent.VK_X);
  jbnButton3.setMnemonic(KeyEvent.VK_Z);
 // getContentPane().setLayout(new FlowLayout());
  Insets is = new Insets(5,5,5,5);
  
  getContentPane().setLayout(new GridBagLayout());
  GridBagConstraints gBC = new GridBagConstraints();

  gBC.anchor=GridBagConstraints.CENTER;
  gBC.fill = GridBagConstraints.NONE;
        gBC.gridx = 1;
        gBC.gridy = 1;
        gBC.gridwidth = 2;
        gBC.gridheight=1;
        gBC.insets=is;
  getContentPane().add(lblname,gBC);
  
  gBC.anchor=GridBagConstraints.CENTER;
  gBC.fill = GridBagConstraints.NONE;
        gBC.gridy = 2;
  getContentPane().add(lblwelcome,gBC);

  
  gBC.anchor=GridBagConstraints.WEST;
  gBC.fill = GridBagConstraints.NONE;
        gBC.gridx = 1;
        gBC.gridy = 3;
        gBC.gridwidth = 1;
        gBC.gridheight=1;
        gBC.insets=is;
  getContentPane().add(lblUsername,gBC);
  
        gBC.gridx = 1;
        gBC.gridy = 4;
        gBC.gridwidth = 1;
        gBC.gridheight=1;
        gBC.insets=is;
  getContentPane().add(lblPassword,gBC);
  
     gBC.gridx = 1;
     gBC.gridy = 5;
     getContentPane().add(lb_ServerName,gBC);
    
     gBC.gridx = 2;
     gBC.gridy = 5;
     getContentPane().add(jt_servername,gBC);    
    
     gBC.gridx = 1;
     gBC.gridy = 6;
     getContentPane().add(lb_port,gBC);
    
     gBC.gridx = 2;
     gBC.gridy = 6;
     getContentPane().add(jt_port,gBC);    
    
    
    
  
  gBC.anchor=GridBagConstraints.WEST;
  gBC.fill = GridBagConstraints.HORIZONTAL;
        gBC.gridx = 2;
        gBC.gridy = 3;
        gBC.gridwidth = 1;
        gBC.gridheight=1;
        gBC.insets=is;
  getContentPane().add(comboUsername,gBC);
  
        gBC.gridx = 2;
        gBC.gridy = 4;
        gBC.gridwidth = 1;
        gBC.gridheight=1;
        gBC.insets=is;
  getContentPane().add(txtPassword,gBC);

  JPanel jp1=new JPanel();
  jp1.setLayout(new FlowLayout());
  jp1.add(jbnButton1,gBC);
  jp1.add(jbnButton2,gBC);
  jp1.add(jbnButton3,gBC);
        gBC.gridx = 1;
        gBC.gridy = 7;
        gBC.gridwidth = 2;
        gBC.gridheight=1;
        gBC.insets=new Insets(10,10,10,10);
 
  getContentPane().add(jp1,gBC);
  
  }
   catch (IOException e){
    System.out.println(e.toString());
   }

 }
 public int connectserver(String username,String password){

  String reply;
  try {
   clientSocket=new Socket(ServerIp, ServerPort);
   sin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
   sout = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
   sout.println("login");
   sout.flush();
   sout.println(username);
   sout.flush();
   sout.println(password);
   sout.flush();
   sout.println("Normal");
   sout.flush();
   reply=sin.readLine();
   if (reply.equals("login succ") )
    return 1;
   else if (reply.equals("same user login")){
    JOptionPane.showMessageDialog(null, "登录失败(该用户名已经被占用)");
    return 0;
   }
   else if(reply.equals("user waiting confirm")){
    JOptionPane.showMessageDialog(null, "登录失败(该用户还未通过验证.)");
    return 0;
   }
   else
   {
    JOptionPane.showMessageDialog(null, "登录失败(原因:错误的用户名或密码)");
    return 0;
   }
  } catch (Exception e) {
   JOptionPane.showMessageDialog(null, "无法连接到服务器!");
  }
  return 0;
 }
 
 public static void main(String[] args) {
  Login frm = new Login("登陆界面");
  frm.pack();
  frm.setVisible(true);
  frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}


以下是注册类reg_user.java

package chatroom;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.*;
public class reg_user extends JFrame{
 JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8;
 JTextField jf1,jf3,jf4,jf6,jf7;
 JTextArea jta8;
 JComboBox jcb5;
 JPasswordField jpf2;
 JButton jb1,jb2,jb3;
 String ServerIp;
 int ServerPort;
 public reg_user(String as_ServerIp,int as_ServerPort){
  super("注册新用户");
  ServerIp=as_ServerIp;
  ServerPort=as_ServerPort;
  jl1 = new JLabel("用户ID:");
  jl2 = new JLabel("密码:");
  jl3 = new JLabel("姓名:");
  jl4 = new JLabel("呢称:");
  jl5 = new JLabel("性别:");
  jl6 = new JLabel("个人主页:");
  jl7 = new JLabel("电子邮箱:");
  jl8 = new JLabel("个人简介:");
  
  jf1 = new JTextField(15);
  jpf2 = new JPasswordField(15);
  jf3 = new JTextField(15);
  jf4 = new JTextField(15);
  jcb5 = new JComboBox();
  jcb5.addItem("男");
  jcb5.addItem("女");
  jcb5.setSelectedIndex(0);
 // jcb5.setEditable(true);
  jf6 = new JTextField(25);
  jf7 = new JTextField(25);
  jta8 = new JTextArea(1, 25);
  
  jb1 = new JButton("提交(T)");
  jb2 = new JButton("清空(Q)");
  jb3 = new JButton("验证是否重名(Y)");
  jb1.setMnemonic(KeyEvent.VK_T);
  jb2.setMnemonic(KeyEvent.VK_Q);
  jb3.setMnemonic(KeyEvent.VK_Y);
  Insets is = new Insets(5,5,5,5);
  getContentPane().setLayout(new GridBagLayout());
  GridBagConstraints gBC = new GridBagConstraints();
  gBC.anchor=GridBagConstraints.EAST;
  gBC.fill = GridBagConstraints.NONE;
        gBC.gridwidth = 1;
        gBC.gridheight=1;
        gBC.insets=is;
        Container thisCP;
        thisCP=getContentPane();
     JScrollPane scrollPane_2 = new JScrollPane(jta8,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  scrollPane_2.setPreferredSize(new Dimension(400,150));
  //第一列:标签列
  gBC.gridy = 1;
        gBC.gridx = 1;
        thisCP.add(jl1,gBC);
   gBC.gridy = 2;
  thisCP.add(jl2,gBC);
  gBC.gridy = 3;
  thisCP.add(jl3,gBC);
  gBC.gridy = 4;
  thisCP.add(jl4,gBC);
  gBC.gridy = 5;
  thisCP.add(jl5,gBC);
  gBC.gridy = 6;
  thisCP.add(jl6,gBC);
  gBC.gridy = 7;
  thisCP.add(jl7,gBC);
  gBC.gridy = 8;
  thisCP.add(jl8,gBC);
  //第二列:控件列
  gBC.anchor=GridBagConstraints.WEST;
   
     gBC.gridx = 2;
     gBC.gridy = 1;
     thisCP.add(jf1,gBC);
     gBC.gridwidth = 2;
  gBC.gridy = 2;
  thisCP.add(jpf2,gBC);
  gBC.gridy = 3;
  thisCP.add(jf3,gBC);
  gBC.gridy = 4;
  thisCP.add(jf4,gBC);
  gBC.gridy = 5;
  thisCP.add(jcb5,gBC);
  gBC.gridy = 6;
  thisCP.add(jf6,gBC);
  gBC.gridy = 7;
  thisCP.add(jf7,gBC);
  gBC.gridy = 8;
  thisCP.add(scrollPane_2,gBC);
     gBC.gridwidth = 1;
 
  gBC.gridx = 2;
  gBC.gridy = 9;
  thisCP.add(jb1,gBC);
  gBC.gridx = 3;
  thisCP.add(jb2,gBC);
  gBC.gridx = 3;
  gBC.gridy = 1;
  thisCP.add(jb3,gBC);
  this.pack();
  this.setVisible(true);
  //清空表单.
  jb2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    clear_form();
   }
  });
  //注册用户信息到数据库.
  jb1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    send_reg_info();
   }
  });
  //测试用户ID是否同名.
  jb3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    check_same_userid();
   }
  });
 }
 public void clear_form(){
  jf1.setText("");
  jpf2.setText("");
  jf3.setText("");
  jf4.setText("");
  jcb5.setSelectedItem("男");
  jf6.setText("");
  jf7.setText("");
  jta8.setText("");
 }
 public void check_same_userid(){
  Socket clientSocket=null;
  BufferedReader sin=null;
  PrintWriter sout=null;
  String reply;
  if (jf1.getText().equals("")){
   JOptionPane.showMessageDialog(null, "用户ID不能为空!");
   return;
  }
  try{
   clientSocket=new Socket(ServerIp, ServerPort);
   sin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
   sout = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
   sout.println("checksameuserid");
   sout.println(jf1.getText());
   sout.flush();
   reply=sin.readLine();
   if (reply.equals("same_username")){
    JOptionPane.showMessageDialog(null, "该用户ID已被占用!");
    sout.close();
    sin.close();
    clientSocket.close();
    return;
   }
   else if (reply.equals("ok_to_regedit")){
    JOptionPane.showMessageDialog(null, "该用户ID可以注册,抓紧时间哦!");
    sout.close();
    sin.close();
    clientSocket.close();
    return;
   }
   else if (reply.equals("db_connect_fail")){
    JOptionPane.showMessageDialog(null, "数据库连接错误!");
    sout.close();
    sin.close();
    clientSocket.close();
    return;
   }
  }
  catch (IOException e) {
   try{
    clear_form();
    sout.close();
    sin.close();
    clientSocket.close();
    }catch (IOException e2)
    {
     System.out.println("严重错误!系统将关闭");
     System.exit(0);
    }
   JOptionPane.showMessageDialog(null, "服务器连接无效!");
   return;
  }
   return;
 }
 public boolean check_form(){
  if (jf1.getText().equals("")){
   JOptionPane.showMessageDialog(null, "用户ID不能为空!");
   jf1.grabFocus();
   return false;
  }
  if (jpf2.getText().equals("")){
   JOptionPane.showMessageDialog(null,"密码不能为空");
   jpf2.grabFocus();
   return false;
  }  
  if (jf3.getText().equals("")){
   JOptionPane.showMessageDialog(null,"姓名不能为空");
   jf3.grabFocus();
   return false;
  }  
  if (jf4.getText().equals("")){
   JOptionPane.showMessageDialog(null,"呢称不能为空");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -