📄 errorinfo.java
字号:
// $Id: ErrorInfo.java,v 1.6 2006/07/25 12:27:47 mike Exp $package org.faceless.pdf2.viewer;import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * Utility class to display an error message with a stack trace * * <p><i> * This code is copyright the Big Faceless Organization. You're welcome to * use, modify and distribute it in any form in your own projects, provided * those projects continue to make use of the Big Faceless PDF library. * </i></p> */class ErrorInfo{ static void displayThrowable(final Throwable throwable, String title, Component component) { // Get class name String className = throwable.getClass().getName(); className = className.substring(className.lastIndexOf('.') + 1); // Basic error message. String msg = throwable.getMessage(); final String basicMessage = className + ((msg != null) ? (": " + msg) : ""); // Here is a JLabel to display the message. final JLabel messageLabel = new JLabel(basicMessage); final JButton detailsButton = new JButton("Show Details"); // Our dialog will display a JOptionPane. JOptionPane pane = new JOptionPane(messageLabel, JOptionPane.ERROR_MESSAGE, JOptionPane.YES_NO_OPTION, null, new Object[] { detailsButton, "Exit" }); // This is the dialog box containing the pane. final JDialog dialog = pane.createDialog(component, title); dialog.setModal(false); // Add an event handler for the Details button detailsButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // Show or hide error details. String label = detailsButton.getText(); if (label.startsWith("Show")) { // JLabel can display simple HTML text messageLabel.setText(getHTMLDetails(throwable)); detailsButton.setText("Hide Details"); dialog.pack(); } else { messageLabel.setText(basicMessage); detailsButton.setText("Show Details"); dialog.pack(); } } }); dialog.setVisible(true); } private static String getHTMLDetails(Throwable throwable) { StringBuffer b = new StringBuffer("<html>"); int lengthOfLastTrace = 1; // initial value // Start with the specified throwable and loop through the chain of // causality for the throwable. while (throwable != null) { // Output Exception name and message, and begin a list b.append("<b>" + throwable.getClass().getName() + "</b>: " + throwable.getMessage() + "<ul>"); // Get the stack trace and output each frame. // Be careful not to repeat stack frames that were already reported // for the exception that this one caused. StackTraceElement[] stack = throwable.getStackTrace(); for (int i=0; i<=stack.length-lengthOfLastTrace;i++) { b.append("<li> in " + stack[i].getClassName() + ".<b>" + stack[i].getMethodName() + "</b>() at <tt>" + stack[i].getFileName() + ":" + stack[i].getLineNumber() + "</tt>"); } b.append("</ul>"); // end list // See if there is a cause for this exception throwable = throwable.getCause(); if (throwable != null) { // If so, output a header b.append("<i>Caused by: </i>"); // And remember how many frames to skip in the stack trace // of the cause exception lengthOfLastTrace = stack.length; } } b.append("</html>"); return b.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -