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

📄 logpane.java

📁 聊天工具
💻 JAVA
字号:
package server.logpane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import server.login.ServerFrme;
/**
 * 日志面板
 * @author 洪景泉
 *
 */
public class LogPane extends JPanel{

	private static final long serialVersionUID = -6883203351638979588L;
	private ServerFrme qqServerFrm=null;
	private JSplitPane splitPane=null;
	private JScrollPane leftPane=null;
	private JPanel rightPane=null;
	
	
	//左边面板
	private JTextArea leftTextArea = null;
	private JScrollPane leftPanel=null;
	
	//右边面板
	private LogEvent le=null;
	private JButton searchBtn=null;
	
	private JLabel dataLab=null;
	private JComboBox year=null;
	private JComboBox month=null;
	private JComboBox day=null;
	
	private Box hBoxTop=null,vBoxTop=null;
	private JScrollPane rightTextPane=null;
	private JTextArea righttextArea = null;
	//private JScrollPane rightPanel=null;
	
	public LogPane(ServerFrme qqServerFrm) {
		this.qqServerFrm = qqServerFrm;
		ini();
		
	}
	private void ini(){
		leftPane=left();
		rightPane=right();
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane,rightPane);
		splitPane.setDividerLocation(340);
		splitPane.setOneTouchExpandable(true);
		this.setLayout(new BorderLayout(0,0));
		this.add(splitPane);
	}
	//左边面板
	private JScrollPane left(){
		leftTextArea=new JTextArea();
		leftTextArea.setEditable(false);
		leftTextArea.setLineWrap(true);
		appendText();
		
				
		leftPanel=new JScrollPane(leftTextArea);
		leftPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(new Color(144, 185, 229)), "今天日志"));
		return leftPanel;
		
	}
	/**
	 * 重新加载日志方法
	 *
	 */
	public void reloadText(){
		appendText();
	}
	private void appendText(){
		String fileName = new SimpleDateFormat("yyyyMMdd").format(new Date());
		File logFile = new File("./log/" + fileName + ".log");
		if(logFile.exists()){
			
			FileReader fr = null;
			BufferedReader br = null;
			try {
				fr = new FileReader(logFile);
				br = new BufferedReader(fr);
				String str = null;
				while ((str = br.readLine()) != null) {
					leftTextArea.append("  "+str+"\n");
	
				}
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e2) {
				e2.printStackTrace();
			} finally {
				try {
					fr.close();
					br.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		
	}
	
	//右边面板
	private JPanel right(){
		
		searchBtn=new JButton("查询");
		searchBtn.setActionCommand("search");
		
		dataLab=new JLabel("请选择日期:");
		year=new JComboBox(getListItem("y"));
		 
		 year.setMaximumRowCount(5);
		 month=new JComboBox();
		 for(int i=1;i<13;i++){
			 if(i<10){
				 month.addItem("0"+String.valueOf(i));
			 }else{
				 month.addItem(String.valueOf(i));
			 }
		 }
		 month.setMaximumRowCount(5);
		 day=new JComboBox();
		 for(int i=1;i<32;i++){
			 if(i<10){
				 day.addItem("0"+String.valueOf(i));
			 }else{
				 day.addItem(String.valueOf(i));
			 }
			 
		 }
		 day.setMaximumRowCount(5);
		
		
		hBoxTop=Box.createHorizontalBox();
		vBoxTop=Box.createVerticalBox();
//		jcBox.setPreferredSize(new Dimension(15,5));
	
		hBoxTop.add(Box.createHorizontalStrut(2));
		hBoxTop.add(dataLab);
		hBoxTop.add(Box.createHorizontalStrut(2));
		hBoxTop.add(year);
		hBoxTop.add(Box.createHorizontalStrut(2));
		hBoxTop.add(month);
		hBoxTop.add(Box.createHorizontalStrut(2));
		hBoxTop.add(day);
		hBoxTop.add(Box.createHorizontalStrut(5));
		hBoxTop.add(searchBtn);
		hBoxTop.add(Box.createHorizontalStrut(10));
		//vBoxTop.add(Box.createVerticalStrut(5));
		vBoxTop.add(hBoxTop);
		vBoxTop.add(Box.createVerticalStrut(5));
		vBoxTop.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(new Color(144, 185, 229)), "日志查询"));
		
		
		righttextArea=new JTextArea();
		righttextArea.setEditable(false);
		righttextArea.setLineWrap(true);
		
		rightTextPane=new JScrollPane(righttextArea);
		rightTextPane.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(new Color(144, 185, 229)), "日志显示"));
		
		rightPane=new JPanel();		
		rightPane.setLayout(new BorderLayout(0,0));
		rightPane.add(vBoxTop,BorderLayout.NORTH);
		rightPane.add(rightTextPane,BorderLayout.CENTER);
	
		//按钮事件
		le=new LogEvent(qqServerFrm);
		searchBtn.addActionListener(le);
		return rightPane;
	}
	//获得年月日列表项
	public Vector<String> getListItem(String dateString){
		File file = new File("./log/");
		String []fileName=file.list();
		
	
		Arrays.sort(fileName);			//升序
		int len=fileName.length;
		//获得年选项
		if(dateString.equals("y")){
			Vector<String> year=new Vector<String>();
			String temp=fileName[0].substring(0,4);
			year.add(temp);
			
			for(int i=1;i<len;i++){
				temp=fileName[i].substring(0, 4);
				if(!(fileName[i-1].substring(0, 4).compareTo(temp)==0)){
					year.add(temp);
				}
			}
					
			return year;
		}
		
		return null;
	}
	
	
	public JComboBox getDay() {
		return day;
	}
	public JComboBox getMonth() {
		return month;
	}
	public JComboBox getYear() {
		return year;
	}
	public JTextArea getRighttextArea() {
		return righttextArea;
	}
	public JTextArea getLeftTextArea() {
		return leftTextArea;
	}
	
	
	
//	public static void main(String[] args) {
//		File f = new File("./log/");
//		String []date=f.list();
//		Arrays.sort(date);
//		for (String string : date) {
//			System.out.println(string);
//		}
//		System.out.println("2008".compareTo("2008"));
//		
//		//升序
//
//	
//		System.out.println(Integer.parseInt( date[date.length-1].substring(0, 4)));
//		System.out.println("-------");
//	}

	
}

⌨️ 快捷键说明

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