作为日常练习内容,就不分析了,我把代码直接贴上,在注释中添加解释
package com.Logging;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* A modification of the image viewer program that logs various events.
* 图像查看器程序的修改,记录各种事件。
* @author 李阳
*
*/
public class LoggingImageViewer
{
public static void main(String[] args)
{
/**
* 下面这段代码确保所有的信息记录到应用程序特定文件中
*/
if (System.getProperty("java.util.logging.config.class")==null
&& System.getProperty("java.util.logging.config.file")==null)
{
try {
//开启所有级别的记录
Logger.getLogger("com.NoobYang.corejava").setLevel(Level.ALL);
final int LOG_ROTATION_COUNT = 10;
Handler handler = new FileHandler("%h/LoggingImageViewer",0,LOG_ROTATION_COUNT);
Logger.getLogger("com.NoobYang.corejava").addHandler(handler);
}
catch (IOException e) {
Logger.getLogger("com.NoobYang.corejava").log(Level.SEVERE,
"Can't creat log file handler",e);
}
}
//@code EventQueue}是一个独立于平台的类,它从底层的对等类和受信任的应用程序类中对事件进行排队。
EventQueue.invokeLater(() ->
{
Handler wi