//Double(String)构造函数可能会抛出异常,捕获它就可以了
try{
    Double dbl1=new Double(input1TextField.getText());
    Double dbl2=new Double(input2TextField.getText());
}catch(NumberFormatException){
    //弹出对话框
    return;
}
double d1 = dbl1.doubleValue(); 
double d2 = dbl2.doubleValue();
...

解决方案 »

  1.   

    //Double(String)构造函数可能会抛出NumberFormatException异常,捕获它就可以了
    try{
        Double dbl1=new Double(input1TextField.getText());
        Double dbl2=new Double(input2TextField.getText());
    }catch(NumberFormatException nfe){
        //弹出对话框
        return;
    }
    double d1 = dbl1.doubleValue(); 
    double d2 = dbl2.doubleValue();
    ...
      

  2.   

    //sorry,修改一下代码
    Double dbl1,dbl2;
    try{
        dbl1=new Double(input1TextField.getText());
        dbl2=new Double(input2TextField.getText());
    }catch(NumberFormatException nfe){
        //弹出对话框,do something
        return;
    }
    double d1 = dbl1.doubleValue(); 
    double d2 = dbl2.doubleValue();
    ...
      

  3.   

    为什么不能直接这样:        try {
                                double d1 = 
        new Double(input1TextField.getText()).doubleValue(); 
     double d2 = 
        new Double(input2TextField.getText()).doubleValue(); 
                               }catch(NumberFormatException nfe){
                                //do something 
                                  return ;
                                }
      

  4.   

    try 块捕获操作异常,声明语句应放到类体开头。谢谢。
      

  5.   

    ... ...
    catch(NumberFormatException nfe){
    System.out.println(input1TextField.getText()+"or"+input2TextField.getText()+"is wrong number");
    return;
    }
    ... ...调试可以通过,但
    为什么出现这样的问题:当我只要输入非字符时,显示的都是input1TextField.getText()的内容!如:我输入的是2与a时:显示的是2or2 is wrong number;
          输入的是a与2时:显示的是aora is wrong number;
    不理解?
      

  6.   

    //运行过了,没问题
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Calculator4 extends JFrame {    // Components are treated as attributes, so that they will be
        // visible to all of the methods of the class.    private Container contentPane;    // Use descriptive names for components where possible; it makes
        // your job easier later on!    private JPanel leftPanel;
        private JPanel centerPanel;
        private JPanel buttonPanel;
        private JTextField input1TextField;
        private JTextField input2TextField;
        private JLabel answerLabel;
        private JButton plusButton;
        private JButton minusButton;    // Constructor.
        public Calculator4() {
            // Invoke the generic JFrame constructor.
            super("Simple Calculator");        // The content pane container is now declared to be an
            // attribute.  Note that the use of "this." is unnecessary;
            // we could have simply written:
            // contentPane = getContentPane();
            contentPane = this.getContentPane();
            this.setSize(250, 100);        // Technique for centering a frame on the screen.
            Dimension frameSize = this.getSize();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation((screenSize.width - frameSize.width)/2,
                    (screenSize.height - frameSize.height)/2);        leftPanel = new JPanel();
            leftPanel.setLayout(new GridLayout(3, 1));
            leftPanel.add(new JLabel("Input 1:  "));
            leftPanel.add(new JLabel("Input 2:  "));
            leftPanel.add(new JLabel("Answer:  "));
            contentPane.add(leftPanel, BorderLayout.WEST);        centerPanel = new JPanel();
            centerPanel.setLayout(new GridLayout(3, 1));
            input1TextField = new JTextField(10);
            input2TextField = new JTextField(10);
            answerLabel = new JLabel();
            centerPanel.add(input1TextField);        centerPanel.add(input2TextField);
            centerPanel.add(answerLabel);
            contentPane.add(centerPanel, BorderLayout.CENTER);        buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(2, 1));
            plusButton = new JButton("+");
            minusButton = new JButton("-");
            buttonPanel.add(plusButton);
            buttonPanel.add(minusButton);
            contentPane.add(buttonPanel, BorderLayout.EAST);        // Add behaviors!  This time we use the SAME
            // listener object to listen to BOTH buttons.        ActionListener l = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Double dbl1,dbl2;
                    try{
                        dbl1=new Double(input1TextField.getText());
                        dbl2=new Double(input2TextField.getText());
                    }catch(NumberFormatException nfe){
                        //弹出对话框,do something
                        System.out.println(input1TextField.getText()+"or"+input2TextField.getText()+" is wrong number");
                        return;
                    }
                    double d1 = dbl1.doubleValue();
                    double d2 = dbl2.doubleValue();
                    if (e.getSource() == plusButton)
                        answerLabel.setText("" + (d1 + d2));
                    else answerLabel.setText("" + (d1 - d2));
                }
            };        plusButton.addActionListener(l);
            minusButton.addActionListener(l);        this.setVisible(true);
        }    public static void main(String[] args) {
            Calculator4 c = new Calculator4();
            c.pack();
            c.show();
        }
    }