JAVA笔记 Form界面的log展示

   
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public  class MainForm  extends Thread{
     private JFrame frame;
     private JScrollPane scrollpanel;
     private JPanel panel =  new JPanel();
     private  final JTextArea text;
     private  boolean islogging= true;
    Log log =LogFactory.getLog(MainForm. class);
    
     public MainForm(){
             final JButton startButton =  new JButton("start");
            startButton.setActionCommand("start");
             final JButton stopButton =  new JButton("stop");
            stopButton.setActionCommand("stop");

            text=  new JTextArea();
            scrollpanel =  new JScrollPane();
            scrollpanel.setViewportView(text);
            panel.setLayout( new BorderLayout());
            panel.add(scrollpanel,BorderLayout.CENTER);
            
            JPanel bottom =  new JPanel();
            bottom.setLayout( new FlowLayout());
            bottom.add(startButton);
            bottom.add(stopButton);
    
        panel.add(bottom,BorderLayout.SOUTH);
            ActionListener listener =  new ActionListener(){
    
                @Override
                 public  void actionPerformed(ActionEvent e) {
                     if("start".equals(e.getActionCommand())){
                        Thread thread =  new Thread(){
                             public  void run(){
                                 // 你自己的操作
                            }
                        };        
                        thread.start();
                    }
                     else  if("stop".equals(e.getActionCommand())){
                         // 你自己的操作
                    }
                     else{
                        
                    }
                }
                
            };
            startButton.addActionListener(listener);
            stopButton.addActionListener(listener);
    
            frame =  new JFrame();
            frame.setTitle("程序");
            frame.getContentPane().add(panel);
            frame.setSize(600,400);
            frame.setResizable( false);
            frame.setVisible( true);
            frame.addWindowListener( new WindowListener(){
        
                    @Override
                     public  void windowActivated(WindowEvent e) {
                         //  TODO Auto-generated method stub
                        
                    }
        
                    @Override
                     public  void windowClosed(WindowEvent e) {
                        System.exit(0);
                    }
        
                    @Override
                     public  void windowClosing(WindowEvent e) {                
                        islogging= false;
                        System.exit(0);
                    }
        
                    @Override
                     public  void windowDeactivated(WindowEvent e) {
                         //  TODO Auto-generated method stub
                        
                    }
        
                    @Override
                     public  void windowDeiconified(WindowEvent e) {
                         //  TODO Auto-generated method stub
                        
                    }
        
                    @Override
                     public  void windowIconified(WindowEvent e) {
                         //  TODO Auto-generated method stub
                        
                    }
        
                    @Override
                     public  void windowOpened(WindowEvent e) {
                         //  TODO Auto-generated method stub
                        
                    }
                });
                
            @Override
           public  void run() {
             int count=0;
             while(islogging){
                 try{
                     synchronized( this.text){
                       // 从log队列中取出下一个
                        LogBean msg =LogDispacher.push();
                         if(msg== null){
                             continue;
                        }
                        LogBean.LogType type=msg.getType();
                         // 判断日志是否要显示到界面上
                         if(type==LogBean.LogType.LOGFILE||type==LogBean.LogType.BOTH){
                            log.info(msg.getMsg());
                             if(msg.getType()==LogBean.LogType.LOGFILE){
                                 continue;
                            }
                        }
                                            
                        count++;
                         if(count>100){
                            count=0;
                             this.text.setText("");
                        }
                        StringBuilder build =  new StringBuilder( this.text.getText());
                        build.append(msg.getMsg());
                         this.text.setText(build.toString()+"\n");
                    }
                    
                }
                 catch(Exception ex){
                    
                }
                
                 try {
                    Thread.sleep(1000);
                }  catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }        
        }
}



package com.ect.smsmessager.server;

import java.util.LinkedList;
import java.util.Queue;

import com.ect.smsmessager.bean.LogBean;

public  class LogDispacher {

     public  static Queue<LogBean> logQueue=  new LinkedList<LogBean>();
    
     public  static  void poll(String msg,LogBean.LogType type){
        LogBean bean =  new LogBean(msg,type);
         synchronized(logQueue){
            logQueue.offer(bean);
        }
    }
    
     public  static  LogBean push(){
         synchronized(logQueue){
             if(logQueue!= null&&!logQueue.isEmpty()){
                 return logQueue.poll();
            }
             return  null;
        }
    }
}

你可能感兴趣的:(JAVA笔记 Form界面的log展示)