源代码如下:package test;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2008</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */public class Frame1
    extends JFrame {
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JTextArea jTextArea1 = new JTextArea();
  JTextArea jTextArea2 = new JTextArea();
  JLabel jLabel3 = new JLabel();
  JTextArea jTextArea3 = new JTextArea();
  Component component1;
  Frame1 frame1;
  Dialog d;
  public Frame1() {
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }  public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
    frame1.setTitle("演示窗体");
    frame1.setBounds(200, 200, 350, 350);
    frame1.setVisible(true);
    Dialog d = new Dialog(frame1,"This is a error"); //创建一个对话框的对象  }  private void jbInit() throws Exception {
    component1 = Box.createGlue();
    jButton1.setBounds(new Rectangle(100, 218, 84, 24));
    jButton1.setText("OK");
    jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    this.getContentPane().setLayout(null);
    jButton2.setBounds(new Rectangle(206, 218, 91, 25));
    jButton2.setText("Cancel");
    jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
    jLabel1.setText("a");
    jLabel1.setBounds(new Rectangle(125, 56, 76, 30));
    jLabel2.setText("b");
    jLabel2.setBounds(new Rectangle(125, 108, 34, 16));
    jTextArea1.setText("");
    jTextArea1.setBounds(new Rectangle(211, 62, 60, 18));
    jTextArea2.setText("");
    jTextArea2.setBounds(new Rectangle(211, 114, 60, 18));
    jLabel3.setText("a+b=");
    jLabel3.setBounds(new Rectangle(128, 170, 34, 16));
    jTextArea3.setText("");
    jTextArea3.setBounds(new Rectangle(209, 168, 62, 16));
    component1.setBounds(new Rectangle(343, 254, 42, 36));
    this.getContentPane().add(jTextArea1, null);
    this.getContentPane().add(jTextArea2, null);
    this.getContentPane().add(jLabel1, null);
    this.getContentPane().add(jLabel2, null);
    this.getContentPane().add(jButton1, null);
    this.getContentPane().add(jButton2, null);
    this.getContentPane().add(component1, null);
    this.getContentPane().add(jLabel3, null);
    this.getContentPane().add(jTextArea3, null);
  }  void jButton1_actionPerformed(ActionEvent e) {
try{
   String a = jTextArea1.getText();
   Integer i = new Integer(a);
   int j1 = i.intValue();
   String a2 = jTextArea2.getText();
    Integer i2 = new Integer(a2);
    int  j2 =i2.intValue();
   int j3= j1+ j2;
    String a3 = new String().valueOf(j3);
   jTextArea3.setText(a3);
    }
catch(Exception en)
 { d.setVisible(true);//为了捕获出现异常输入时,弹出对话框。
}
 }  void jButton2_actionPerformed(ActionEvent e) {
     System.exit(1);
  }
}class Frame1_jButton1_actionAdapter
    implements java.awt.event.ActionListener {
  Frame1 adaptee;  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}class Frame1_jButton2_actionAdapter
    implements java.awt.event.ActionListener {
  Frame1 adaptee;  Frame1_jButton2_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}
如上所示:
当我输入字符类型的时候,能够捕获到异常,但无法正常执行对话框的提示信息,请大家帮小弟分析一下原因。

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;/**
     * <p>
     * Title:
     * </p>
     * <p>
     * Description:
     * </p>
     * <p>
     * Copyright: Copyright (c) 2008
     * </p>
     * <p>
     * Company:
     * </p>
     * 
     * @author not attributable
     * @version 1.0
     */public class Frame1 extends JFrame {
        JButton jButton1 = new JButton();    JButton jButton2 = new JButton();    JLabel jLabel1 = new JLabel();    JLabel jLabel2 = new JLabel();    JTextArea jTextArea1 = new JTextArea();    JTextArea jTextArea2 = new JTextArea();    JLabel jLabel3 = new JLabel();    JTextArea jTextArea3 = new JTextArea();    Component component1;    Frame1 frame1;    Dialog d;    public Frame1() {
            try {
                jbInit();
                d = new Dialog(frame1, "This is a error");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public static void main(String[] args) {
            Frame1 frame1 = new Frame1();
            frame1.setTitle("Frame");
            frame1.setBounds(200, 200, 350, 350);
            frame1.setVisible(true);
    //        Dialog d = new Dialog(frame1, "This is a error");    }    private void jbInit() throws Exception {
            component1 = Box.createGlue();
            jButton1.setBounds(new Rectangle(100, 218, 84, 24));
            jButton1.setText("OK");
            jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
            this.getContentPane().setLayout(null);
            jButton2.setBounds(new Rectangle(206, 218, 91, 25));
            jButton2.setText("Cancel");
            jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
            jLabel1.setText("a");
            jLabel1.setBounds(new Rectangle(125, 56, 76, 30));
            jLabel2.setText("b");
            jLabel2.setBounds(new Rectangle(125, 108, 34, 16));
            jTextArea1.setText("");
            jTextArea1.setBounds(new Rectangle(211, 62, 60, 18));
            jTextArea2.setText("");
            jTextArea2.setBounds(new Rectangle(211, 114, 60, 18));
            jLabel3.setText("a+b=");
            jLabel3.setBounds(new Rectangle(128, 170, 34, 16));
            jTextArea3.setText("");
            jTextArea3.setBounds(new Rectangle(209, 168, 62, 16));
            component1.setBounds(new Rectangle(343, 254, 42, 36));
            this.getContentPane().add(jTextArea1, null);
            this.getContentPane().add(jTextArea2, null);
            this.getContentPane().add(jLabel1, null);
            this.getContentPane().add(jLabel2, null);
            this.getContentPane().add(jButton1, null);
            this.getContentPane().add(jButton2, null);
            this.getContentPane().add(component1, null);
            this.getContentPane().add(jLabel3, null);
            this.getContentPane().add(jTextArea3, null);
        }    void jButton1_actionPerformed(ActionEvent e) {
            try {
                String a = jTextArea1.getText();
                Integer i = new Integer(a);
                int j1 = i.intValue();
                String a2 = jTextArea2.getText();
                Integer i2 = new Integer(a2);
                int j2 = i2.intValue();
                int j3 = j1 + j2;
                String a3 = new String().valueOf(j3);
                jTextArea3.setText(a3);
            } catch (Exception en) {
                d.setVisible(true);
            }
        }    void jButton2_actionPerformed(ActionEvent e) {
            System.exit(1);
        }
    }class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
        Frame1 adaptee;    Frame1_jButton1_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void actionPerformed(ActionEvent e) {
            adaptee.jButton1_actionPerformed(e);
        }
    }class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
        Frame1 adaptee;    Frame1_jButton2_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void actionPerformed(ActionEvent e) {
            adaptee.jButton2_actionPerformed(e);
        }
    }
    对象用错了