簡體   English   中英

JOPtionPane遇到問題

[英]Having trouble with JOPtionPane

我是Java的初學者,遇到一些困難。 我正在使用JOPtionPane詢問用戶一些問題。 但是,我的困難來自於這樣一個事實:當用戶單擊X或取消程序中的任何位置時,我不知道如何正確處理異常。 另外,當我查看Java API並了解如何實現標題時,我嘗試了一下,但是現在應該在文本字段中放置標題。

從程序的其他部分退出也會在主線程中產生異常,因此我希望能夠捕獲這些異常並正常退出而不是出錯。 同樣,在輸入對話框中,標題“ Investment Advisor”將顯示在文本字段內。 我看過API,看來我使用的是正確的格式,但顯然沒有。 具有輸入對話框的圖標也是如此。 我不希望它們顯示,但是程序無法編譯,並且說如果我將它們放在輸入對話框中也找不到符號。 它適用於optiondialog。

以上是解決問題之前的問題

非常感謝那些花時間閱讀本文的人。

這是代碼(請注意investment1和investment2只是具有適當公式的簡單靜態方法):

編輯下面的代碼; 只需嘗試為空字符串添加錯誤檢查

public static void main(String[] args) 
{  
String initialAmt_Str, targetAmt_Str, interestPct_Str, years_Str, result;
double principle = 0, target = 0, interest = 0;
int again = JOptionPane.NO_OPTION, time = 0;

NumberFormat fmt = NumberFormat.getCurrencyInstance();

do {  
     Object[] options = {"Compute years to reach target amount",
        "Compute target amount given number of years"};

     int choice = JOptionPane.showOptionDialog(null, "Please choose what you would like to do.",
            "Investment Advisor", JOptionPane.YES_NO_OPTION,
            JOptionPane.PLAIN_MESSAGE, null, options, null);

     if (choice != JOptionPane.CANCEL_OPTION) 
     {

        if (choice == 1) 
        {        
           initialAmt_Str = JOptionPane.showInputDialog (null, "Enter the principle:", "Investment Advisor", 
           JOptionPane.PLAIN_MESSAGE);

           if (initialAmt_Str != null) 
              principle = Double.parseDouble(initialAmt_Str);

           else 
              System.exit(0);


           interestPct_Str = JOptionPane.showInputDialog (null, "Enter the interest rate as a"
                        + " percentage (without the percent sign):", "Investment Advisor", JOptionPane.PLAIN_MESSAGE); 

           if (interestPct_Str != null) 
              interest = Double.parseDouble(interestPct_Str);

           else 
              System.exit(0); 


           years_Str = JOptionPane.showInputDialog (null, "Enter the amount of years:", "Investment Advisor", 
           JOptionPane.PLAIN_MESSAGE);

           if (years_Str != null) 
              time = Integer.parseInt(years_Str);

           else 
              System.exit(0);  

           result = "Your target amount given the number of years is " + 
              fmt.format(investment2(principle, interest, time)) + ".";

           JOptionPane.showMessageDialog (null, result, "Investment Advisor", JOptionPane.PLAIN_MESSAGE);

           again = JOptionPane.YES_OPTION;
        }
     }

     else 
        again = JOptionPane.NO_OPTION;


     if (choice == 0) 
     {
        initialAmt_Str = JOptionPane.showInputDialog (null,"Enter the principle:","Investment Advisor",
        JOptionPane.PLAIN_MESSAGE);

        if (initialAmt_Str != null) 
           principle = Double.parseDouble(initialAmt_Str);

        else 
           System.exit(0);


        interestPct_Str = JOptionPane.showInputDialog (null, "Enter the interest rate as a"
                        + " percentage (without the percent sign):", "Investment Advisor", JOptionPane.PLAIN_MESSAGE); 

        if (interestPct_Str != null) 
           interest = Double.parseDouble(interestPct_Str);

        else 
           System.exit(0); 


        targetAmt_Str = JOptionPane.showInputDialog (null, "Enter your target amount:", "Investment Advisor", 
        JOptionPane.PLAIN_MESSAGE);


        if (targetAmt_Str != null)
           target = Double.parseDouble(targetAmt_Str);

        else
           System.exit(0);

        result = "You will reach your target amount in " + 
              investment1(principle, target, interest) + 
              (investment1(principle, target, interest) == 1 ? " year." : " years.");

        JOptionPane.showMessageDialog (null, result, "Investment Advisor", JOptionPane.PLAIN_MESSAGE);

        again = JOptionPane.YES_OPTION;

     }

     if (again != JOptionPane.NO_OPTION) 
        again = JOptionPane.showConfirmDialog(null, "Find Another?", "", JOptionPane.YES_NO_OPTION, 
        JOptionPane.PLAIN_MESSAGE); 

} while (again == JOptionPane.YES_OPTION);
}  

如果用戶單擊“ x”關閉按鈕,則JOptionPane將返回JOptionPane.CLOSED_OPTION

您應該先檢查一下...

if (choice != JOptionPane.CLOSED_OPTION) {
    // Do the normal stuff
} else {
    // Break out of the do loop
    break;
}

您還應該注意,使用JOptionPane.showOptionDialogJOptionPane將返回用戶選擇的選項的索引。

這意味着返回值0表示用戶選擇"Compute years to reach target" ,返回值1表示用戶選擇"Compute target given number of years"

使用JOptionPane.NO_OPTIONJOptionPane.YES_OPTION可能無法獲得預期的結果...

更新了示例

您的重組允許一個稍微“偷偷摸摸”的選項,而不是使用break,當用戶關閉選項對話框時,我簡單地將again變量設置為JOptionPane.NO_OPTION

public static void main(String[] args) {
        String initialAmt_Str, targetAmt_Str, interestPct_Str, years_Str, result;
        double principle, target, interest;
        int again, time;

        NumberFormat fmt = NumberFormat.getCurrencyInstance();

        do {
            Object[] options = {"Compute years to reach target",
                "Compute target given number of years"};

            int choice = JOptionPane.showOptionDialog(null, "Please choose what you would like to do.",
                    "Investment Advisor", JOptionPane.YES_NO_OPTION,
                    JOptionPane.PLAIN_MESSAGE, null, options, null);

            if (choice != JOptionPane.CANCEL_OPTION) {

                if (choice == 1) {
                    JOptionPane.showMessageDialog(null, "Compute target given number of years");
                } else if (choice == 0) {
                    JOptionPane.showMessageDialog(null, "Compute years to reach target");
                }

                again = JOptionPane.showConfirmDialog(null, "Find Another?");

            } else {
                again = JOptionPane.NO_OPTION;
            }
        } while (again == JOptionPane.YES_OPTION);
    }

更新了其他示例以處理取消的輸入

因此,如果用戶在輸入對話框中單擊“ x”按鈕,則他們將返回null 此時,您需要檢查是否為null結果,然后選擇要如何處理它。 在下面的示例中,我只是將again設置為等於JOptionPane.NO_OPTION

public static void main(String[] args) {
    String initialAmt_Str, targetAmt_Str, interestPct_Str, years_Str, result;
    double principle, target, interest;
    int again, time;

    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    do {
        Object[] options = {"Compute years to reach target",
            "Compute target given number of years"};

        int choice = JOptionPane.showOptionDialog(null, "Please choose what you would like to do.",
                "Investment Advisor", JOptionPane.YES_NO_OPTION,
                JOptionPane.PLAIN_MESSAGE, null, options, null);

        if (choice != JOptionPane.CANCEL_OPTION) {

            again = JOptionPane.YES_OPTION;
            if (choice == 1) {
                String input = JOptionPane.showInputDialog("Enter the principle:", "Investment Advisor");
                if (input != null) {
                    // Process further...
                    System.out.println("Continue processing...");
                } else {
                    again = JOptionPane.NO_OPTION;
                }
            } else if (choice == 0) {
                String input = JOptionPane.showInputDialog("Enter your target amount:", "Investment Advisor");
                if (input != null) {
                    // Process further...
                    System.out.println("Continue processing...");
                } else {
                    again = JOptionPane.NO_OPTION;
                }
            }

            if (again != JOptionPane.NO_OPTION) {
                again = JOptionPane.showConfirmDialog(null, "Find Another?");
            }

        } else {
            again = JOptionPane.NO_OPTION;
        }
    } while (again == JOptionPane.YES_OPTION);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM