import java.awt.*;experts hope the examples posted to enlighten enlighten ~ ~ ~ ~ ~
import java.awt.event.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;
/**
* A modification of the image viewer program that logs various events.
* @version 1.02 2007-05-31
* @author Cay Horstmann
*/
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.horstmann.corejava").setLevel(Level.ALL);
final int LOG_ROTATION_COUNT = 10;
Handler handler = new FileHandler("%h/LoggingImageViewer.log", 0, LOG_ROTATION_COUNT);
Logger.getLogger("com.horstmann.corejava").addHandler(handler);
}
catch (IOException e)
{
Logger.getLogger("com.horstmann.corejava").log(Level.SEVERE,
"Can't create log file handler", e);
}
}
EventQueue.invokeLater(new Runnable()
{
public void run()
{
Handler windowHandler = new WindowHandler();
windowHandler.setLevel(Level.ALL);
Logger.getLogger("com.horstmann.corejava").addHandler(windowHandler);
JFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Logger.getLogger("com.horstmann.corejava").fine("Showing frame");
frame.setVisible(true);
}
});
}
}
/**
* The frame that shows the image.
*/
class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
logger.entering("ImageViewerFrame", "<init>");
setTitle("LoggingImageViewer");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// set up menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new FileOpenListener());
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
logger.fine("Exiting.");
System.exit(0);
}
});
// use a label to display the images
label = new JLabel();
add(label);
logger.exiting("ImageViewerFrame", "<init>");
}
private class FileOpenListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
logger.entering("ImageViewerFrame.FileOpenListener", "actionPerformed", event);
// set up file chooser
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
// accept all files ending with .gif
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
}
public String getDescription()
{
return "GIF Images";
}
});
// show file chooser dialog
int r = chooser.showOpenDialog(ImageViewerFrame.this);
// if image file accepted, set it as icon of the label
if (r == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
logger.log(Level.FINE, "Reading file {0}", name);
label.setIcon(new ImageIcon(name));
}
else logger.fine("File open dialog canceled.");
logger.exiting("ImageViewerFrame.FileOpenListener", "actionPerformed");
}
}
private JLabel label;
private static Logger logger = Logger.getLogger("com.horstmann.corejava");
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 400;
}
/**
* A handler for displaying log records in a window.
*/
class WindowHandler extends StreamHandler
{
public WindowHandler()
{
frame = new JFrame();
final JTextArea output = new JTextArea();
output.setEditable(false);
frame.setSize(200, 200);
frame.add(new JScrollPane(output));
frame.setFocusableWindowState(false);
frame.setVisible(true);
setOutputStream(new OutputStream()
{
public void write(int b)
{
} // not called
public void write(byte[] b, int off, int len)
{
output.append(new String(b, off, len));
}
});
}
public void publish(LogRecord record)
{
if (!frame.isVisible()) return;
super.publish(record);
flush();
}
private JFrame frame;
}
------ Solution ----------------- --------------------------- cat xxx.log or tail-f xxx under
linux . log-n200
windows down type xxx.log | more
------ For reference only ------------------------- --------------
log here is a plug-in
------ For reference only --------------- ------------------------
Oh saw that if the default , then ( they do not install this program plug-in) ; logs are generally where to look? ?
------ For reference only -------------------------------------- -
I put the complex part of the program to remove the back end of all compile and run and then enter the command line type ImageViewer.log ; displays file not found ah
mport java.awt.EventQueue;the program log on to retain only a single line of code
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.logging.*;
/**
* A program for viewing images.
* @version 1.22 2007-05-21
* @author Cay Horstmann
*/
public class ImageViewer
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
Logger.getLogger("com.houstmann.corejava").setLevel(Level.ALL);
JFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* A frame with a label to show an image.
*/
class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle("ImageViewer");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// use a label to display the images
label = new JLabel();
add(label);
// set up the file chooser
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
// set up the menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// show file chooser dialog
int result = chooser.showOpenDialog(null);
// if file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
});
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
}
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 400;
}
------ For reference only ----- ----------------------------------
system default log somewhere ? ? How to see ? ?
------ For reference only -------------------------------------- -
that nobody knows ~ ~
------ For reference only ---------------------------- -----------
Handler handler = new FileHandler ("% h / LoggingImageViewer.log", 0, LOG_ROTATION_COUNT) see did not understand this sentence ? % h document represents the current user is the meaning of my document , for example, you run the program in admin user, then the log file in
C :/ Users / Admin ... that folder , the file name is LoggingImageViewer.log
I do not understand this sentence . .
if (System.getProperty ("java.util.logging.config.class") == null
&& System.getProperty ("java.util.logging.config.file" ) == null)
why this judgment . . .
没有评论:
发表评论