2.3.9实现聊天记录

2.3.9实现聊天记录_第1张图片
Paste_Image.png
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class QQMain extends JFrame implements ActionListener {
    JTextField txtMess = new JTextField();
    JComboBox cmbUser = new JComboBox();
    JTextArea txtContent = new JTextArea();
    
    QQMain(){
        this.setSize(300, 400);
        
        //new组件
        JButton btnSend = new JButton("发送");
        
        JScrollPane spContent = new JScrollPane(txtContent);
        
        //注册事件监听
        btnSend.addActionListener(this);
        
        //布置小面板
        JPanel panSmall = new JPanel();
        panSmall.setLayout(new GridLayout(1, 2));
        
        panSmall.add(cmbUser);
        panSmall.add(btnSend);
        
        //布置大面板
        JPanel panBig = new JPanel();
        panBig.setLayout(new GridLayout(2, 1));
        
        panBig.add(txtMess);
        panBig.add(panSmall);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panBig, BorderLayout.NORTH);
        this.add(spContent,BorderLayout.CENTER);
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        
    }
}
Paste_Image.png
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class QQLogin extends JFrame implements ActionListener{
    //new 组件
    JLabel labUser = new JLabel("用户名");
    JLabel labPass = new JLabel("密码");
    
    JTextField txtUser = new JTextField();
    JPasswordField txtPass = new JPasswordField();
    
    JButton btnLogin = new JButton("登录");
    JButton btnReg = new JButton("注册");
    JButton btnCancel = new JButton("取消");
    
    //布置输入面板
    JPanel panInput = new JPanel();
    
    //布置按钮面板
    JPanel panButton = new JPanel();
    
    public QQLogin() {
        this.setSize(250, 125);
        //注册事件监听
        btnLogin.addActionListener(this);
        btnReg.addActionListener(this);
        btnCancel.addActionListener(this);
        
        //布置输入面板
        panInput.setLayout(new GridLayout(2, 2));
        panInput.add(labUser);
        panInput.add(txtUser);
        
        panInput.add(labPass);
        panInput.add(txtPass);
        
        //布置按钮面板
        panButton.setLayout(new FlowLayout());
        
        panButton.add(btnLogin);
        panButton.add(btnReg);
        panButton.add(btnCancel);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panInput, BorderLayout.CENTER);
        this.add(panButton, BorderLayout.SOUTH);
        
    }
    public static void main(String args[]){
        Object w = new QQLogin();
        ((QQLogin)w).setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        if(arg0.getActionCommand().equals("登录")){
//          System.out.println("用户点击了登录");
            String user = txtUser.getText();
            String pass = txtPass.getText();
            if (user.equals("aaa")&& pass.equals("111")) {
                QQMain w = new QQMain();
                w.setVisible(true);
                this.setVisible(false);
            }
        }
        if(arg0.getActionCommand().equals("注册")){
            System.out.println("用户点击了注册");
        }
        if(arg0.getActionCommand().equals("取消")){
            System.out.println("用户点击了取消");
        }
    }
}
2.3.9实现聊天记录_第2张图片
Paste_Image.png
2.3.9实现聊天记录_第3张图片
Paste_Image.png
Paste_Image.png
2.3.9实现聊天记录_第4张图片
Paste_Image.png
2.3.9实现聊天记录_第5张图片
Paste_Image.png
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class QQLogin extends JFrame implements ActionListener{
    //new 组件
    JLabel labUser = new JLabel("用户名");
    JLabel labPass = new JLabel("密码");
    
    JTextField txtUser = new JTextField();
    JPasswordField txtPass = new JPasswordField();
    
    JButton btnLogin = new JButton("登录");
    JButton btnReg = new JButton("注册");
    JButton btnCancel = new JButton("取消");
    
    //布置输入面板
    JPanel panInput = new JPanel();
    
    //布置按钮面板
    JPanel panButton = new JPanel();
    
    public QQLogin() {
        this.setSize(250, 125);
        //注册事件监听
        btnLogin.addActionListener(this);
        btnReg.addActionListener(this);
        btnCancel.addActionListener(this);
        
        //布置输入面板
        panInput.setLayout(new GridLayout(2, 2));
        panInput.add(labUser);
        panInput.add(txtUser);
        
        panInput.add(labPass);
        panInput.add(txtPass);
        
        //布置按钮面板
        panButton.setLayout(new FlowLayout());
        
        panButton.add(btnLogin);
        panButton.add(btnReg);
        panButton.add(btnCancel);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panInput, BorderLayout.CENTER);
        this.add(panButton, BorderLayout.SOUTH);
        
    }
    public static void main(String args[]){
        Object w = new QQLogin();
        ((QQLogin)w).setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        if(arg0.getActionCommand().equals("登录")){
//          System.out.println("用户点击了登录");
            String user = txtUser.getText();
            String pass = txtPass.getText();
            if (user.equals("aaa")&& pass.equals("111")) {
                QQMain w = new QQMain();
                w.setVisible(true);
                this.setVisible(false);
            }
        }
        if(arg0.getActionCommand().equals("注册")){
            System.out.println("用户点击了注册");
        }
        if(arg0.getActionCommand().equals("取消")){
            System.out.println("用户点击了取消");
        }
    }
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class QQMain extends JFrame implements ActionListener {
    JTextField txtMess = new JTextField();
    JComboBox cmbUser = new JComboBox();
    JTextArea txtContent = new JTextArea();
    
    QQMain(){
        this.setSize(300, 400);
        
        //new组件
        JButton btnSend = new JButton("发送");
        
        JScrollPane spContent = new JScrollPane(txtContent);
        
        //注册事件监听
        btnSend.addActionListener(this);
        
        //布置小面板
        JPanel panSmall = new JPanel();
        panSmall.setLayout(new GridLayout(1, 2));
        
        panSmall.add(cmbUser);
        panSmall.add(btnSend);
        
        //布置大面板
        JPanel panBig = new JPanel();
        panBig.setLayout(new GridLayout(2, 1));
        
        panBig.add(txtMess);
        panBig.add(panSmall);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panBig, BorderLayout.NORTH);
        this.add(spContent,BorderLayout.CENTER);
        
        //读聊天记录
        try {
            File f = new File("D:/work/聊天记录.qq");
                
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
                    
            while (br.ready()) {
                txtContent.append(br.readLine()+"\n");      
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        //txtMess ====>txtContent
        txtContent.append(txtMess.getText()+"\n");
        
        //将txtMess的内容存入聊天记录文件
        try {
            File f = new File("D:/work/聊天记录.qq");
            
            FileWriter fw = new FileWriter(f);
            PrintWriter pw = new PrintWriter(fw);
            
            pw.println(txtMess.getText());
            
            pw.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
        //清除txtMess中的内容
        txtMess.setText("");
    }
}
2.3.9实现聊天记录_第6张图片
Paste_Image.png
2.3.9实现聊天记录_第7张图片
Paste_Image.png
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class QQMain extends JFrame implements ActionListener {
    JTextField txtMess = new JTextField();
    JComboBox cmbUser = new JComboBox();
    JTextArea txtContent = new JTextArea();
    
    QQMain(){
        this.setSize(300, 400);
        
        //new组件
        JButton btnSend = new JButton("发送");
        
        JScrollPane spContent = new JScrollPane(txtContent);
        
        //注册事件监听
        btnSend.addActionListener(this);
        
        //布置小面板
        JPanel panSmall = new JPanel();
        panSmall.setLayout(new GridLayout(1, 2));
        
        panSmall.add(cmbUser);
        panSmall.add(btnSend);
        
        //布置大面板
        JPanel panBig = new JPanel();
        panBig.setLayout(new GridLayout(2, 1));
        
        panBig.add(txtMess);
        panBig.add(panSmall);
        
        //布置窗体
        this.setLayout(new BorderLayout());
        
        this.add(panBig, BorderLayout.NORTH);
        this.add(spContent,BorderLayout.CENTER);
        
        //读聊天记录
        try {
            File f = new File("D:/work/聊天记录.qq");
                
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
                    
            while (br.ready()) {
                txtContent.append(br.readLine()+"\n");      
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0){
        //txtMess ====>txtContent
        txtContent.append(txtMess.getText()+"\n");
        
        //将txtMess的内容存入聊天记录文件
        try {
            File f = new File("D:/work/聊天记录.qq");
            
            FileWriter fw = new FileWriter(f,true);
            PrintWriter pw = new PrintWriter(fw);
            
            pw.println(txtMess.getText());
            
            pw.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
        //清除txtMess中的内容
        txtMess.setText("");
    }
}
2.3.9实现聊天记录_第8张图片
Paste_Image.png

你可能感兴趣的:(2.3.9实现聊天记录)