【问题标题】:Making this confirmation dialog work in java使此确认对话框在 java 中工作
【发布时间】:2015-01-24 21:36:28
【问题描述】:

所以我试图在我的程序中运行一个确认对话框,询问用户是否想使用 import javax.swing.JOptionPane 升级他们的会员资格

但是,当我去执行时,程序总是恢复到好像我单击了“是”。请帮忙?

int option =
                     JOptionPane.showConfirmDialog(null,
                             "Would you like to become a member for $4.95?",
                             "Upgrade Membership",
                             JOptionPane.YES_NO_CANCEL_OPTION);
             if (answer == JOptionPane.YES_OPTION)
             {
                 JOptionPane.showMessageDialog(null, "Thank you. Your membership fee will be"
                         + " added to your receipt.");
                         dFee = 4.95;
                         sMember = "Premium";
             }
             if (answer == JOptionPane.NO_OPTION)
             {
                 JOptionPane.showMessageDialog(null, "Thank you for your consideration.");
                 sMember = "Regular";
             }
             if (answer == JOptionPane.CANCEL_OPTION)
             {
                 JOptionPane.showMessageDialog(null, "You may purchase a membership in the"
                         + " future.");
                 sMember = "Regular";
             }

【问题讨论】:

    标签: java swing confirmation


    【解决方案1】:

    所提供的 sn-p 最明显的问题可能是两个不同的变量被用于答案。 option 是实际接收用户响应的变量,而 answer 在别处定义,从未分配给,但在 if 语句中使用。

    简而言之,在if 语句中使用option 而不是answer

    【讨论】:

    • 哎呀,这么多人反对,让我在这里休息一下。无论如何,谢谢,那行得通。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多