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

📄 errordialog.java

📁 经典游戏炸弹人游戏
💻 JAVA
字号:
/*
 * ErrorDialog.java
 * Created on March 9, 2001, 3:06 PM
 */
import java.awt.*;
import javax.swing.*;
import java.io.*;

/**
 * This class displays an error dialog then exits the program when specified.
 *
 * @author Sammy Leong
 * @version 1.0
 */
public class ErrorDialog
{
    /**
     * Construct from an Exception object.
     * @param e exception object thrown
     */
    public ErrorDialog(Exception e)
    {
        /** store the stack trace (error message) into a string variable */
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer, true));
        String result = new String(" " + writer.toString() + "\n" +
        " CLICK OK TO TERMINATE THE PROGRAM.");
        
        /** create a text field and put the error message into it */
        JTextArea ta = new JTextArea(result);
        /** make it read only */
        ta.setEditable(false);
        
        /** create a scrollpane to contain the text field */
        JScrollPane scroller = new JScrollPane(ta);
        /** create a container to contain the scrollpane */
        JOptionPane pane = new JOptionPane(scroller);
        /** setup the scrollpane */
        pane.setOptionType(-JOptionPane.NO_OPTION);
        pane.setMessageType(JOptionPane.ERROR_MESSAGE);
        
        /** create and show the dialog */
        JDialog dialog = pane.createDialog(null, "Exception Caught");
        dialog.setResizable(false);
        dialog.show();
        Object selection = pane.getValue();
        
        /** exit the program */
        System.exit(-1);
    }
    
    /** 
     * Construct from an Exception object and a boolean flag of whether to 
     * terminate the program or not.
     * @param e exception object thrown
     */    
    public ErrorDialog(Exception e, boolean terminate)
    {
        /** store the stack trace (error message) into a string variable */
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer, true));
        String result = new String(" " + writer.toString());
        
        /** create a text field and put the error message into it */
        JTextArea ta = new JTextArea(result);
        ta.setEditable(false);
        
        /** create a scrollpane to contain the text field */
        JScrollPane scroller = new JScrollPane(ta);
        /** create a container to contain the scrollpane */
        JOptionPane pane = new JOptionPane(scroller);
        /** setup the scrollpane */
        pane.setOptionType(-JOptionPane.NO_OPTION);
        pane.setMessageType(JOptionPane.ERROR_MESSAGE);
        
        /** create and show the dialog */
        JDialog dialog = pane.createDialog(null, "Exception Caught");
        dialog.setResizable(false);
        dialog.show();
        Object selection = pane.getValue();
        
        /** exit the program if terminate is tree */
        if (terminate)
            System.exit(-1);
    }        
}

⌨️ 快捷键说明

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