java - 取消按钮必须有一个功能

标签 java swing joptionpane

我在使用此取消按钮时遇到问题。

使用这些代码:

int deposit;
String dep =
   JOptionPane.showInputDialog("How much would you like to deposit?\n\t$: ");
deposit = Integer.parseInt(dep);

应该出现“确定”和“取消”按钮,但每当我单击取消按钮时,都没有任何响应。我想要的是,每当我单击“取消”按钮时,它都应该返回到主菜单,但是如何返回呢?

代码:

private static void processDeposit(String num, int rc){
    int deposit;
    String dep =
        JOptionPane.showInputDialog(
            "How much would you like to deposit?\n\t$: ");
    deposit = Integer.parseInt(dep);
    JOptionPane.showMessageDialog(
        null, "You have deposited $" + dep + " into the account of " + name);
    myAccount.setBalance(myAccount.getBalance() + deposit);
}

最佳答案

如果用户点击取消 dep 将返回 null

所以你可以这样做:

 if(input == null || (input != null && ("".equals(input))))   
 {

         //what you ever you need to do here

 }

关于java - 取消按钮必须有一个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14859256/

相关文章:

java - 选择/选项值相当于java?

Java:JTextAreas后面的JPanel的MouseListener

java - JOptionPane 传递自定义按钮

java - 导入库在 Play Framework 中出现错误

java - 强制 kafka 消费者轮询延迟最高的分区

java - 使用 JMX 或 JNDI 列出来自 Jboss 的所有部署

java - 如何让我的 ContainerRequestFilter 在 @ServerEndpoint onOpen 上调用?

java - 在 JButton 中调整 ImageIcon 的大小

java - 是否可以更改 JOptionPane.showMessageDialog() 中的字符串颜色

java - 输入对话框循环提前结束