首页>代码>基于java swing的聊天室GUI demo实例>/chat/src/com/test/ChatHistory.java
package com.test;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class ChatHistory extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4322029311676518125L;
	private JSplitPane jsp;
	@SuppressWarnings({ "rawtypes", "unused" })
	private JList jl;
	private JScrollPane leftJP;
	private JScrollPane rightJP;
	@SuppressWarnings("rawtypes")
	private JList friends;
	private JTextArea JTrecord;
	private String txtName;
	private Parameterx parameterx;
	private File file;
	@SuppressWarnings("unused")
	private ArrayList<String> fileName;
public ChatHistory(){
		
		this.setTitle("聊天记录查询");
		init();
		setBounds(100,100,600,300);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	}
	@SuppressWarnings({ "static-access", "rawtypes", "unchecked" })
	public void init(){
		parameterx = new Parameterx();
		file = new File(parameterx.path);
		File[] fa = file.listFiles();
		String []data = new String[fa.length];
		int strleng = 0;
		for (File file : fa) {
			if(strleng < fa.length){
				data[strleng] = new String(file.getName());
				strleng++;
			}
		}	
		jsp = new JSplitPane();//底容器
		jsp.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		leftJP = new JScrollPane();//列表的滚动条
		rightJP = new JScrollPane();
		JTrecord = new JTextArea();
		
		jsp.setLeftComponent(leftJP);
		jsp.setRightComponent(rightJP);
		friends = new JList(data);

		leftJP.getViewport().add(friends);
		friends.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2){
					JTrecord.setText("");
					String name = friends.getSelectedValue().toString();
					txtName = parameterx.path + "\\" + name;
					//txtName += ".txt";
					fileReader();
				}
			}
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		rightJP.getViewport().add(JTrecord);
		
		add(jsp);

	}
	public void fileReader(){
		try{
			
			FileReader fr = new FileReader(txtName);
			BufferedReader br = new BufferedReader(fr);
			
			String s;
			while((s = br.readLine())!=null){
				JTrecord.append(s + "\n");
			}
			br.close();
		}catch(FileNotFoundException e){
			JOptionPane.showMessageDialog(this, "找不到聊天记录");
			return ;
		}catch(IOException e){
			JOptionPane.showMessageDialog(this, "读文件出错");
			return ;
		}
	}
}
最近下载更多
taoshen95  LV14 1月19日
漫步的海星  LV4 2023年8月21日
2017143155  LV12 2023年6月24日
wanglinddad  LV54 2022年6月18日
微信网友_6003487859068928  LV5 2022年6月16日
zhendong  LV7 2022年3月6日
dfz12345  LV4 2021年12月8日
胡萝北呀  LV3 2021年11月28日
ComeDebug  LV6 2021年9月6日
忧麦紫  LV18 2021年6月26日
最近浏览更多
taoshen95  LV14 1月19日
陈小灏  LV14 2023年12月27日
求学的熊猫  LV6 2023年12月25日
wuxin427 2023年12月20日
暂无贡献等级
fuyouou  LV5 2023年6月29日
2017143155  LV12 2023年6月24日
17683946472  LV9 2023年6月8日
CL200228  LV4 2023年5月4日
青柠编程  LV13 2023年4月18日
xingbing  LV9 2023年2月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友