gpt4 book ai didi

java - 使用 JOPtionPane 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 04:09:50 25 4
gpt4 key购买 nike

我是 Java 初学者,我在解决问题时遇到了一些困难。我正在使用 JOPtionPane 向用户询问一些问题。然而,我的困难来自这样的事实:当用户单击 X 或取消程序中的任何位置时,我不知道如何正确处理异常。另外,当我查看 Java API 并了解如何实现标题时,我尝试了它,但现在应该将标题放置在文本字段中。

从程序的其他部分退出也会在主线程中产生异常,因此我希望能够捕获这些异常并正常退出而不是出错。此外,在输入对话框中,标题“投资顾问”显示在文本字段内。我查看了 API,似乎我使用了正确的形式,但显然不是。输入对话框的图标也是如此。我不希望它们显示,但程序无法编译,并且如果我将它们放入输入对话框中,则显示找不到符号。不过它适用于选项对话框。

以上是问题解决之前

非常感谢那些花时间阅读本文的人。

以下是代码(请注意,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 表示用户选择了“计算达到目标的年数” 并且返回值 1表示用户选择了“计算给定年数的目标”

使用 JOptionPane.NO_OPTIONJOptionPane.YES_OPTION 可能无法获得您期望的结果...

已更新示例

您的重组允许稍微“偷偷摸摸”的选项,而不是使用中断,当用户关闭选项对话框时,我简单地将 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);
}

关于java - 使用 JOPtionPane 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com