用java实现简单的聊天小程序

该小程序目前还不具有对话功能,只有最基本的登录,显示聊天记录的功能

 技术分享
//登录界面
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
public class QQLogin extends JFrame{
   private JLabel labUser;   //用户标签
   private JLabel labPass;   //密码标签
   private JTextField txtUser;  //用户信息输入框
   private JPasswordField txtPass; //用户密码输入框
   private JButton btnReg;   //注册按钮
   private JButton btnLogin;  //登录按钮
   private JButton btnCancel;  //取消按钮
 
   private JPanel jpCenter;  //中部布局
   private JPanel jpSouth;   //南部布局
   public QQLogin(){
      init();
      this.setSize(250,125);
      this.setResizable(false);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLocation(300,100);
   }
   //界面布局
   public void init(){
      //用户标签和密码标签
      labUser=new JLabel("用户");
      labPass=new JLabel("密码");
       //用户和密码输入框
      txtUser=new JTextField();
      txtPass=new JPasswordField();
      //按钮
      btnReg=new JButton("注册");
      btnLogin=new JButton("登录");
      btnCancel=new JButton("取消");
  
      //中部布局
      jpCenter=new JPanel();
      jpCenter.setLayout(new GridLayout(2,2));
      jpCenter.add(labUser);
      jpCenter.add(txtUser);
      jpCenter.add(labPass);
      jpCenter.add(txtPass);
      this.add(jpCenter,BorderLayout.CENTER);
  
      //南部布局
      jpSouth=new JPanel();
      jpSouth.setLayout(new FlowLayout());
      jpSouth.add(btnReg);
      jpSouth.add(btnLogin);
      jpSouth.add(btnCancel);
      this.add(jpSouth,BorderLayout.SOUTH);
   }
 public static void main(String[] args){
  QQLogin login=new QQLogin();
  login.setVisible(true);
 }
技术分享
//注册界面
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
public class QQReg extends JFrame implements ActionListener{
  JLabel labMess;//显示注册标签
  JLabel labUser;//用户标签
  JLabel labPass;//密码标签
  JTextField txtUser;//用户输入框
  JPasswordField txtPass;//密码输入框
  JButton btnOK;//确认按钮
  JButton btnCancel;//取消按钮
  JPanel jpSouth;
  JPanel jpCenter;
  public QQReg(){
    init();
    this.setBounds(500,200,300,150);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public void init(){
    //实例化所有选项
    labMess=new JLabel("注册信息");
    labUser=new JLabel("用户");
    labPass=new JLabel("密码");
    txtUser=new JTextField();
    txtPass=new JPasswordField();
    btnOK=new JButton("确认");
    btnOK.addActionListener(this);
    btnCancel=new JButton("取消");
    btnCancel.addActionListener(this);
 
    jpSouth=new JPanel();
    jpSouth.add(btnOK);
    jpSouth.add(btnCancel);
 
    jpCenter=new JPanel();
    jpCenter.setLayout(new GridLayout(2,2));
    jpCenter.add(labUser);
    jpCenter.add(txtUser);
    jpCenter.add(labPass);
    jpCenter.add(txtPass);
 
    this.add(labMess,BorderLayout.NORTH);
    this.add(jpCenter,BorderLayout.CENTER);
    this.add(jpSouth,BorderLayout.SOUTH);
 
  }
  @Override
  public void actionPerformed(ActionEvent e){
    if(e.getActionCommand()=="确认"){
      try{
         File outFile=new File("F:/java/QQSource.txt");
 
         FileWriter fw=new FileWriter(outFile,true);
         PrintWriter pw=new PrintWriter(fw);
 
         String user=txtUser.getText();
         String pass=new String(txtPass.getPassword());
 
         if(!(user.equals("")&&pass.equals(""))){
           pw.println(user+"%"+pass);
           pw.flush();
           this.setVisible(false);
         }
        }catch(Exception e1){
          e1.printStackTrace();
        }
    }
    if(e.getActionCommand()=="取消"){
      txtUser.setText("");
      txtPass.setText("");
    }
  }
//  public static void main(String[] args){
//    QQReg r=new QQReg();
//    r.setVisible(true);
//  }
}
技术分享
//聊天界面
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
public class QQMain extends JFrame implements ActionListener{
  private JTextArea txtSend;//发送信息的窗口
  private JTextArea txtMess;//显示信息的窗口
  private JButton btnSend;//发送按钮去
  private JButton btnCancel;//取消按钮
  private JButton btnMess;//显示消息按钮
  private JComboBox jbList;//显示聊天对象的下拉列表
  private JPanel jpSmall;
  private JPanel jpBig;
  private int score=0;
  private QQChat chat;
  public QQMain(){
    init();
    chat=new QQChat();
    this.setBounds(300,100,300,400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
  }
  //界面布局按钮
  public void init(){
    txtSend=new JTextArea();
    txtMess=new JTextArea(10,10);
    txtMess.setEditable(false);
 
    btnSend=new JButton("发送");
    btnSend.addActionListener(this);
    btnCancel=new JButton("取消");
    btnMess=new JButton("聊天记录");
    btnMess.addActionListener(this);
    jbList=new JComboBox();
 
    jpSmall=new JPanel();
    jpSmall.add(jbList);
    jpSmall.add(btnSend);
    jpSmall.add(btnCancel);
    jpSmall.add(btnMess);
 
    jpBig=new JPanel();
    jpBig.setLayout(new BorderLayout());
    jpBig.add(jpSmall,BorderLayout.NORTH);
    jpBig.add(txtSend,BorderLayout.CENTER);
 
    this.setLayout(new GridLayout(2,1));
    this.add(txtMess);
    this.add(jpBig);
  }
  @Override
  public void actionPerformed(ActionEvent e){
    if(e.getActionCommand().equals("发送")){
      if(!txtSend.getText().equals("")){
        writeChat();
        txtMess.append(txtSend.getText()+"\n");
        chat.getArea().append(txtSend.getText()+"\n");
        txtSend.setText("");
      }
    }
    if(e.getActionCommand().equals("聊天记录")){
      score++;
      if(score%2==1){
        chat.setVisible(true);
      }else{
        chat.setVisible(false);
      }
    }
  }
  public void writeChat(){
    try{
      File chat=new File("F:/java/chat.txt");
 
      FileWriter fw=new FileWriter(chat,true);
      PrintWriter pw=new PrintWriter(fw);
 
      String s=txtSend.getText();
      pw.println(s);
      pw.flush();
 
    }catch(Exception e){
      e.printStackTrace();
    }
  }
//  public static void main(String[] args){
//    QQMain main=new QQMain();
//    main.setVisible(true);
//  }
}
//聊天记录界面
package QQ1;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
public class QQChat extends JFrame{
  private JTextArea areaMess;
  public QQChat(){
    areaMess=new JTextArea();
    areaMess.setEditable(false);
    chatFile();
    this.add(areaMess);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(620,100,300,400);;
  }
  public JTextArea getArea(){
    return areaMess;
  }
  public void chatFile(){
    try{
      File chat=new File("F:/java/chat.txt");
      FileReader fr=new FileReader(chat);
 
      BufferedReader br=new BufferedReader(fr);
 
      StringBuffer s=new StringBuffer();
      while(br.ready()){
        s.append(br.readLine()+"\n");
      }
       areaMess.setText(s.toString());
     }catch(Exception e){
       e.printStackTrace();
     }
  }
//  public static void main(String[] args){
//    QQChat chat=new QQChat();
/    /chat.setVisible(true);
//  }
}
 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。