gpt4 book ai didi

java - 如何使用 JOptionPane 的响应?

转载 作者:行者123 更新时间:2023-11-30 08:22:29 25 4
gpt4 key购买 nike

这是我第一次使用确认框,我想请教一些如何使用它的建议,我想使用用户输入的“是或否”,但不知道该怎么做?如果我想在 if 语句中引用来自 JOptionPane 的输入,该怎么做?

JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);

谢谢。

最佳答案

像这样使用它:

   int result = JOptionPane.showConfirmDialog(null, "Click yes to terminate. ", "TERMINATE SIMULATION?", JOptionPane.YES_NO_OPTION);

if (JOptionPane.YES_OPTION == result) {
System.out.println("yes");
} else if (JOptionPane.NO_OPTION == result) {
System.out.println("No");
}else{
System.out.println("Nothing");
}

还可以在下面找到选项类型和返回值(来自源代码):

 /**
* Type meaning Look and Feel should not supply any options -- only
* use the options from the <code>JOptionPane</code>.
*/
public static final int DEFAULT_OPTION = -1;
/** Type used for <code>showConfirmDialog</code>. */
public static final int YES_NO_OPTION = 0;
/** Type used for <code>showConfirmDialog</code>. */
public static final int YES_NO_CANCEL_OPTION = 1;
/** Type used for <code>showConfirmDialog</code>. */
public static final int OK_CANCEL_OPTION = 2;

//
// Return values.
//
/** Return value from class method if YES is chosen. */
public static final int YES_OPTION = 0;
/** Return value from class method if NO is chosen. */
public static final int NO_OPTION = 1;
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
/** Return value form class method if OK is chosen. */
public static final int OK_OPTION = 0;
/** Return value from class method if user closes window without selecting
* anything, more than likely this should be treated as either a
* <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
public static final int CLOSED_OPTION = -1;

此外,不要直接对响应值进行 int 检查,例如 if(1==result) 对于 NO_OPTION,始终使用 JoptionPane 中的常量 类。

关于java - 如何使用 JOptionPane 的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24341216/

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