如何在我的异常中使用actionPerformed计算的值?

时间:2014-07-25 19:32:33

标签: java swing exception

这是我的计划的一部分。我知道它很乱,抱歉! 我想在我的异常中使用我的方法为(余数)计算的值,但是它的初始值为零。

double remainder = 0;

public void actionPerformed(ActionEvent e) {
    String message = e.getActionCommand();
    try {
        if (e.getSource() == b24 && books[droplist2.getSelectedIndex()] != "Choose a book ") {
            if (t22.getText().equals("")) {
                if (Double.parseDouble(price[droplist2.getSelectedIndex()]) > remainder)
                    throw new MyExcption("Sorry, you don't have enough money to buy this book!");
                price1 = Double.parseDouble(price[droplist2.getSelectedIndex()]);
                add_sound.play();
                book1 = books[droplist2.getSelectedIndex()];
                t22.setText(book1);
            } else if (!t22.getText().equals("") && t23.getText().equals("")) {
                if (Double.parseDouble(price[droplist2.getSelectedIndex()]) > remainder)
                    throw new MyExcption("Sorry, you don't have enough money to buy this book!");
                price2 = Double.parseDouble(price[droplist2.getSelectedIndex()]);
                add_sound.play();
                book2 = books[droplist2.getSelectedIndex()];
                t23.setText(book2);
            }
            total = price1 + price2;
            t26.setText(Double.toString(total));
            balance = Double.parseDouble(t1.getText());
            remainder = balance - total;
            t27.setText(Double.toString(remainder));
        }
        if (message.equalsIgnoreCase("delete")) {
            sound3.play();
        }
        //To delete each textfield
        //Delete the first
        if (e.getSource().equals(d1) && !t22.getText().equals("")) {
            t22.setText("");
            total -= price1;
            price1 = 0;
            t26.setText(Double.toString(total));
            remainder = balance - total;
            t27.setText(Double.toString(remainder));
        }
        //Delete the second textfield
        else if (e.getSource().equals(d2) && !t23.getText().equals("")) {
            t23.setText("");
            total -= price2;
            price2 = 0;
            t26.setText(Double.toString(total));
            remainder = balance - total;
            t27.setText(Double.toString(remainder));
        }
    } catch (NumberFormatException exc) {
        JOptionPane.showMessageDialog(this, "Please enter a numbers !", "Error", JOptionPane.ERROR_MESSAGE);
        t1.setText("");
    } catch (MyException exc2) {
        JOptionPane.showMessageDialog(this, exc2.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}

0 个答案:

没有答案